From da15798c84bccbfc13fddd94c38b4ac69df5388c Mon Sep 17 00:00:00 2001 From: holly sparkles Date: Wed, 4 Oct 2023 13:03:44 +0200 Subject: [PATCH] fix: add fallback routes and errors when requesting invalid routes --- src/main.rs | 52 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index 71991d8..0ba63ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,8 +13,10 @@ use crate::{ mod config; mod data; +const ERROR_NO_LICENSES_FOUND: &str = + "No licenses found."; const ERROR_LICENSE_NOT_FOUND: &str = - " is undefined. use the 'list' endpoint to see defined licenses."; + "Invalid license. use the 'list' or 'list/all' endpoints to see defined licenses."; #[async_std::main] async fn main() -> Result<()> { @@ -45,16 +47,28 @@ fn create_routes(server: &mut Server) { // TODO: figure out how to not need clone let url = server.clone().state().config.base_url.clone(); let mut base_route = server.at(&url); - base_route.get(get_welcome_message); + // BASE_URL/ + base_route.at(":license").get(get_requested_license); + + /* + BASE_URL/list + BASE_URL/full + BASE_URL/list/ + BASE_URL/full/ + */ base_route.at(":license").get(get_requested_license); base_route.at("list").get(get_license_list); base_route.at("list/full").get(get_detailed_license_list); // nyaaaaa whyyyy https://github.com/http-rs/tide/issues/205 - base_route.at("").get(get_welcome_message); - base_route.at("list/").get(get_license_list); - base_route.at("list/full/").get(get_detailed_license_list); + let mut fallback_routes = base_route.at(""); + fallback_routes.get(get_welcome_message); + fallback_routes.at("list/").get(get_license_list); + fallback_routes.at("list/full/").get(get_detailed_license_list); + + fallback_routes.at("list/*").get(get_fallback_response); + fallback_routes.at("list/full/*").get(get_fallback_response); } fn build_response(status: StatusCode, data: String) -> Response { @@ -76,6 +90,12 @@ async fn get_welcome_message(req: tide::Request) -> tide::Result< )) } +async fn get_fallback_response(req: tide::Request) -> tide::Result { + log::error!("{:?}", req); + + Ok(error_response(req.url().path())) +} + async fn get_license_list(req: tide::Request) -> tide::Result { log::info!("{:?}", req); @@ -84,7 +104,7 @@ async fn get_license_list(req: tide::Request) -> tide::Result Ok(build_response(StatusCode::Ok, format!("{}", licenses))), None => Ok(build_response( StatusCode::NotFound, - String::from("License not found"), + String::from(ERROR_NO_LICENSES_FOUND), )), } } @@ -96,7 +116,7 @@ async fn get_detailed_license_list(req: tide::Request) -> tide::R Some(licenses) => Ok(build_response(StatusCode::Ok, format!("{}", licenses))), None => Ok(build_response( StatusCode::NotFound, - String::from("No licenses found."), + String::from(ERROR_NO_LICENSES_FOUND), )), } } @@ -127,22 +147,22 @@ async fn get_requested_license(req: tide::Request) -> tide::Resul license_file_path.to_string_lossy().to_string() ); - build_response( - StatusCode::NotFound, - format!("ERROR - `{}`{}", request, ERROR_LICENSE_NOT_FOUND).to_string(), - ) + error_response(request) } } } None => { log::error!("License not found: `{}`", request); - - build_response( - StatusCode::NotFound, - format!("ERROR - `{}`{}", request, ERROR_LICENSE_NOT_FOUND).to_string(), - ) + error_response(request) } }; Ok(response) } + +fn error_response(_req: &str) -> Response { + build_response( + StatusCode::NotFound, + format!("{}", ERROR_LICENSE_NOT_FOUND).to_string(), + ) +} -- libgit2 1.7.2