Middlewares

This article isn't finished. Read Middleware CONCEPT to learn more about middlewares.

Teo supports custom first-in-last-out middlewares.

declare middleware timing
 
middlewares [timing]

Implement the middleware in code:

app.main_namespace().define_middleware("timing", |arguments: Arguments| {
    Ok(middleware_wrap_fn(move |ctx: Ctx, next: &'static dyn Next| async move {
        let start = SystemTime::now();
        let res = next.call(ctx).await?;
        let end = SystemTime::now();
        let duration = end.duration_since(start).unwrap();
        println!("request duration: {:?}", duration);
        return Ok(res);
    }))
});