diff --git a/src/error.rs b/src/error.rs index 1e1bbd4..4e8d70e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -6,6 +6,7 @@ pub enum TimeBannerError { ParseError(String), RenderError(String), RasterizeError(String), + NotFound, } #[derive(Serialize, Deserialize)] @@ -18,7 +19,8 @@ pub fn get_error_response(error: TimeBannerError) -> (StatusCode, Json (StatusCode::INTERNAL_SERVER_ERROR, format!("RenderError :: {}", msg)), TimeBannerError::ParseError(msg) => (StatusCode::BAD_REQUEST, format!("ParserError :: {}", msg)), - TimeBannerError::RasterizeError(msg) => (StatusCode::INTERNAL_SERVER_ERROR, format!("RasertizeError :: {}", msg)) + TimeBannerError::RasterizeError(msg) => (StatusCode::INTERNAL_SERVER_ERROR, format!("RasterizeError :: {}", msg)), + TimeBannerError::NotFound => { (StatusCode::NOT_FOUND, "Not Found".to_string()) } }; (code, Json(ErrorResponse { code: code.as_u16(), message })) diff --git a/src/main.rs b/src/main.rs index b26e5ca..fe74c3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,11 @@ use std::net::SocketAddr; use axum::{Router, routing::get}; +use axum::response::IntoResponse; use dotenvy::dotenv; use config::Configuration; -use crate::routes::{relative_handler, implicit_handler, absolute_handler, index_handler}; +use crate::error::{get_error_response, TimeBannerError}; +use crate::routes::{relative_handler, implicit_handler, absolute_handler, index_handler, fallback_handler}; mod config; mod raster; @@ -34,7 +36,8 @@ async fn main() { .route("/rel/:path", get(relative_handler)) .route("/relative/:path", get(relative_handler)) .route("/absolute/:path", get(absolute_handler)) - .route("/abs/:path", get(absolute_handler)); + .route("/abs/:path", get(absolute_handler)) + .fallback(fallback_handler); let addr = SocketAddr::from((config.socket_addr(), config.port)); axum::Server::bind(&addr) diff --git a/src/routes.rs b/src/routes.rs index 2805406..9b560df 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -43,6 +43,10 @@ pub async fn relative_handler(Path(path): Path) -> impl IntoResponse { let (raw_time, extension) = parse_path(path.as_str()); } +pub async fn fallback_handler() -> impl IntoResponse { + return get_error_response(TimeBannerError::NotFound).into_response(); +} + pub async fn absolute_handler(Path(path): Path) -> impl IntoResponse { let (raw_time, extension) = parse_path(path.as_str()); }