Revisit low level parallel processing of incoming sockets
Full Example
use anyhow::Result;use webserver_from_scratch::{middleware, router::Router,HTTPServer,LogLevel,StatusCode};fnmain() -> Result<()>{letmut server = HTTPServer::new();
server.loglevel(LogLevel::Off);let hello_world_handler = middleware!(|ctx| {let resp = b"<h1>Hello World</h1>";
ctx.response.content_type("text/html");
ctx.response.write(resp);
ctx.end();
});let hello_handler = middleware!(|ctx| {
ctx.response.content_type("text/html");
ctx.response.write(b"<h1>Hello ");
let params = ctx.params.clone();
let name = params.get(":name");
let name = ifletSome(name) = name {
name.value.as_bytes()} else {b"World"};
ctx.response.write(name);
ctx.response.write(b"</h1>");
ctx.end();
});
server
.get("/", hello_world_handler).get("/:name", hello_handler);
server.any("*",middleware!(|ctx| {let resp = b"404";
ctx.response.status_code(StatusCode::NotFound);
ctx.response.write(resp);
}),);
server.listen_blocking("[::1]:8080".parse().unwrap())}
Macro
Usage
middleware!(|ctx| {// User Code (can use .await)})
Resulting Code
{let closure = |ctx:MiddlewareCtx| -> HandlerFut{Box::pin(asyncmove{// We have to use a mutex for the request context since the// borrow checker doesn't recognize that an async function has finished running andletmut ctx = ctx.lock();// will be locked after `ctx` is dropped at the end of this block/// {User code}Ok(())// enable error catching by returning a result (enables using `?` for catching errors)})};
closure // return closure from this block}