Designed to be wasm
compatible, simple and small.
```rust use mimeograph_router::*;
/// A request could be an http
crate Request, or a CloudFlare
/// worker Request. Here is a very simple request:
pub struct Request {
path: String,
method: String
}
/// To use the router, implement mimeograph_request::Request
/// for the Request type that you are working with.
impl mimeographrequest::Request for Request {
fn path(&self) -> Cow<', str> {
Cow::Borrowed(&self.path)
}
fn method(&self) -> Cow<'_, str> {
Cow::Borrowed(&self.method)
}
}
/// A dummy Response type pub struct Response { status: u32, body: String }
fn get_hello(request: &Request) -> Result
fn get_greeting(request: &Request, name: &str) -> Result
/// All verbs are supported: /// get, put, post, delete, head, patch, options
fn post_image(request: &Request) -> Result
/// The entrypoint for handling a request pub fn handlerequest(request: Request) -> Response { // router![] creates a closure that iterates through // the handlers until one is matched, then it returns // the matched handler's response let router = router![gethello, get_greeting];
match router(&request) {
// Matched a route
Some(Ok(resp)) => resp,
// There was an error
Some(Err(_err)) => {
// convert err into an http::Response;
todo!()
}
// No routes matched
None => {
// provide a 404, redirect, or similar response
todo!()
}
}
}
```