fix: add fallback routes and errors when requesting invalid routes
Diff
src/main.rs | 52 ++++++++++++++++++++++++++++++++++++----------------
1 file changed, 36 insertions(+), 16 deletions(-)
@@ -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<RunningConfig>) {
let url = server.clone().state().config.base_url.clone();
let mut base_route = server.at(&url);
base_route.get(get_welcome_message);
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);
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<RunningConfig>) -> tide::Result<
))
}
async fn get_fallback_response(req: tide::Request<RunningConfig>) -> tide::Result<Response> {
log::error!("{:?}", req);
Ok(error_response(req.url().path()))
}
async fn get_license_list(req: tide::Request<RunningConfig>) -> tide::Result<Response> {
log::info!("{:?}", req);
@@ -84,7 +104,7 @@ async fn get_license_list(req: tide::Request<RunningConfig>) -> tide::Result<Res
Some(licenses) => 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<RunningConfig>) -> 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<RunningConfig>) -> 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(),
)
}