Add users table
parent: tbd commit: 9f36e3f
1 | use ; |
2 | |
3 | use ; |
4 | use |
5 | , | TcpStream
6 | |
7 | , |
8 | Mutex, |
9 | , |
10 | , | JoinHandle
11 | ; |
12 | use ; |
13 | |
14 | use crate:: |
15 | , | AuthenticationTokenGranter
16 | , |
17 | , |
18 | , | Listeners
19 | |
20 | , |
21 | |
22 | RepositoryMessage, RepositoryMessageKind, RepositoryRequest, RepositoryResponse, |
23 | , |
24 | MessageKind, |
25 | , |
26 | |
27 | , |
28 | , | Repository
29 | , | User
30 | , |
31 | ; |
32 | |
33 | |
34 | pub task: , |
35 | |
36 | |
37 | |
38 | pub instance: InstanceMeta, |
39 | pub sender: , |
40 | pub task: , |
41 | |
42 | |
43 | /// Represents a connection which hasn't finished the handshake. |
44 | |
45 | pub socket: , |
46 | |
47 | |
48 | |
49 | |
50 | pub connections: , |
51 | pub instance_connections: , |
52 | |
53 | |
54 | pub async |
55 | mut socket: , |
56 | listeners: , |
57 | connections: , |
58 | backend: , |
59 | user_backend: , |
60 | auth_granter: , |
61 | addr: SocketAddr, |
62 | |
63 | let mut handshaked = false; |
64 | let this_instance = Instance |
65 | url: String from, |
66 | ; |
67 | |
68 | while let Some = socket.next .await |
69 | let message = match message |
70 | Ok => message, |
71 | Err => |
72 | error!; |
73 | continue; |
74 | |
75 | ; |
76 | |
77 | let payload = match message |
78 | => text.into_bytes, | Text
79 | => bytes, | Binary
80 | => continue, | Ping
81 | => continue, | Pong
82 | => | Close
83 | info!; |
84 | |
85 | return; |
86 | |
87 | _ => unreachable!, |
88 | ; |
89 | |
90 | let message = match |
91 | Ok => message, |
92 | Err => |
93 | error!; |
94 | continue; |
95 | |
96 | ; |
97 | |
98 | // info!("Read payload: {}", std::str::from_utf8(&payload).unwrap()); |
99 | |
100 | if let Handshake = message |
101 | match handshake |
102 | => | Initiate
103 | // Send HandshakeMessage::Response |
104 | let message = HandshakeResponse |
105 | identity: Instance |
106 | url: String from, |
107 | , |
108 | version: String from, |
109 | ; |
110 | |
111 | socket |
112 | .send |
113 | to_vec |
114 | , | Response
115 | |
116 | .unwrap, |
117 | |
118 | .await |
119 | .unwrap; |
120 | |
121 | continue; |
122 | |
123 | => | Response
124 | // Send HandshakeMessage::Finalize |
125 | let message = HandshakeFinalize ; |
126 | |
127 | socket |
128 | .send |
129 | to_vec |
130 | , | Finalize
131 | |
132 | .unwrap, |
133 | |
134 | .await |
135 | .unwrap; |
136 | |
137 | continue; |
138 | |
139 | => | Finalize
140 | handshaked = true; |
141 | |
142 | // Send HandshakeMessage::Finalize |
143 | let message = HandshakeFinalize ; |
144 | |
145 | socket |
146 | .send |
147 | to_vec |
148 | , | Finalize
149 | |
150 | .unwrap, |
151 | |
152 | .await |
153 | .unwrap; |
154 | |
155 | continue; |
156 | |
157 | |
158 | |
159 | |
160 | if !handshaked |
161 | continue; |
162 | |
163 | |
164 | if let Repository = &message |
165 | if repository.target.instance != this_instance |
166 | info!; |
167 | // We need to send this command to a different instance |
168 | |
169 | let mut listener = send_and_get_listener .await; |
170 | |
171 | // Wait for response |
172 | while let Ok = listener.recv .await |
173 | if let Repository |
174 | command: Response, |
175 | .. |
176 | = message |
177 | |
178 | socket |
179 | .send |
180 | .await |
181 | .unwrap; |
182 | |
183 | |
184 | continue; |
185 | else |
186 | // This message is targeting this instance |
187 | match &repository.command |
188 | => match request.clone | Request
189 | => | CreateRepository
190 | let mut backend = backend.lock .await; |
191 | let request = request.validate .await.unwrap; |
192 | let response = backend.create_repository .await; |
193 | |
194 | let response = match response |
195 | Ok => response, |
196 | Err => |
197 | error!; |
198 | continue; |
199 | |
200 | ; |
201 | drop; |
202 | |
203 | socket |
204 | .send |
205 | to_vec |
206 | RepositoryMessage |
207 | target: repository.target.clone, |
208 | command: Response |
209 | , | CreateRepository
210 | , |
211 | , |
212 | |
213 | .unwrap, |
214 | |
215 | .await |
216 | .unwrap; |
217 | |
218 | continue; |
219 | |
220 | => | RepositoryFileInspect
221 | let mut backend = backend.lock .await; |
222 | let request = request.validate .await.unwrap; |
223 | let response = backend.repository_file_inspect; |
224 | |
225 | let response = match response |
226 | Ok => response, |
227 | Err => |
228 | error!; |
229 | continue; |
230 | |
231 | ; |
232 | drop; |
233 | |
234 | socket |
235 | .send |
236 | to_vec |
237 | RepositoryMessage |
238 | target: repository.target.clone, |
239 | command: Response |
240 | RepositoryFileInspection |
241 | response, |
242 | , |
243 | , |
244 | , |
245 | |
246 | .unwrap, |
247 | |
248 | .await |
249 | .unwrap; |
250 | continue; |
251 | |
252 | => | RepositoryInfo
253 | let mut backend = backend.lock .await; |
254 | let request = request.validate .await.unwrap; |
255 | let response = backend.repository_info .await; |
256 | |
257 | let response = match response |
258 | Ok => response, |
259 | Err => |
260 | error!; |
261 | continue; |
262 | |
263 | ; |
264 | drop; |
265 | |
266 | socket |
267 | .send |
268 | to_vec |
269 | RepositoryMessage |
270 | target: repository.target.clone, |
271 | command: Response |
272 | , | RepositoryInfo
273 | , |
274 | , |
275 | |
276 | .unwrap, |
277 | |
278 | .await |
279 | .unwrap; |
280 | continue; |
281 | |
282 | => | IssuesCount
283 | let request = &request.validate .await.unwrap; |
284 | |
285 | let mut backend = backend.lock .await; |
286 | let response = backend.issues_count; |
287 | |
288 | let response = match response |
289 | Ok => response, |
290 | Err => |
291 | error!; |
292 | continue; |
293 | |
294 | ; |
295 | drop; |
296 | |
297 | socket |
298 | .send |
299 | to_vec |
300 | RepositoryMessage |
301 | target: repository.target.clone, |
302 | command: Response |
303 | , | IssuesCount
304 | , |
305 | , |
306 | |
307 | .unwrap, |
308 | |
309 | .await |
310 | .unwrap; |
311 | continue; |
312 | |
313 | => | IssueLabels
314 | let request = &request.validate .await.unwrap; |
315 | |
316 | let mut backend = backend.lock .await; |
317 | let response = backend.issue_labels; |
318 | |
319 | let response = match response |
320 | Ok => response, |
321 | Err => |
322 | error!; |
323 | continue; |
324 | |
325 | ; |
326 | drop; |
327 | socket |
328 | .send |
329 | to_vec |
330 | RepositoryMessage |
331 | target: repository.target.clone, |
332 | command: Response |
333 | , | IssueLabels
334 | , |
335 | , |
336 | |
337 | .unwrap, |
338 | |
339 | .await |
340 | .unwrap; |
341 | continue; |
342 | |
343 | => | Issues
344 | let request = request.validate .await.unwrap; |
345 | |
346 | let mut backend = backend.lock .await; |
347 | let response = backend.issues; |
348 | |
349 | let response = match response |
350 | Ok => response, |
351 | Err => |
352 | error!; |
353 | continue; |
354 | |
355 | ; |
356 | drop; |
357 | |
358 | socket |
359 | .send |
360 | to_vec |
361 | RepositoryMessage |
362 | target: repository.target.clone, |
363 | command: Response |
364 | , | Issues
365 | , |
366 | , |
367 | |
368 | .unwrap, |
369 | |
370 | .await |
371 | .unwrap; |
372 | continue; |
373 | |
374 | , |
375 | => | Response
376 | unreachable! |
377 | |
378 | |
379 | |
380 | |
381 | |
382 | if let Authentication = &message |
383 | match authentication |
384 | => match request | Request
385 | => | AuthenticationToken
386 | let mut granter = auth_granter.lock .await; |
387 | |
388 | let response = granter.token_request .await.unwrap; |
389 | drop; |
390 | |
391 | socket |
392 | .send |
393 | to_vec |
394 | Response |
395 | |
396 | .unwrap, |
397 | |
398 | .await |
399 | .unwrap; |
400 | continue; |
401 | |
402 | => | TokenExtension
403 | let mut granter = auth_granter.lock .await; |
404 | |
405 | let response = granter |
406 | .extension_request |
407 | .await |
408 | .unwrap_or; |
409 | drop; |
410 | |
411 | socket |
412 | .send |
413 | to_vec |
414 | Response |
415 | |
416 | .unwrap, |
417 | |
418 | .await |
419 | .unwrap; |
420 | continue; |
421 | |
422 | , |
423 | => unreachable!, | Response
424 | |
425 | |
426 | |
427 | |
428 | info!; |
429 | |
430 | |
431 | async |
432 | message: MessageKind, |
433 | listeners: & , |
434 | connections: & , |
435 | |
436 | let : = |
437 | match &message |
438 | => | Handshake
439 | todo! |
440 | |
441 | => , | Repository
442 | => todo!, | Authentication
443 | ; |
444 | |
445 | let target = match |
446 | => instance.clone, |
447 | => user.instance.clone, |
448 | => repository.instance.clone, |
449 | _ => unreachable!, |
450 | ; |
451 | |
452 | let mut listeners = listeners.lock .await; |
453 | let listener = listeners.add; |
454 | drop; |
455 | |
456 | let connections = connections.lock .await; |
457 | |
458 | if let Some = connections.instance_connections.get |
459 | connection.sender.send; |
460 | else |
461 | error!; |
462 | |
463 | panic!; |
464 | |
465 | |
466 | drop; |
467 | |
468 | listener |
469 | |
470 |