index : license-api-rs.git

ascending towards madness

use tide::{http::mime, log, Response, Result, Server, StatusCode};

const BASE_URL: &str = "/api";

#[async_std::main]
async fn main() -> Result<()> {
    let mut app = tide::new();
    log::start();

    create_routes(&mut app);

    app.listen("127.0.0.1:8080").await?;
    Ok(())
}

fn create_routes(server: &mut Server<()>) {
    let mut base_route = server.at(BASE_URL);
    base_route.get(get_welcome_message);

    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);
}

fn build_response(status: StatusCode, data: String) -> Response {
    // this is a plain text only service
    let response = Response::builder(status)
        .body(format!("{}\n", data))
        .content_type(mime::PLAIN)
        .build();

    response
}

async fn get_welcome_message(req: tide::Request<()>) -> tide::Result<Response> {
    log::info!("{:?}", req);

    Ok(build_response(
        StatusCode::Ok,
        format!("{}", "Welcome message goes here").to_string(),
    ))
}

async fn get_license_list(req: tide::Request<()>) -> tide::Result<Response> {
    log::info!("{:?}", req);

    Ok(build_response(
        StatusCode::Ok,
        format!("{}", "License list goes here").to_string(),
    ))
}

async fn get_detailed_license_list(req: tide::Request<()>) -> tide::Result<Response> {
    log::info!("{:?}", req);

    Ok(build_response(
        StatusCode::Ok,
        format!("{}", "License list with full names goes here").to_string(),
    ))
}

async fn get_requested_license(req: tide::Request<()>) -> tide::Result<Response> {
    log::info!("{:?}", req);

    Ok(build_response(
        StatusCode::Ok,
        format!("{}", "Content of requested license goes here").to_string(),
    ))
}