1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use std::sync::Arc;

use viz_core::{http, Context, DynMiddleware, Guard, Middleware, Response, Result};
use viz_utils::futures::future::BoxFuture;

pub struct RouteHandler {
    guard: Arc<Box<dyn Guard>>,
    handler: Arc<DynMiddleware>,
}

impl RouteHandler {
    pub fn new(guard: Arc<Box<dyn Guard>>, handler: Arc<DynMiddleware>) -> Self {
        Self { guard, handler }
    }
}

impl<'a> Middleware<'a, Context> for RouteHandler {
    type Output = Result<Response>;

    #[inline]
    fn call(&'a self, cx: &'a mut Context) -> BoxFuture<'a, Self::Output> {
        if self.guard.check(cx) {
            return self.handler.call(cx);
        }

        Box::pin(async { Ok(http::StatusCode::NOT_FOUND.into()) })
    }
}