Skip to content

Commit

Permalink
feat: 存在しないエンドポイントにアクセスされた場合のハンドラを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
rito528 committed Dec 14, 2023
1 parent 20fc994 commit 23d0692
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions server/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/entrypoint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
14 changes: 13 additions & 1 deletion server/entrypoint/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use std::net::SocketAddr;

use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::{
http::{
header::{AUTHORIZATION, CONTENT_TYPE, LOCATION},
Method,
},
middleware,
routing::{get, patch, post},
Router,
Json, Router,
};
use common::config::{ENV, HTTP};
use presentation::{
Expand All @@ -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;
Expand Down Expand Up @@ -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(),
Expand All @@ -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() {
Expand Down

0 comments on commit 23d0692

Please sign in to comment.