A simple request router
Designed to be wasm
compatible, simple and small.
use mimeograph_router::*;
pub struct Request {
path: String,
method: String
}
impl mimeograph_request::Request for Request {
fn path(&self) -> Cow<'_, str> {
Cow::Borrowed(&self.path)
}
fn method(&self) -> Cow<'_, str> {
Cow::Borrowed(&self.method)
}
}
pub struct Response {
status: u32,
body: String
}
#[get("/hello")]
fn get_hello(request: Request) -> Result<Response> {
Ok(Response { status: 200, body: String::from("world") })
}
#[get("/greet/:name")]
fn get_greeting(request: Request, name: &str) -> Result<Response> {
Ok(Response { status: 200, body: format!("Hello, {}!", name) })
}
#[get("/foo/*/baz/:name")]
fn foo_baz_name(request: Request, name: &str) -> Result<Response> {
Ok(Response { status: 200, body: format!("Hello, {}!", name) })
}
#[get("/page/**")]
fn page_glob(request: Request, name) -> Result<Response> {
Ok(Response { status: 200, body: String::from("Hello, World") })
}
#[post("/images")]
fn post_image(request: Request) -> Result<Response> {
unimplemented!()
}
pub fn handle_request(request: Request) -> Response {
let router = router![get_hello, get_greeting, foo_baz_name, page_glob];
match router(request) {
Ok(Ok(resp)) => resp,
Ok(Err(_err)) => {
todo!()
}
Err(request) => {
todo!()
}
}
}