feat: add api endpoints using tide
Diff
Cargo.toml | 2 ++-
src/main.rs | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 75 insertions(+), 2 deletions(-)
@@ -6,3 +6,5 @@ edition = "2021"
[dependencies]
async-std = { version = "1.12.0", features = ["attributes"] }
tide = "0.16.0"
@@ -1,3 +1,74 @@
fn main() {
println!("Hello, world!");
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);
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 {
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(),
))
}