diff --git a/docs/chapter1/section3/0_hello-server.md b/docs/chapter1/section3/0_hello-server.md index 93888e5a..135ca43e 100644 --- a/docs/chapter1/section3/0_hello-server.md +++ b/docs/chapter1/section3/0_hello-server.md @@ -2,7 +2,7 @@ ## ファイルの作成 -今回は、Go と、Go の有名な web フレームワークである [Echo](https://echo.labstack.com/) を使ってサーバーアプリケーションを作っていきます。 +今回は、Rust と、Rust の有名な web フレームワークである [axum](https://github.com/tokio-rs/axum) を使ってサーバーアプリケーションを作っていきます。 `~/develop/hello-server`というディレクトリを作成し、そのディレクトリを開きます。 ```bash @@ -25,11 +25,22 @@ $ man mkdir 変えた場合には適宜読み替えてください。 ::: -作ったディレクトリの中に`main.go`を作成し、以下のプログラムを書き込みます。 +作成したディレクトリを、Rust プロジェクトとして初期化します。 +以下のコマンドを実行してみましょう。 +```bash +# Rust プロジェクトの初期化をする。 +$ cargo init +``` + +すると、`src/main.rs`を含むいくつかのファイルが生成されます。 + +`src/main.rs`に以下のプログラムを書き込みましょう。 + +<<<@/chapter1/section3/src/1-1_hello-server.rs -<<<@/chapter1/section3/src/1-1_hello-server.go +axum は、[Rust の標準ライブラリ](https://doc.rust-lang.org/std/)に入っていない外部ライブラリなので、外部からダウンロードしなければなりません。しかし、`cargo` という Rust のパッケージマネージャを使えば、簡単にダウンロードできます。 -Echo は、[Go の標準ライブラリ](https://pkg.go.dev/std)に入っていない外部ライブラリなので、外部からダウンロードしなければなりません。しかし、Go にはそれを自動でやってくれる [Go module](https://go.dev/doc/tutorial/create-module) という便利な機能があるので使ってみましょう。以下を VSCode 内のターミナルで実行してください。(他のターミナルでも可) +以下を VSCode 内のターミナルで実行してください。(他のターミナルでも可) :::tip **ターミナルの開き方** @@ -39,21 +50,16 @@ Echo は、[Go の標準ライブラリ](https://pkg.go.dev/std)に入ってい ::: ```bash -# Go module を初期化して、足りない物をインストールし、使われてない物を削除する。 - -$ go mod init develop -$ go mod tidy +# Rust プロジェクトに axum と tokio の依存を追加する。 +$ cargo add axum +$ cargo add tokio --features rt-multi-thread,macros ``` -:::tip -本来この `develop` の所にはリポジトリ名を入れることが多いです。詳しくは[公式ドキュメント](https://go.dev/doc/modules/managing-dependencies#naming_module)を参照してください。 -::: - -続けて、`main.go` を実行してサーバーを立てましょう。 +続けて、`main.rs` を実行してサーバーを立てましょう。 ```bash # 先ほど書いたファイルを実行して、サーバーを立てる -$ go run main.go +$ cargo run ``` 以下のような画面が出れば起動できています。 @@ -101,16 +107,16 @@ localhost は自分自身を表すドメインなので、自分のブラウザ ## 基本問題 エンドポイントとして自分の traQ ID のものを生やして自己紹介を返すようにしてみましょう。 -`main.go`に`/{自分の traQ ID}`処理を追加して作ってください。 +`main.rs`に`/{自分の traQ ID}`処理を追加して作ってください。 :::tip -この章では、この`main.go`に処理を追加していきます。 +この章では、この`main.rs`に処理を追加していきます。 以降のコードではすでに作ったエンドポイントを省略していますが、作ったエンドポイントは消さずに、新しいエンドポイントを追加していくようにしてください。 ::: -作り終わったら、変更を反映させるために、`go run main.go`を実行したターミナル上で`Ctrl+C`を押してサーバーを止めた後、また`go run main.go`してサーバーを立て直しましょう。 -今後`main.go`を書き換えたらこの工程を行うようにして下さい。 +作り終わったら、変更を反映させるために、`cargo run`を実行したターミナル上で`Ctrl+C`を押してサーバーを止めた後、また`cargo run`してサーバーを立て直しましょう。 +今後`main.rs`を書き換えたらこの工程を行うようにして下さい。 サーバーの立て直しができたら、ブラウザで`http://localhost:8080/{自分の traQ ID}` にアクセスするか、以下のコマンドを実行して、上手く出来ていることを確認しましょう。 ```bash diff --git a/docs/chapter1/section3/assets/hello_server.png b/docs/chapter1/section3/assets/hello_server.png index 404de3ec..91e29091 100644 Binary files a/docs/chapter1/section3/assets/hello_server.png and b/docs/chapter1/section3/assets/hello_server.png differ diff --git a/docs/chapter1/section3/assets/hello_server_detail.png b/docs/chapter1/section3/assets/hello_server_detail.png index 08efe003..780ab2ef 100644 Binary files a/docs/chapter1/section3/assets/hello_server_detail.png and b/docs/chapter1/section3/assets/hello_server_detail.png differ diff --git a/docs/chapter1/section3/assets/hello_server_localhost.png b/docs/chapter1/section3/assets/hello_server_localhost.png index 2db0c019..b31c125c 100644 Binary files a/docs/chapter1/section3/assets/hello_server_localhost.png and b/docs/chapter1/section3/assets/hello_server_localhost.png differ diff --git a/docs/chapter1/section3/assets/hello_server_me.png b/docs/chapter1/section3/assets/hello_server_me.png index 934c7bed..d48789ec 100644 Binary files a/docs/chapter1/section3/assets/hello_server_me.png and b/docs/chapter1/section3/assets/hello_server_me.png differ diff --git a/docs/chapter1/section3/assets/hello_server_success.png b/docs/chapter1/section3/assets/hello_server_success.png index 3423269a..a112bb9a 100644 Binary files a/docs/chapter1/section3/assets/hello_server_success.png and b/docs/chapter1/section3/assets/hello_server_success.png differ diff --git a/docs/chapter1/section3/src/1-1_hello-server.go b/docs/chapter1/section3/src/1-1_hello-server.go deleted file mode 100644 index 68c5826a..00000000 --- a/docs/chapter1/section3/src/1-1_hello-server.go +++ /dev/null @@ -1,21 +0,0 @@ -package main - -import ( - "net/http" - - "github.com/labstack/echo/v4" -) - -func main() { - // Echoの新しいインスタンスを作成 - e := echo.New() - - // 「/hello」というエンドポイントを設定する - e.GET("/hello", func(c echo.Context) error { - // HTTPステータスコードは200番で、文字列「Hello, World.」をクライアントに返す - return c.String(http.StatusOK, "Hello, World.\n") - }) - - // Webサーバーをポート番号8080で起動し、エラーが発生した場合はログにエラーメッセージを出力する - e.Logger.Fatal(e.Start(":8080")) -} diff --git a/docs/chapter1/section3/src/1-1_hello-server.rs b/docs/chapter1/section3/src/1-1_hello-server.rs new file mode 100644 index 00000000..3949b6de --- /dev/null +++ b/docs/chapter1/section3/src/1-1_hello-server.rs @@ -0,0 +1,22 @@ +use axum::{routing::get, Router}; + +#[tokio::main] +async fn main() { + // 「/hello」というエンドポイントを設定する + let app = Router::new().route("/hello", get(handler)); + + // ポート8080でリスナーを作成する + let listener = tokio::net::TcpListener::bind("127.0.0.1:8080") + .await + .unwrap(); + + println!("listening on {}", listener.local_addr().unwrap()); + + // サーバーを起動する + axum::serve(listener, app).await.unwrap(); +} + +// 文字列「Hello, World.」をクライアントに返す +async fn handler() -> String { + String::from("Hello, World.") +} diff --git a/docs/chapter1/section3/src/1-2_hello-server-me.go b/docs/chapter1/section3/src/1-2_hello-server-me.go deleted file mode 100644 index 2e98a388..00000000 --- a/docs/chapter1/section3/src/1-2_hello-server-me.go +++ /dev/null @@ -1,26 +0,0 @@ -package main - -import ( - "net/http" - - "github.com/labstack/echo/v4" -) - -func main() { - // Echoの新しいインスタンスを作成 - e := echo.New() - - // 「/hello」というエンドポイントを設定する - e.GET("/hello", func(c echo.Context) error { - // HTTPステータスコードは200番で、文字列「Hello, World.」をクライアントに返す - return c.String(http.StatusOK, "Hello, World.\n") - }) - - // 「/pikachu」というエンドポイントを設定する - e.GET("/pikachu", func(c echo.Context) error { - return c.String(http.StatusOK, "始めまして、@pikachuです。\nケモノ(特に四足歩行)や、低頭身デフォルメマスコット(TDM)が大好きです。\n普段はVRChatに生息しています。twitter: @pikachu0310VRC") - }) - - // Webサーバーをポート番号8080で起動し、エラーが発生した場合はログにエラーメッセージを出力する - e.Logger.Fatal(e.Start(":8080")) -} diff --git a/docs/chapter1/section3/src/1-2_hello-server-me.rs b/docs/chapter1/section3/src/1-2_hello-server-me.rs new file mode 100644 index 00000000..d189607a --- /dev/null +++ b/docs/chapter1/section3/src/1-2_hello-server-me.rs @@ -0,0 +1,31 @@ +use axum::{routing::get, Router}; + +#[tokio::main] +async fn main() { + // 「/hello」「/kenken」の2つのエンドポイントを持つアプリケーションを作成 + let app = Router::new() + .route("/hello", get(hello_handler)) + .route("/kenken", get(me_handler)); + + // ポート8080でリスナーを作成する + let listener = tokio::net::TcpListener::bind("127.0.0.1:8080") + .await + .unwrap(); + + println!("listening on {}", listener.local_addr().unwrap()); + + // サーバーを起動する + axum::serve(listener, app).await.unwrap(); +} + +// 文字列「Hello, World.」をクライアントに返す +async fn hello_handler() -> String { + String::from("Hello, World.") +} + +// 自己紹介をクライアントに返す +async fn me_handler() -> String { + String::from( + "始めまして、@kenkenです。\nきらら作品(特に恋する小惑星、スロウスタート)が好きです。", + ) +}