From 520c8fef1269fd412e8bd280d5a2db823c2bd256 Mon Sep 17 00:00:00 2001 From: kenken714 Date: Mon, 11 Nov 2024 01:12:12 +0900 Subject: [PATCH] feat: add ping handler --- docs/chapter2/section2/2_fetch.md | 4 ++-- docs/chapter2/section2/src/2/handler.rs | 27 +++++++++++++++++++++++++ docs/chapter2/section2/src/2/main.go | 12 ----------- 3 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 docs/chapter2/section2/src/2/handler.rs delete mode 100644 docs/chapter2/section2/src/2/main.go diff --git a/docs/chapter2/section2/2_fetch.md b/docs/chapter2/section2/2_fetch.md index 956d08b3..f9de4750 100644 --- a/docs/chapter2/section2/2_fetch.md +++ b/docs/chapter2/section2/2_fetch.md @@ -3,9 +3,9 @@ ## pingページの作成 接続の練習のためサーバーに`/ping`エンドポイントを実装しておきましょう。 -サーバーのリポジトリの`main.go`の`main`関数を書き換えます。 +サーバーのリポジトリの`handler.rs`を書き換えます。 -<<<@/chapter2/section2/src/2/main.go{go:line-numbers} +<<<@/chapter2/section2/src/2/handler.rs{rs:line-numbers} 次にフロントエンドから`/ping`エンドポイントにアクセスしてみるコードを書いてみましょう。 diff --git a/docs/chapter2/section2/src/2/handler.rs b/docs/chapter2/section2/src/2/handler.rs new file mode 100644 index 00000000..b29c68b6 --- /dev/null +++ b/docs/chapter2/section2/src/2/handler.rs @@ -0,0 +1,27 @@ +use axum::{ + routing::{get, post}, + Router, +}; + +use crate::repository::Repository; + +mod auth; +mod country; + +pub fn make_router(app_state: Repository) -> Router { + let city_router = Router::new() + .route("/cities/:city_name", get(country::get_city_handler)) + .route("/cities", post(country::post_city_handler)); + + let auth_router = Router::new() + .route("/signup", post(auth::sign_up)) + .route("/login", post(auth::login)); + + let ping_router = Router::new().route("/ping", get(|| async { "pong" })); // [!code ++] + + Router::new() + .nest("/", city_router) + .nest("/", auth_router) + .nest("/", ping_router) // [!code ++] + .with_state(app_state) +} diff --git a/docs/chapter2/section2/src/2/main.go b/docs/chapter2/section2/src/2/main.go deleted file mode 100644 index 4f2e12c0..00000000 --- a/docs/chapter2/section2/src/2/main.go +++ /dev/null @@ -1,12 +0,0 @@ -e := echo.New() -e.Use(middleware.Logger()) -e.Use(session.Middleware(store)) - -e.POST("/login", loginHandler) -e.POST("/signup", signUpHandler) -e.GET("/ping", func (c echo.Context) error { return c.String(http.StatusOK,"pong")})//[!code ++] - -withAuth := e.Group("") -withAuth.Use(userAuthMiddleware) -withAuth.GET("/cities/:cityName", getCityInfoHandler) -withAuth.POST("/cities", postCityHandler) \ No newline at end of file