1
1
//! Handlers for the server.
2
2
3
+ use hyper:: header:: { ACCESS_CONTROL_ALLOW_ORIGIN , CONTENT_TYPE } ;
3
4
use std:: convert:: Infallible ;
4
5
// use iron::modifiers::Header;
5
6
// use iron::prelude::*;
6
7
// use iron::{headers, middleware, status};
7
- use log:: { debug, error, info, warn} ;
8
8
use hyper:: { Body , Request , Response , Server , StatusCode } ;
9
+ use log:: { debug, error, info, warn} ;
9
10
use routerify:: prelude:: * ;
10
11
// use router::Router;
11
12
use serde:: ser:: { Serialize , SerializeMap , Serializer } ;
12
13
13
14
use crate :: api;
14
15
//use crate::modifiers;
15
16
use crate :: sensors;
16
- use crate :: types:: RedisPool ;
17
17
use crate :: server:: SpaceapiServer ;
18
+ use crate :: types:: RedisPool ;
18
19
19
20
#[ derive( Debug ) ]
20
21
struct ErrorResponse {
@@ -59,7 +60,7 @@ pub async fn json_response_handler(req: Request<Body>) -> Result<Response<Body>,
59
60
warn ! (
60
61
"Could not retrieve key '{}' from Redis, omiting the sensor" ,
61
62
& sensor_spec. data_key
62
- ) ;
63
+ ) ;
63
64
match err {
64
65
sensors:: SensorError :: Redis ( e) => debug ! ( "Error: {:?}" , e) ,
65
66
sensors:: SensorError :: R2d2 ( e) => debug ! ( "Error: {:?}" , e) ,
@@ -74,36 +75,17 @@ pub async fn json_response_handler(req: Request<Body>) -> Result<Response<Body>,
74
75
}
75
76
76
77
// 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)
105
88
}
106
- */
107
89
108
90
pub ( crate ) struct UpdateHandler {
109
91
redis_pool : RedisPool ,
0 commit comments