Skip to content

Commit 31020eb

Browse files
committed
WIP: content type
1 parent fabc88e commit 31020eb

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/server/handlers.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
//! Handlers for the server.
22
3+
use hyper::header::{ACCESS_CONTROL_ALLOW_ORIGIN, CONTENT_TYPE};
34
use 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};
88
use hyper::{Body, Request, Response, Server, StatusCode};
9+
use log::{debug, error, info, warn};
910
use routerify::prelude::*;
1011
// use router::Router;
1112
use serde::ser::{Serialize, SerializeMap, Serializer};
1213

1314
use crate::api;
1415
//use crate::modifiers;
1516
use crate::sensors;
16-
use crate::types::RedisPool;
1717
use crate::server::SpaceapiServer;
18+
use crate::types::RedisPool;
1819

1920
#[derive(Debug)]
2021
struct 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,10 +75,16 @@ 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-
))))
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)
8188
}
8289

8390
/*

0 commit comments

Comments
 (0)