Last active
October 24, 2021 08:57
-
-
Save appcypher/ffaa886b43c17dec5146d736849b7d30 to your computer and use it in GitHub Desktop.
Tokio Sock Custom Impl API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! 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