Add fallback route handler

This commit is contained in:
2023-07-22 18:20:29 -05:00
parent 4e0e6f1d83
commit 3a6c4172dc
3 changed files with 12 additions and 3 deletions

View File

@@ -6,6 +6,7 @@ pub enum TimeBannerError {
ParseError(String), ParseError(String),
RenderError(String), RenderError(String),
RasterizeError(String), RasterizeError(String),
NotFound,
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
@@ -18,7 +19,8 @@ pub fn get_error_response(error: TimeBannerError) -> (StatusCode, Json<ErrorResp
let (code, message) = match error { let (code, message) = match error {
TimeBannerError::RenderError(msg) => (StatusCode::INTERNAL_SERVER_ERROR, format!("RenderError :: {}", msg)), TimeBannerError::RenderError(msg) => (StatusCode::INTERNAL_SERVER_ERROR, format!("RenderError :: {}", msg)),
TimeBannerError::ParseError(msg) => (StatusCode::BAD_REQUEST, format!("ParserError :: {}", 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 })) (code, Json(ErrorResponse { code: code.as_u16(), message }))

View File

@@ -1,9 +1,11 @@
use std::net::SocketAddr; use std::net::SocketAddr;
use axum::{Router, routing::get}; use axum::{Router, routing::get};
use axum::response::IntoResponse;
use dotenvy::dotenv; use dotenvy::dotenv;
use config::Configuration; 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 config;
mod raster; mod raster;
@@ -34,7 +36,8 @@ async fn main() {
.route("/rel/:path", get(relative_handler)) .route("/rel/:path", get(relative_handler))
.route("/relative/:path", get(relative_handler)) .route("/relative/:path", get(relative_handler))
.route("/absolute/:path", get(absolute_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)); let addr = SocketAddr::from((config.socket_addr(), config.port));
axum::Server::bind(&addr) axum::Server::bind(&addr)

View File

@@ -43,6 +43,10 @@ pub async fn relative_handler(Path(path): Path<String>) -> impl IntoResponse {
let (raw_time, extension) = parse_path(path.as_str()); 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<String>) -> impl IntoResponse { pub async fn absolute_handler(Path(path): Path<String>) -> impl IntoResponse {
let (raw_time, extension) = parse_path(path.as_str()); let (raw_time, extension) = parse_path(path.as_str());
} }