1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use juniper::{execute, Context as JuniperContext, GraphQLType, RootNode, Variables};
use kubos_system::Config;
use serde_json;
use std::cell::RefCell;
use std::collections::HashMap;
use std::net::{SocketAddr, UdpSocket};
pub struct Context<T> {
subsystem: T,
storage: RefCell<HashMap<String, String>>,
}
impl<T> JuniperContext for Context<T> {}
impl<T> Context<T> {
pub fn subsystem(&self) -> &T {
&self.subsystem
}
pub fn get(&self, name: &str) -> String {
let stor = self.storage.borrow();
match stor.get(&name.to_string()) {
Some(s) => s.clone(),
None => "".to_string(),
}
}
pub fn set(&self, key: &str, value: &str) {
let mut stor = self.storage.borrow_mut();
stor.insert(key.to_string(), value.to_string());
}
pub fn clear(&self, name: &String) {
let mut storage = self.storage.borrow_mut();
storage.remove(name);
}
pub fn clear_all(&self) {
self.storage.borrow_mut().clear();
}
}
pub struct Service<'a, Query, Mutation, S>
where
Query: GraphQLType<Context = Context<S>> + Send + Sync + 'static,
Mutation: GraphQLType<Context = Context<S>> + Send + Sync + 'static,
{
config: Config,
root_node: RootNode<'a, Query, Mutation>,
context: Context<S>,
}
impl<'a, Query, Mutation, S> Service<'a, Query, Mutation, S>
where
Query: GraphQLType<Context = Context<S>, TypeInfo = ()> + Send + Sync + 'static,
Mutation: GraphQLType<Context = Context<S>, TypeInfo = ()> + Send + Sync + 'static,
{
pub fn new(config: Config, subsystem: S, query: Query, mutation: Mutation) -> Self {
Service {
config: config,
root_node: RootNode::new(query, mutation),
context: Context {
subsystem: subsystem,
storage: RefCell::new(HashMap::new()),
},
}
}
pub fn start(&self) {
let addr = self.config.hosturl().parse::<SocketAddr>().unwrap();
let socket = UdpSocket::bind(&addr).unwrap();
println!("Listening on: {}", socket.local_addr().unwrap());
let mut buf = [0; 4096];
loop {
let (size, peer) = socket
.recv_from(&mut buf)
.expect("Failed to receive a message");
if let Ok(query_string) = String::from_utf8(buf[0..(size)].to_vec()) {
let res = self.process(query_string);
let _amt = socket.send_to(&res.as_bytes(), &peer);
}
}
}
pub fn process(&self, query: String) -> String {
match execute(
&query,
None,
&self.root_node,
&Variables::new(),
&self.context,
) {
Ok((val, errs)) => {
let errs_msg: String = errs.into_iter()
.map(|x| serde_json::to_string(&x).unwrap())
.collect();
json!({
"msg": val,
"errs": errs_msg})
.to_string()
}
Err(e) => serde_json::to_string(&e).unwrap(),
}
}
}