Skip to content

Replace iron with hyper + routerify #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ edition = "2018"
r2d2 = "^0.8.7"
redis = { version = "^0.21.1", features = ['r2d2'] }
log = "^0.4"
iron = "^0.6.1"
urlencoded = "^0.6"
router = "^0.6"
routerify = "3.0.0"
hyper = "^0.14"
serde = "^1.0"
serde_json = "^1.0"
spaceapi = "^0.8.1"
Expand All @@ -38,3 +37,4 @@ quick-error = "2.0"

[dev-dependencies]
env_logger = "^0.9.0"
tokio = { version = "*", features = ["full"] }
8 changes: 6 additions & 2 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use spaceapi_server::api;
use spaceapi_server::SpaceapiServerBuilder;

fn main() {
#[tokio::main]
async fn main() {
// Create new minimal Status instance compatible with v0.13 and v14
let status = api::StatusBuilder::mixed("coredump")
.logo("https://www.coredump.ch/logo.png")
Expand Down Expand Up @@ -29,5 +30,8 @@ fn main() {
.unwrap();

// Serve!
let _ = server.serve("127.0.0.1:8000");
let _ = server
.serve("127.0.0.1:8000")
.await
.expect("Could not start the server");
}
4 changes: 3 additions & 1 deletion examples/with_custom_redis_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ impl StatusModifier for OpenStatusFromRedisModifier {
}
}

fn main() {
#[tokio::main]
async fn main() {
env_logger::init();

let status = api::StatusBuilder::mixed("Mittelab")
Expand Down Expand Up @@ -81,5 +82,6 @@ fn main() {
// Serve!
server
.serve("127.0.0.1:8000")
.await
.expect("Could not start the server");
}
4 changes: 3 additions & 1 deletion examples/with_sensors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use spaceapi_server::api::sensors::{PeopleNowPresentSensorTemplate, TemperatureS
use spaceapi_server::modifiers::StateFromPeopleNowPresent;
use spaceapi_server::SpaceapiServerBuilder;

fn main() {
#[tokio::main]
async fn main() {
env_logger::init();

// Create new minimal Status instance compatible with v0.13 and v14
Expand Down Expand Up @@ -63,5 +64,6 @@ fn main() {
// Serve!
server
.serve("127.0.0.1:8000")
.await
.expect("Could not start the server");
}
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,14 @@

pub use spaceapi as api;

pub use iron::error::HttpResult;
pub use iron::Listening;
//pub use iron::error::HttpResult;
//pub use iron::Listening;

mod errors;
pub mod modifiers;
mod sensors;
mod server;
mod types;

pub use crate::errors::SpaceapiServerError;
pub use crate::server::SpaceapiServer;
pub use crate::server::SpaceapiServerBuilder;
Expand Down
144 changes: 57 additions & 87 deletions src/server/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
//! Handlers for the server.

use iron::modifiers::Header;
use iron::prelude::*;
use iron::{headers, middleware, status};
use hyper::header::{ACCESS_CONTROL_ALLOW_ORIGIN, CONTENT_TYPE};
use std::convert::Infallible;
// use iron::modifiers::Header;
// use iron::prelude::*;
// use iron::{headers, middleware, status};
use hyper::{Body, Request, Response, Server, StatusCode};
use log::{debug, error, info, warn};
use router::Router;
use routerify::prelude::*;
// use router::Router;
use serde::ser::{Serialize, SerializeMap, Serializer};

use crate::api;
use crate::modifiers;
//use crate::modifiers;
use crate::sensors;
use crate::server::SpaceapiServer;
use crate::types::RedisPool;

#[derive(Debug)]
Expand All @@ -29,96 +34,57 @@ impl Serialize for ErrorResponse {
}
}

pub(crate) struct ReadHandler {
status: api::Status,
redis_pool: RedisPool,
sensor_specs: sensors::SafeSensorSpecs,
status_modifiers: Vec<Box<dyn modifiers::StatusModifier>>,
}

impl ReadHandler {
pub(crate) fn new(
status: api::Status,
redis_pool: RedisPool,
sensor_specs: sensors::SafeSensorSpecs,
status_modifiers: Vec<Box<dyn modifiers::StatusModifier>>,
) -> ReadHandler {
ReadHandler {
status,
redis_pool,
sensor_specs,
status_modifiers,
}
}

fn build_response_json(&self) -> String {
// Create a mutable copy of the status struct
let mut status_copy = self.status.clone();

// Process registered sensors
for sensor_spec in self.sensor_specs.iter() {
match sensor_spec.get_sensor_value(&self.redis_pool) {
// Value could be read successfullly
Ok(value) => {
if status_copy.sensors.is_none() {
status_copy.sensors = Some(api::Sensors {
people_now_present: vec![],
temperature: vec![],
});
}
sensor_spec
.template
.to_sensor(&value, &mut status_copy.sensors.as_mut().unwrap());
pub async fn json_response_handler(req: Request<Body>) -> Result<Response<Body>, Infallible> {
// Create a mutable copy of the status struct
let state = req.data::<SpaceapiServer>().unwrap();
let mut status_copy = state.status.clone();

// Process registered sensors
for sensor_spec in state.sensor_specs.iter() {
match sensor_spec.get_sensor_value(&state.redis_pool) {
// Value could be read successfullly
Ok(value) => {
if status_copy.sensors.is_none() {
status_copy.sensors = Some(api::Sensors {
people_now_present: vec![],
temperature: vec![],
});
}
sensor_spec
.template
.to_sensor(&value, &mut status_copy.sensors.as_mut().unwrap());
}

// Value could not be read, do error logging
Err(err) => {
warn!(
"Could not retrieve key '{}' from Redis, omiting the sensor",
&sensor_spec.data_key
);
match err {
sensors::SensorError::Redis(e) => debug!("Error: {:?}", e),
sensors::SensorError::R2d2(e) => debug!("Error: {:?}", e),
sensors::SensorError::UnknownSensor(e) => warn!("Error: {:?}", e),
}
// Value could not be read, do error logging
Err(err) => {
warn!(
"Could not retrieve key '{}' from Redis, omiting the sensor",
&sensor_spec.data_key
);
match err {
sensors::SensorError::Redis(e) => debug!("Error: {:?}", e),
sensors::SensorError::R2d2(e) => debug!("Error: {:?}", e),
sensors::SensorError::UnknownSensor(e) => warn!("Error: {:?}", e),
}
}
}

for status_modifier in &self.status_modifiers {
status_modifier.modify(&mut status_copy);
}

// Serialize to JSON
serde_json::to_string(&status_copy).expect(
"Status object could not be serialized to JSON. \
Please open an issue at https://github.com/spaceapi-community/spaceapi-server-rs/issues",
)
}
}

impl middleware::Handler for ReadHandler {
/// Return the current status JSON.
fn handle(&self, req: &mut Request) -> IronResult<Response> {
info!("{} /{} from {}", req.method, req.url.path()[0], req.remote_addr);

// Get response body
let body = self.build_response_json();

// Create response
let response = Response::with((status::Ok, body))
// Set headers
.set(Header(headers::ContentType(
"application/json; charset=utf-8".parse().unwrap(),
)))
.set(Header(headers::CacheControl(vec![
headers::CacheDirective::NoCache,
])))
.set(Header(headers::AccessControlAllowOrigin::Any));

Ok(response)
for status_modifier in &state.status_modifiers {
status_modifier.modify(&mut status_copy);
}

// Serialize to JSON
let response = Response::builder()
.status(StatusCode::OK)
.header(CONTENT_TYPE, "application/json; charset=utf-8")
.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
.body(Body::from(serde_json::to_string(&status_copy).expect(
"Status object could not be serialized to JSON. \
Please open an issue at https://github.com/spaceapi-community/spaceapi-server-rs/issues",
)))
.unwrap();
Ok(response)
}

pub(crate) struct UpdateHandler {
Expand Down Expand Up @@ -147,6 +113,7 @@ impl UpdateHandler {
sensor_spec.set_sensor_value(&self.redis_pool, value)
}

/*
/// Build an OK response with the `HTTP 204 No Content` status code.
fn ok_response(&self) -> Response {
Response::with(status::NoContent)
Expand Down Expand Up @@ -176,8 +143,10 @@ impl UpdateHandler {
])))
.set(Header(headers::AccessControlAllowOrigin::Any))
}
*/
}

/*
impl middleware::Handler for UpdateHandler {
/// Update the sensor, return correct status code.
fn handle(&self, req: &mut Request) -> IronResult<Response> {
Expand Down Expand Up @@ -226,6 +195,7 @@ impl middleware::Handler for UpdateHandler {
Ok(self.ok_response())
}
}
*/

#[cfg(test)]
mod tests {
Expand Down
42 changes: 20 additions & 22 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
//! The SpaceAPI server struct.

use std::convert::Infallible;
use std::error::Error;
use std::net::ToSocketAddrs;
use std::sync::Arc;
use std::time::Duration;

use iron::Iron;
// use iron::Iron;
use hyper::{Body, Request, Response, Server};
use log::debug;
use redis::{ConnectionInfo, IntoConnectionInfo};
use router::Router;
use routerify::{Router, RouterService};

use serde_json::map::Map;
use serde_json::Value;
Expand Down Expand Up @@ -171,41 +174,36 @@ pub struct SpaceapiServer {

impl SpaceapiServer {
/// Create and return a Router instance.
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(
fn route(self) -> Router<Body, Infallible> {
Router::builder()
.data(self)
.get("/", handlers::json_response_handler)
.build()
.unwrap()
/*
.put(
"/sensors/:sensor/",
handlers::UpdateHandler::new(self.redis_pool.clone(), self.sensor_specs),
"sensors",
);

router
)
*/
}

/// Start a HTTP server listening on ``self.host:self.port``.
///
/// The call returns an `HttpResult<Listening>` object, see
/// http://ironframework.io/doc/hyper/server/struct.Listening.html
/// for more information.
pub fn serve<S: ToSocketAddrs>(self, socket_addr: S) -> crate::HttpResult<crate::Listening> {
pub async fn serve<S: ToSocketAddrs>(self, socket_addr: S) -> Result<(), Box<dyn Error>> {
// Launch server process
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)
let service = RouterService::new(router).unwrap();

let server = Server::bind(&socket_addr.to_socket_addrs().unwrap().next().unwrap()).serve(service);
Ok(server.await?)
}
}