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
use std::net::ToSocketAddrs;
use std::sync::Arc;
use std::time::Duration;
use r2d2;
use r2d2_redis::RedisConnectionManager;
use iron::Iron;
use router::Router;
use redis::{IntoConnectionInfo, ConnectionInfo};
use serde_json::Value;
use serde_json::map::Map;
mod handlers;
use ::api;
use ::sensors;
use ::modifiers;
use ::errors::SpaceapiServerError;
use ::types::RedisPool;
pub struct SpaceapiServerBuilder {
status: api::Status,
redis_connection_info: Result<ConnectionInfo, SpaceapiServerError>,
sensor_specs: Vec<sensors::SensorSpec>,
status_modifiers: Vec<Box<modifiers::StatusModifier>>,
}
impl SpaceapiServerBuilder {
pub fn new(mut status: api::Status) -> SpaceapiServerBuilder {
let mut versions = Map::new();
versions.insert("spaceapi-rs".into(), api::get_version().into());
versions.insert("spaceapi-server-rs".into(), ::get_version().into());
status.extensions.insert("versions".into(), Value::Object(versions));
SpaceapiServerBuilder {
status: status,
redis_connection_info: Err("redis_connection_info missing".into()),
sensor_specs: vec![],
status_modifiers: vec![],
}
}
pub fn redis_connection_info<R: IntoConnectionInfo>(mut self, redis_connection_info: R) -> Self {
self.redis_connection_info = redis_connection_info.into_connection_info().map_err(|e| e.into());
self
}
pub fn add_status_modifier<M: modifiers::StatusModifier + 'static>(mut self, modifier: M) -> Self {
self.status_modifiers.push(Box::new(modifier));
self
}
pub fn add_sensor<T: api::SensorTemplate + 'static>(mut self, template: T, data_key: String) -> Self {
self.sensor_specs.push(
sensors::SensorSpec { template: Box::new(template), data_key: data_key}
);
self
}
pub fn build(self) -> Result<SpaceapiServer, SpaceapiServerError> {
debug!("Redis connection info: {:?}", &self.redis_connection_info);
let redis_config = r2d2::Config::builder()
.pool_size(6)
.min_idle(Some(2))
.initialization_fail_fast(false)
.connection_timeout(Duration::from_secs(1))
.error_handler(Box::new(r2d2::NopErrorHandler))
.build();
let redis_manager = try!(RedisConnectionManager::new(self.redis_connection_info?));
let pool = try!(r2d2::Pool::new(redis_config, redis_manager));
Ok(SpaceapiServer {
status: self.status,
redis_pool: pool,
sensor_specs: Arc::new(self.sensor_specs),
status_modifiers: self.status_modifiers,
})
}
}
pub struct SpaceapiServer {
status: api::Status,
redis_pool: RedisPool,
sensor_specs: sensors::SafeSensorSpecs,
status_modifiers: Vec<Box<modifiers::StatusModifier>>,
}
impl SpaceapiServer {
fn route(self) -> Router {
let mut router = Router::new();
router.get("/", handlers::ReadHandler::new(
self.status.clone(), self.redis_pool.clone(),
self.sensor_specs.clone(), self.status_modifiers), "root");
router.put("/sensors/:sensor/", handlers::UpdateHandler::new(
self.redis_pool.clone(), self.sensor_specs.clone()), "sensors");
router
}
pub fn serve<S: ToSocketAddrs>(self, socket_addr: S) -> ::HttpResult<::Listening> {
let router = self.route();
println!("Starting HTTP server on:");
for a in socket_addr.to_socket_addrs()? {
println!("\thttp://{}", a);
}
Iron::new(router).http(socket_addr)
}
}