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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

use super::Shared;
use std::sync::Arc;
use futures::Future;
use hyper::server::{Request, Response};


pub struct PluginData<S: Send + Sync>
{
    pub shared: Arc<Shared<S>>,
    pub request: Request,
    pub response: Response,
}


/// A plugable component, run on every request (for which a valid route exists)
pub trait Plugin<S: Send + Sync + 'static, E: 'static>: Send + Sync
{
    /// This runs before the handler, and potentially modifies the request
    fn before_handler(&self, request: &mut Request);

    /// This runs after the handler. This must return a future that uses the passed in
    /// future, returning the modified future, (e.g. perhaps using and_then)
    /// This potentially modifies the Response (available through the `data` parameter).
    fn after_handler(&self,
                     future: Box<Future<Item = PluginData<S>, Error = E>>)
                     -> Box<Future<Item = PluginData<S>, Error = E>>;
}

impl<'a, S: Send + Sync + 'static, E: 'static, T: Send + Sync + Plugin<S,E>>
    Plugin<S, E> for &'a T
{
    fn before_handler(&self, request: &mut Request)
    {
        (*self).before_handler(request)
    }

    fn after_handler(&self,
                     future: Box<Future<Item = PluginData<S>, Error = E>>)
                     -> Box<Future<Item = PluginData<S>, Error = E>>
    {
        (*self).after_handler(future)
    }
}

pub mod page_visits;
pub use self::page_visits::PageVisits;