mini-serve 0.13.12

An HTTP server: trie router, middleware, CORS, optional TLS. Built on hyper + tokio.
Documentation
use mini_serve::{handler, json, path_params, RouteBuilder};
use hyper::StatusCode;
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;

#[derive(Serialize)]
struct HealthResponse {
    status: String,
}

#[derive(Serialize)]
struct HelloResponse {
    message: String,
}

#[derive(Deserialize)]
struct NameParam {
    name: String,
}

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let port = std::env::var("PORT")
        .unwrap_or_else(|_| "8080".to_string())
        .parse::<u16>()?;
    let addr = SocketAddr::from(([0, 0, 0, 0], port));

    // Use RouteBuilder to register all routes before sealing the app.
    let app = RouteBuilder::stateless()
        // Health endpoint for Docker HEALTHCHECK
        .get("/health", handler(|_req, _state| async {
            json(StatusCode::OK, &HealthResponse {
                status: "ok".to_string(),
            })
        }))
        // Greeter endpoint demonstrating path params and typed extraction
        .get("/hello/:name", handler(|req, _state| async move {
            let params: NameParam = path_params(&req)?;
            json(StatusCode::OK, &HelloResponse {
                message: format!("hello, {}", params.name),
            })
        }))
        .seal();

    println!("listening on http://{}", addr);
    app.bind(addr).await?;
    Ok(())
}