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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#![doc(html_root_url = "https://docs.rs/pemmican")]
extern crate hyper;
extern crate futures;
extern crate futures_cpupool;
extern crate chashmap;
#[macro_use]
extern crate log;
pub mod error;
pub use error::Error;
pub mod router;
pub use router::{Router, DynamicRouter};
pub mod plugins;
pub use plugins::{Plugin, PluginData};
use futures::Future;
use futures::future;
use futures_cpupool::CpuPool;
use hyper::server::{Http, Request, Response, Service};
use hyper::StatusCode;
use std::time::Duration;
use std::sync::Arc;
use std::error::Error as StdError;
pub type Handler<S, E> =
fn(pemmican: &Pemmican<S, E>, &Request)
-> Box<Future<Item = Response, Error = E>>;
pub struct Config {
pub num_threads: usize,
pub shutdown_timeout: Duration,
pub keep_alive: bool,
}
impl Default for Config {
fn default() -> Config {
Config {
num_threads: 4,
shutdown_timeout: Duration::from_secs(1),
keep_alive: true,
}
}
}
pub struct Shared<S: Send + Sync>
{
pub pool: CpuPool,
#[allow(dead_code)]
pub state: S,
}
pub struct Pemmican<S: Send + Sync, E>
{
router: Box<Router<S, E>>,
pub config: Config,
plugins: Vec<Arc<Plugin<S, E>>>,
pub shared: Arc<Shared<S>>,
}
impl<S, E> Pemmican<S, E>
where S: Send + Sync + 'static,
E: Send + Sync + StdError + 'static
{
pub fn new(config: Config, router: Box<Router<S, E>>, initial_state: S)
-> Pemmican<S, E>
{
let num_threads = config.num_threads;
Pemmican {
router: router,
config: config,
plugins: Vec::new(),
shared: Arc::new(Shared {
pool: CpuPool::new(num_threads),
state: initial_state,
})
}
}
pub fn plug_in(&mut self, plugin: Arc<Plugin<S, E>>)
{
self.plugins.push(plugin);
}
pub fn run<F>(self, addr: &str, shutdown_signal: F) -> Result<(), Error>
where F: Future<Item = (), Error = ()>
{
let keep_alive = self.config.keep_alive;
let shutdown_timeout = self.config.shutdown_timeout;
let arcself = Arc::new(self);
let addr = addr.parse()?;
let mut server = Http::new()
.keep_alive(keep_alive)
.bind(&addr, move|| Ok(arcself.clone()))?;
server.shutdown_timeout(shutdown_timeout);
server.run_until(shutdown_signal).map_err(From::from)
}
}
impl<S, E> Service for Pemmican<S, E>
where S: Send + Sync + 'static,
E: Send + Sync + StdError + 'static
{
type Request = Request;
type Response = Response;
type Error = ::hyper::Error;
type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;
fn call(&self, mut req: Request) -> Self::Future {
if let Some(handler) = self.router.get_handler(req.path(), req.method()) {
for m in &self.plugins {
m.before_handler(&mut req);
}
let shared = self.shared.clone();
let mut fut: Box<Future<Item = PluginData<S>, Error = E>> =
Box::new(
(handler)(self, &req)
.map(move |response| {
PluginData {
shared: shared,
request: req,
response: response,
}
})
);
for m in &self.plugins {
fut = Box::new( m.after_handler(fut) );
}
let fut = Box::new( fut.map(|plugin_data| plugin_data.response) );
Box::new( fut.or_else(|e| {
error!("error: {}", e);
future::ok(Response::new().with_status(StatusCode::InternalServerError))
}))
} else {
Box::new(future::ok(
Response::new().with_status(StatusCode::NotFound)
))
}
}
}