2个不稳定版本

0.2.0 2020年4月12日
0.1.0 2020年2月22日

#1201 in HTTP服务器

MIT许可协议

42KB
1K SLoC

brio

玩具异步HTTP服务器和Web框架

MIT licensed

#![feature(async_closure)]

use brio::{App, Ctx, Request, Response, Status};
use log::info;
use std::{future::Future, pin::Pin, time::Instant};

type BoxFuture<'a, Response> = Pin<Box<dyn Future<Output = Response> + Send + 'static>>;

async fn foo(_: Request) -> Response {
    Response::with_status(Status::Ok)
}

fn main() {
    std::env::set_var("RUST_LOG", "brio=info,server=debug");
    env_logger::init();
    let mut app = App::new();
    app.get("/foo", foo);
    app.get("/bar", async move |_req: Request| {
        Response::with_status(Status::Ok)
    });
    app.middleware("*", logger);
    app.files("/public/", "./examples/static/");
    app.run(8000).unwrap();
}

fn logger(ctx: Ctx) -> BoxFuture<Response> {
    let now = Instant::now();
    let path = ctx.req().route().path().clone();
    let method = ctx.req().route().method().unwrap().clone();
    let fut = ctx.next();
    Box::pin(async move {
        let res = fut.await;
        info!(
            "request {} {} took {:?} ({})",
            method,
            path,
            Instant::now().duration_since(now),
            res.status() as u32
        );
        res
    })
}

依赖项

~6–15MB
~205K SLoC