From 23d06920c7df3ec376f48204c6d0e74fd376d662 Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Thu, 14 Dec 2023 17:17:36 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AD=98=E5=9C=A8=E3=81=97=E3=81=AA?= =?UTF-8?q?=E3=81=84=E3=82=A8=E3=83=B3=E3=83=89=E3=83=9D=E3=82=A4=E3=83=B3?= =?UTF-8?q?=E3=83=88=E3=81=AB=E3=82=A2=E3=82=AF=E3=82=BB=E3=82=B9=E3=81=95?= =?UTF-8?q?=E3=82=8C=E3=81=9F=E5=A0=B4=E5=90=88=E3=81=AE=E3=83=8F=E3=83=B3?= =?UTF-8?q?=E3=83=89=E3=83=A9=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/Cargo.lock | 1 + server/entrypoint/Cargo.toml | 1 + server/entrypoint/src/main.rs | 14 +++++++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/server/Cargo.lock b/server/Cargo.lock index 2872d1fdd..db495b44b 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -938,6 +938,7 @@ dependencies = [ "presentation", "resource", "sentry", + "serde_json", "tokio", "tower", "tower-http", diff --git a/server/entrypoint/Cargo.toml b/server/entrypoint/Cargo.toml index 91e3e742c..2efa56f77 100644 --- a/server/entrypoint/Cargo.toml +++ b/server/entrypoint/Cargo.toml @@ -19,6 +19,7 @@ tower = "0.4.13" tower-http = { version = "0.5.0", features = ["cors"] } tracing = { workspace = true } tracing-subscriber = { version = "0.3.18", features = ["std", "registry", "env-filter"] } +serde_json = { workspace = true } [package.metadata.cargo-udeps.ignore] development = ["cargo-husky"] diff --git a/server/entrypoint/src/main.rs b/server/entrypoint/src/main.rs index 59f150eba..dadef422d 100644 --- a/server/entrypoint/src/main.rs +++ b/server/entrypoint/src/main.rs @@ -1,5 +1,7 @@ use std::net::SocketAddr; +use axum::http::StatusCode; +use axum::response::IntoResponse; use axum::{ http::{ header::{AUTHORIZATION, CONTENT_TYPE, LOCATION}, @@ -7,7 +9,7 @@ use axum::{ }, middleware, routing::{get, patch, post}, - Router, + Json, Router, }; use common::config::{ENV, HTTP}; use presentation::{ @@ -22,6 +24,7 @@ use presentation::{ }; use resource::{database::connection::ConnectionPool, repository::Repository}; use sentry::integrations::tower::{NewSentryLayer, SentryHttpLayer}; +use serde_json::json; use tokio::net::TcpListener; use tower_http::cors::{Any, CorsLayer}; use tracing::log; @@ -88,6 +91,7 @@ async fn main() -> anyhow::Result<()> { .route("/users/:uuid", patch(patch_user_role)) .with_state(shared_repository.to_owned()) .route("/health", get(health_check)) + .fallback(not_found_handler) .layer(layer) .route_layer(middleware::from_fn_with_state( shared_repository.to_owned(), @@ -111,6 +115,14 @@ async fn main() -> anyhow::Result<()> { Ok(()) } +async fn not_found_handler() -> impl IntoResponse { + ( + StatusCode::NOT_FOUND, + Json(json!({ "reason": "ACCESS TO UNKNOWN ENDPOINT." })), + ) + .into_response() +} + // NOTE: hyper::Serverが削除され、2023/12/03時点でgraceful_shutdownが実装できない // ref: https://github.com/hyperium/hyper/issues/2862 // async fn graceful_handler() {