1
1
//! Handlers for the server.
2
2
3
+ use std:: convert:: Infallible ;
3
4
// use iron::modifiers::Header;
4
5
// use iron::prelude::*;
5
6
// use iron::{headers, middleware, status};
6
7
use log:: { debug, error, info, warn} ;
8
+ use hyper:: { Body , Request , Response , Server , StatusCode } ;
9
+ use routerify:: prelude:: * ;
7
10
// use router::Router;
8
11
use serde:: ser:: { Serialize , SerializeMap , Serializer } ;
9
12
10
13
use crate :: api;
11
- use crate :: modifiers;
14
+ // use crate::modifiers;
12
15
use crate :: sensors;
13
16
use crate :: types:: RedisPool ;
17
+ use crate :: server:: SpaceapiServer ;
14
18
15
19
#[ derive( Debug ) ]
16
20
struct ErrorResponse {
@@ -29,73 +33,51 @@ impl Serialize for ErrorResponse {
29
33
}
30
34
}
31
35
32
- pub ( crate ) struct ReadHandler {
33
- status : api:: Status ,
34
- redis_pool : RedisPool ,
35
- sensor_specs : sensors:: SafeSensorSpecs ,
36
- status_modifiers : Vec < Box < dyn modifiers:: StatusModifier > > ,
37
- }
38
-
39
- impl ReadHandler {
40
- pub ( crate ) fn new (
41
- status : api:: Status ,
42
- redis_pool : RedisPool ,
43
- sensor_specs : sensors:: SafeSensorSpecs ,
44
- status_modifiers : Vec < Box < dyn modifiers:: StatusModifier > > ,
45
- ) -> ReadHandler {
46
- ReadHandler {
47
- status,
48
- redis_pool,
49
- sensor_specs,
50
- status_modifiers,
51
- }
52
- }
53
-
54
- fn build_response_json ( & self ) -> String {
55
- // Create a mutable copy of the status struct
56
- let mut status_copy = self . status . clone ( ) ;
57
-
58
- // Process registered sensors
59
- for sensor_spec in self . sensor_specs . iter ( ) {
60
- match sensor_spec. get_sensor_value ( & self . redis_pool ) {
61
- // Value could be read successfullly
62
- Ok ( value) => {
63
- if status_copy. sensors . is_none ( ) {
64
- status_copy. sensors = Some ( api:: Sensors {
65
- people_now_present : vec ! [ ] ,
66
- temperature : vec ! [ ] ,
67
- } ) ;
68
- }
69
- sensor_spec
70
- . template
71
- . to_sensor ( & value, & mut status_copy. sensors . as_mut ( ) . unwrap ( ) ) ;
36
+ pub async fn json_response_handler ( req : Request < Body > ) -> Result < Response < Body > , Infallible > {
37
+ // Create a mutable copy of the status struct
38
+ let state = req. data :: < SpaceapiServer > ( ) . unwrap ( ) ;
39
+ let mut status_copy = state. status . clone ( ) ;
40
+
41
+ // Process registered sensors
42
+ for sensor_spec in state. sensor_specs . iter ( ) {
43
+ match sensor_spec. get_sensor_value ( & state. redis_pool ) {
44
+ // Value could be read successfullly
45
+ Ok ( value) => {
46
+ if status_copy. sensors . is_none ( ) {
47
+ status_copy. sensors = Some ( api:: Sensors {
48
+ people_now_present : vec ! [ ] ,
49
+ temperature : vec ! [ ] ,
50
+ } ) ;
72
51
}
52
+ sensor_spec
53
+ . template
54
+ . to_sensor ( & value, & mut status_copy. sensors . as_mut ( ) . unwrap ( ) ) ;
55
+ }
73
56
74
- // Value could not be read, do error logging
75
- Err ( err) => {
76
- warn ! (
77
- "Could not retrieve key '{}' from Redis, omiting the sensor" ,
78
- & sensor_spec. data_key
57
+ // Value could not be read, do error logging
58
+ Err ( err) => {
59
+ warn ! (
60
+ "Could not retrieve key '{}' from Redis, omiting the sensor" ,
61
+ & sensor_spec. data_key
79
62
) ;
80
- match err {
81
- sensors:: SensorError :: Redis ( e) => debug ! ( "Error: {:?}" , e) ,
82
- sensors:: SensorError :: R2d2 ( e) => debug ! ( "Error: {:?}" , e) ,
83
- sensors:: SensorError :: UnknownSensor ( e) => warn ! ( "Error: {:?}" , e) ,
84
- }
63
+ match err {
64
+ sensors:: SensorError :: Redis ( e) => debug ! ( "Error: {:?}" , e) ,
65
+ sensors:: SensorError :: R2d2 ( e) => debug ! ( "Error: {:?}" , e) ,
66
+ sensors:: SensorError :: UnknownSensor ( e) => warn ! ( "Error: {:?}" , e) ,
85
67
}
86
68
}
87
69
}
70
+ }
88
71
89
- for status_modifier in & self . status_modifiers {
90
- status_modifier. modify ( & mut status_copy) ;
91
- }
72
+ for status_modifier in & state . status_modifiers {
73
+ status_modifier. modify ( & mut status_copy) ;
74
+ }
92
75
93
- // Serialize to JSON
94
- serde_json:: to_string ( & status_copy) . expect (
95
- "Status object could not be serialized to JSON. \
76
+ // 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. \
96
79
Please open an issue at https://github.com/spaceapi-community/spaceapi-server-rs/issues",
97
- )
98
- }
80
+ ) ) ) )
99
81
}
100
82
101
83
/*
0 commit comments