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.
- Loading branch information
Showing
5 changed files
with
51 additions
and
49 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
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,45 @@ | ||
use core::fmt; | ||
|
||
use axum::{ | ||
extract::rejection::JsonRejection, | ||
http::StatusCode, | ||
routing::{get, post}, | ||
Json, Router, | ||
}; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// post() メソッドを使ってPOSTリクエストを処理する | ||
let app = Router::new() | ||
.route("/post", post(post_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(); | ||
} | ||
|
||
// JSONを受け取るための構造体を定義 | ||
// 構造体を JSON に変換するためにserde::Serializeを導出する | ||
#[derive(serde::Deserialize, serde::Serialize)] | ||
struct JsonData { | ||
number: i32, | ||
string: String, | ||
bool: bool, | ||
} | ||
|
||
async fn post_handler( | ||
query: Result<Json<JsonData>, JsonRejection>, | ||
) -> Result<Json<JsonData>, (StatusCode, JsonRejection)> { | ||
match query { | ||
// 正常なときリクエストデータをそのまま返す | ||
Ok(data) => Ok(data), | ||
// 正常でないときステータスコード 400 Bad Requestを返す | ||
Err(rejection) => Err((StatusCode::BAD_REQUEST, rejection)), | ||
} | ||
} |
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