11//! Handlers for the server.
22
3+ use hyper:: header:: { ACCESS_CONTROL_ALLOW_ORIGIN , CONTENT_TYPE } ;
34use std:: convert:: Infallible ;
45// use iron::modifiers::Header;
56// use iron::prelude::*;
67// use iron::{headers, middleware, status};
7- use log:: { debug, error, info, warn} ;
88use hyper:: { Body , Request , Response , Server , StatusCode } ;
9+ use log:: { debug, error, info, warn} ;
910use routerify:: prelude:: * ;
1011// use router::Router;
1112use serde:: ser:: { Serialize , SerializeMap , Serializer } ;
1213
1314use crate :: api;
1415//use crate::modifiers;
1516use crate :: sensors;
16- use crate :: types:: RedisPool ;
1717use crate :: server:: SpaceapiServer ;
18+ use crate :: types:: RedisPool ;
1819
1920#[ derive( Debug ) ]
2021struct ErrorResponse {
@@ -59,7 +60,7 @@ pub async fn json_response_handler(req: Request<Body>) -> Result<Response<Body>,
5960 warn ! (
6061 "Could not retrieve key '{}' from Redis, omiting the sensor" ,
6162 & sensor_spec. data_key
62- ) ;
63+ ) ;
6364 match err {
6465 sensors:: SensorError :: Redis ( e) => debug ! ( "Error: {:?}" , e) ,
6566 sensors:: SensorError :: R2d2 ( e) => debug ! ( "Error: {:?}" , e) ,
@@ -74,36 +75,17 @@ pub async fn json_response_handler(req: Request<Body>) -> Result<Response<Body>,
7475 }
7576
7677 // Serialize to JSON
77- Ok ( Response :: new ( Body :: from ( serde_json:: to_string ( & status_copy) . expect (
78- "Status object could not be serialized to JSON. \
79- Please open an issue at https://github.com/spaceapi-community/spaceapi-server-rs/issues",
80- ) ) ) )
81- }
82-
83- /*
84- impl middleware::Handler for ReadHandler {
85- /// Return the current status JSON.
86- fn handle(&self, req: &mut Request) -> IronResult<Response> {
87- info!("{} /{} from {}", req.method, req.url.path()[0], req.remote_addr);
88-
89- // Get response body
90- let body = self.build_response_json();
91-
92- // Create response
93- let response = Response::with((status::Ok, body))
94- // Set headers
95- .set(Header(headers::ContentType(
96- "application/json; charset=utf-8".parse().unwrap(),
97- )))
98- .set(Header(headers::CacheControl(vec![
99- headers::CacheDirective::NoCache,
100- ])))
101- .set(Header(headers::AccessControlAllowOrigin::Any));
102-
103- Ok(response)
104- }
78+ let response = Response :: builder ( )
79+ . status ( StatusCode :: OK )
80+ . header ( CONTENT_TYPE , "application/json; charset=utf-8" )
81+ . header ( ACCESS_CONTROL_ALLOW_ORIGIN , "*" )
82+ . body ( Body :: from ( serde_json:: to_string ( & status_copy) . expect (
83+ "Status object could not be serialized to JSON. \
84+ Please open an issue at https://github.com/spaceapi-community/spaceapi-server-rs/issues",
85+ ) ) )
86+ . unwrap ( ) ;
87+ Ok ( response)
10588}
106- */
10789
10890pub ( crate ) struct UpdateHandler {
10991 redis_pool : RedisPool ,
0 commit comments