forked from traPtitech/naro-text
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
第一部 - サーバー演習問題
- Loading branch information
Showing
8 changed files
with
171 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ dist-ssr | |
*.sw? | ||
|
||
.textlintcache | ||
/.vitepress/cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use axum::{routing::get, Router}; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// 「/ping」というエンドポイントを設定する | ||
let app = Router::new().route("/ping", 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(); | ||
} | ||
|
||
// 文字列「pong」をクライアントに返す | ||
async fn handler() -> String { | ||
String::from("pong") | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use axum::{extract::Query, http::StatusCode, routing::get, Router}; | ||
#[tokio::main] | ||
async fn main() { | ||
// 「/ping」というエンドポイントを設定する | ||
let app = Router::new().route("/fizzbuzz", get(fizzbuzz_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(); | ||
} | ||
|
||
// クエリパラメータを受け取るための構造体を定義 | ||
#[derive(serde::Deserialize)] | ||
struct FizzBuzzQuery { | ||
count: Option<String>, | ||
} | ||
|
||
async fn fizzbuzz_handler(Query(query): Query<FizzBuzzQuery>) -> (StatusCode, String) { | ||
// クエリパラメータが指定されていない場合はデフォルト値を使用する | ||
let mut n: i32 = 30; | ||
// クエリパラメータが指定されている場合はその値を調べる | ||
if let Some(count) = query.count { | ||
let count = count.parse(); | ||
match count { | ||
// 数値に変換できた場合はその値を使用する | ||
Ok(count) => n = count, | ||
// ステータスコード 400 Bad Request を返す | ||
Err(_) => return (StatusCode::BAD_REQUEST, String::from("Bad Request\n")), | ||
} | ||
} | ||
|
||
// FizzBuzzの処理をする | ||
let fizzbuzz_str = fizzbuzz(n); | ||
|
||
// ステータスコード 200 Ok とfizzBuzzの結果を返す | ||
(StatusCode::OK, fizzbuzz_str + "\n") | ||
} | ||
|
||
// fizzBuzzの処理 | ||
fn fizzbuzz(n: i32) -> String { | ||
let mut result = String::new(); | ||
for i in 1..=n { | ||
if i % 15 == 0 { | ||
result.push_str("FizzBuzz\n"); | ||
} else if i % 3 == 0 { | ||
result.push_str("Fizz\n"); | ||
} else if i % 5 == 0 { | ||
result.push_str("Buzz\n"); | ||
} else { | ||
result.push_str(&i.to_string()); | ||
result.push('\n'); | ||
} | ||
} | ||
result | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.