Skip to content

Commit ee148a8

Browse files
committed
Fallback to 404 handler if path not found
1 parent da31229 commit ee148a8

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

aiscript-runtime/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use ast::HttpMethod;
22
use axum::Json;
3+
use axum::response::IntoResponse;
34
use axum::{response::Html, routing::*};
5+
use hyper::StatusCode;
46
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
57
use sqlx::postgres::PgPoolOptions;
68
use sqlx::sqlite::SqlitePoolOptions;
@@ -255,6 +257,18 @@ async fn run_server(
255257
}
256258
}
257259

260+
// Add a custom 404 handler for unmatched routes
261+
async fn handle_404() -> impl IntoResponse {
262+
let error_json = serde_json::json!({
263+
"message": "Not Found"
264+
});
265+
266+
(StatusCode::NOT_FOUND, Json(error_json))
267+
}
268+
269+
// Add the fallback handler to the router
270+
router = router.fallback(handle_404);
271+
258272
let addr = SocketAddr::from(([0, 0, 0, 0], port));
259273
let listener = TcpListener::bind(addr).await.unwrap();
260274

0 commit comments

Comments
 (0)