1use std::time::Duration;
2use std::path::Path;
3
4use iron;
5use iron::prelude::*;
6use iron::{Timeouts};
7use mount::Mount;
8use staticfile::Static;
9use iron_cors::CorsMiddleware;
10
11pub struct Route {
12 pub mount_path: String,
13 pub relative_path: String,
14}
15
16pub struct SimbolServer {
17 pub address: String,
18 pub port: u16,
19 pub path: String,
20 pub routes: Vec<Route>,
21}
22
23impl SimbolServer {
24 pub fn new(address: String, port: u16, path: String, routes: Vec<Route>) -> SimbolServer {
25 SimbolServer {
26 address: address,
27 port: port,
28 path: path,
29 routes: routes,
30 }
31 }
32
33 fn chain(&self) -> iron::Chain {
34 let mut asset_mount = Mount::new();
35
36 asset_mount.mount("/", Static::new(Path::new(&self.path)));
37 for route in &self.routes {
38 let path = format!("{}{}", &self.path, &route.relative_path);
39 let static_content = Static::new(Path::new(path.as_str()));
40 asset_mount.mount(&route.mount_path, static_content);
41 }
42
43 iron::Chain::new(asset_mount)
44 }
45
46 pub fn run_server(&self) -> iron::Listening {
47 let cors_middleware = CorsMiddleware::with_allow_any();
48 let mut chain = self.chain();
49 chain.link_after(::middleware::DefaultContentTypeMiddleware);
50 chain.link_after(::middleware::Custom404Middleware);
51 chain.link_around(cors_middleware);
52
53 let mut iron = Iron::new(chain);
54
55 iron.threads = 8;
56 iron.timeouts = Timeouts {
57 keep_alive: Some(Duration::from_secs(10)),
58 read: Some(Duration::from_secs(10)),
59 write: Some(Duration::from_secs(10))
60 };
61 iron.http(format!("{}:{}", self.address, self.port)).unwrap()
62 }
63}