use {
amiya::{async_trait, Context, Middleware, Result},
std::time::Instant,
};
struct TimeMeasurer;
#[async_trait]
impl<Ex> Middleware<Ex> for TimeMeasurer
where
Ex: Send + Sync + 'static,
{
async fn handle(&self, mut ctx: Context<'_, Ex>) -> Result {
let start = Instant::now();
ctx.next().await?;
let measure = format!("req; dur={}us", start.elapsed().as_micros());
ctx.resp.append_header("server-timing", measure);
Ok(())
}
}
struct RequestHandler;
#[async_trait]
impl<Ex> Middleware<Ex> for RequestHandler
where
Ex: Send + Sync + 'static,
{
async fn handle(&self, mut ctx: Context<'_, Ex>) -> Result {
ctx.next().await?;
ctx.resp.set_body("Finish");
Ok(())
}
}
fn main() {
let app = amiya::new().uses(TimeMeasurer).uses(RequestHandler);
app.listen("[::]:8080").unwrap();
std::thread::park();
}