Skip to content

Commit

Permalink
Merge pull request #31 from traP-jp/feat/#13-server-app
Browse files Browse the repository at this point in the history
第一部 - サーバーアプリケーションを作ってみよう
  • Loading branch information
kenken714 authored Oct 10, 2024
2 parents 522d1d9 + d5864cd commit 6089e85
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 65 deletions.
42 changes: 24 additions & 18 deletions docs/chapter1/section3/0_hello-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
**ターミナルの開き方**
Expand All @@ -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
```

以下のような画面が出れば起動できています。
Expand Down Expand Up @@ -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
Expand Down
Binary file modified docs/chapter1/section3/assets/hello_server.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter1/section3/assets/hello_server_detail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter1/section3/assets/hello_server_localhost.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter1/section3/assets/hello_server_me.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/chapter1/section3/assets/hello_server_success.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 0 additions & 21 deletions docs/chapter1/section3/src/1-1_hello-server.go

This file was deleted.

22 changes: 22 additions & 0 deletions docs/chapter1/section3/src/1-1_hello-server.rs
Original file line number Diff line number Diff line change
@@ -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.")
}
26 changes: 0 additions & 26 deletions docs/chapter1/section3/src/1-2_hello-server-me.go

This file was deleted.

31 changes: 31 additions & 0 deletions docs/chapter1/section3/src/1-2_hello-server-me.rs
Original file line number Diff line number Diff line change
@@ -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きらら作品(特に恋する小惑星、スロウスタート)が好きです。",
)
}

0 comments on commit 6089e85

Please sign in to comment.