index : license-api-rs.git

ascending towards madness

author holly sparkles <sparkles@holly.sh> 2023-10-04 11:03:44.0 +00:00:00
committer holly sparkles <sparkles@holly.sh> 2023-10-04 11:03:44.0 +00:00:00
commit
da15798c84bccbfc13fddd94c38b4ac69df5388c [patch]
tree
87860ea4278d2fb8d94bf338c7be7282f31a7230
parent
8cf93f45c1cc789ae55d7b46196bc3746ad23093
download
da15798c84bccbfc13fddd94c38b4ac69df5388c.tar.gz

fix: add fallback routes and errors when requesting invalid routes



Diff

 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<RunningConfig>) {
    // 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/<license>
	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<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(),
    )
}