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
use futures::Future;
use chashmap::CHashMap;
use plugins::{Plugin, PluginData};
pub struct PageVisits {
counts: CHashMap<String, u32>,
}
impl PageVisits
{
pub fn new() -> PageVisits {
PageVisits {
counts: CHashMap::new(),
}
}
pub fn get(&self, url_path: &str) -> Option<u32>
{
let url_path = url_path.to_owned();
self.counts.get(&url_path).map(|guard| *guard)
}
}
impl<S,E> Plugin<S,E> for PageVisits
where S: 'static, E: 'static
{
fn handle(&self, data: PluginData<S>)
-> Box<Future<Item = PluginData<S>, Error = E>>
{
let url_path: String = data.request.path().to_owned();
if let Some(mut count) = self.counts.get_mut(&url_path) {
*count += 1;
} else {
self.counts.insert(url_path, 1);
}
Box::new(::futures::future::ok(data))
}
}