Skip to content

Instantly share code, notes, and snippets.

@appcypher
Last active October 24, 2021 08:57
Show Gist options
  • Save appcypher/ffaa886b43c17dec5146d736849b7d30 to your computer and use it in GitHub Desktop.
Save appcypher/ffaa886b43c17dec5146d736849b7d30 to your computer and use it in GitHub Desktop.
Tokio Sock Custom Impl API
//! Simple Low-level API to work with first.
//! Handlers have the type:
//! H: FnOnce(req: Request) -> impl Future<Output = Result<impl AsRef<[u8]>, impl ResponseError>>
//! Handlers are run in a separate task
let server = Server::new();
server.set(handlers::hello);
server.listen(([127, 0, 0, 1], 5050)).await?;
mod handlers {
#[any("/r/*")] // Procedural macro // Destructures the path at compile-time
async fn hello(req: &Request) -> Result<&'static str, HandlerError> {
// Print requests bytes.
println!(">>> request bytes =", req.as_bytes())
// Response.
Ok("Hello there!")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment