NowというPaaSをご存知でしょうか。
コマンドラインにnowと打つだけで、デプロイが出来ちゃいます。
Rustがサポートされたので、実際にやって見ましょう。

Cargoを使う方法と、Cargoを使わない方法が用意されているようですが、普通Rustでプロジェクトを作るときはCargoを使うので、Cargoを使って、やって見ましょう

nowのインストール

npm i -g now

これだけ。npmかyarnでインストールしてくださいね。

nowのサインアップとログイン

https://zeit.co/signup でサインアップ

CLIで

now login

しましょう。

nowのプロジェクトを作る

cargo new now-rust-example

now-rust-exampleのところはなんでもいいです。

Cargo.tomlに依存パッケージを書く

[dependencies]
http = "0.1.16"
now_lambda = "0.1.2"

httpとnow_lambdaを追加する。

src/main.rsを編集する


use http::{header, StatusCode};
use now_lambda::{error::NowError, lambda, IntoResponse, Request, Response};
use std::error::Error;

fn handler(request: Request) -> Result<impl IntoResponse, NowError> {
    let uri = request.uri();
    let response = Response::builder()
        .status(StatusCode::OK)
        .header(header::CONTENT_TYPE, "text/html")
        .body(format!("Hello,World! This is lambda written by Rust. \n You made a request to the following URL: {}", uri))
        .expect("failed to render response");

    Ok(response)
}

// Start the runtime with the handler
fn main() -> Result<(), Box<dyn Error>> {
    Ok(lambda!(handler))
}

now.jsonで設定する

{
  "name": "now-rust-example",
  "version": 2,
  "builds": [
    {
      "src": "Cargo.toml",
      "use": "@now/rust"
    }
  ],
  "routes": [
    { "src": "/", "dest": "/now-rust-example"}
  ]
}

nameはなんでもお好きに。
routesで、/を/now-rust-exampleに割り当てることで
ルートパスで、rustのlambdaが実行されます。

いよいよ、Nowにデプロイ!!

now

はい、これだけ。

スクリーンショット 2019-03-14 14.55.22.png
スクリーンショット 2019-03-15 7.29.59.png

このように簡単にデプロイ出来ました!!

超簡単なんでやって見てください〜〜〜〜〜!

广告
将在 10 秒后关闭
bannerAds