forked from sparckles/Robyn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rs
560 lines (493 loc) · 19.9 KB
/
server.rs
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
use crate::executors::{
execute_http_function, execute_middleware_function, execute_startup_handler,
};
use crate::routers::const_router::ConstRouter;
use crate::routers::Router;
use crate::routers::http_router::HttpRouter;
use crate::routers::{middleware_router::MiddlewareRouter, web_socket_router::WebSocketRouter};
use crate::shared_socket::SocketHeld;
use crate::types::function_info::{FunctionInfo, MiddlewareType};
use crate::types::headers::Headers;
use crate::types::request::Request;
use crate::types::response::Response;
use crate::types::HttpMethod;
use crate::types::MiddlewareReturn;
use crate::websockets::start_web_socket;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::{Relaxed, SeqCst};
use std::sync::{Arc, RwLock};
use std::process::exit;
use std::{env, thread};
use actix_files::Files;
use actix_http::KeepAlive;
use actix_web::*;
// pyO3 module
use log::{debug, error};
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
const MAX_PAYLOAD_SIZE: &str = "ROBYN_MAX_PAYLOAD_SIZE";
const DEFAULT_MAX_PAYLOAD_SIZE: usize = 1_000_000; // 1Mb
static STARTED: AtomicBool = AtomicBool::new(false);
#[derive(Clone)]
struct Directory {
route: String,
directory_path: String,
show_files_listing: bool,
index_file: Option<String>,
}
#[pyclass]
pub struct Server {
router: Arc<HttpRouter>,
const_router: Arc<ConstRouter>,
websocket_router: Arc<WebSocketRouter>,
middleware_router: Arc<MiddlewareRouter>,
global_request_headers: Arc<Headers>,
global_response_headers: Arc<Headers>,
directories: Arc<RwLock<Vec<Directory>>>,
startup_handler: Option<Arc<FunctionInfo>>,
shutdown_handler: Option<Arc<FunctionInfo>>,
excluded_response_headers_paths: Option<Vec<String>>,
}
#[pymethods]
impl Server {
#[new]
pub fn new() -> Self {
Self {
router: Arc::new(HttpRouter::new()),
const_router: Arc::new(ConstRouter::new()),
websocket_router: Arc::new(WebSocketRouter::new()),
middleware_router: Arc::new(MiddlewareRouter::new()),
global_request_headers: Arc::new(Headers::new(None)),
global_response_headers: Arc::new(Headers::new(None)),
directories: Arc::new(RwLock::new(Vec::new())),
startup_handler: None,
shutdown_handler: None,
excluded_response_headers_paths: None,
}
}
pub fn start(
&mut self,
py: Python,
socket: &PyCell<SocketHeld>,
workers: usize,
) -> PyResult<()> {
pyo3_log::init();
if STARTED
.compare_exchange(false, true, SeqCst, Relaxed)
.is_err()
{
debug!("Robyn is already running...");
return Ok(());
}
let raw_socket = socket.try_borrow_mut()?.get_socket();
let router = self.router.clone();
let const_router = self.const_router.clone();
let middleware_router = self.middleware_router.clone();
let web_socket_router = self.websocket_router.clone();
let global_request_headers = self.global_request_headers.clone();
let global_response_headers = self.global_response_headers.clone();
let directories = self.directories.clone();
let asyncio = py.import("asyncio")?;
let event_loop = asyncio.call_method0("new_event_loop")?;
asyncio.call_method1("set_event_loop", (event_loop,))?;
let startup_handler = self.startup_handler.clone();
let shutdown_handler = self.shutdown_handler.clone();
let excluded_response_headers_paths = self.excluded_response_headers_paths.clone();
let task_locals = pyo3_asyncio::TaskLocals::new(event_loop).copy_context(py)?;
let task_locals_copy = task_locals.clone();
let max_payload_size = env::var(MAX_PAYLOAD_SIZE)
.unwrap_or(DEFAULT_MAX_PAYLOAD_SIZE.to_string())
.trim()
.parse::<usize>()
.map_err(|e| {
PyValueError::new_err(format!(
"Failed to parse environment variable {MAX_PAYLOAD_SIZE} - {e}"
))
})?;
thread::spawn(move || {
actix_web::rt::System::new().block_on(async move {
debug!("The number of workers is {}", workers);
execute_startup_handler(startup_handler, &task_locals_copy)
.await
.unwrap();
HttpServer::new(move || {
let mut app = App::new();
let task_locals = task_locals_copy.clone();
let directories = directories.read().unwrap();
// this loop matches three types of directory serving
// 1. Serves a build folder. e.g. the build folder generated from yarn build
// 2. Shows file listing
// 3. Just serves the file without any redirection to sub links
for directory in directories.iter() {
if let Some(index_file) = &directory.index_file {
app = app.service(
Files::new(&directory.route, &directory.directory_path)
.index_file(index_file)
.redirect_to_slash_directory(),
);
} else if directory.show_files_listing {
app = app.service(
Files::new(&directory.route, &directory.directory_path)
.redirect_to_slash_directory()
.show_files_listing(),
);
} else {
app = app
.service(Files::new(&directory.route, &directory.directory_path));
}
}
app = app
.app_data(web::Data::new(router.clone()))
.app_data(web::Data::new(const_router.clone()))
.app_data(web::Data::new(middleware_router.clone()))
.app_data(web::Data::new(global_request_headers.clone()))
.app_data(web::Data::new(global_response_headers.clone()))
.app_data(web::Data::new(excluded_response_headers_paths.clone()));
let web_socket_map = web_socket_router.get_web_socket_map();
for (elem, value) in (web_socket_map.read()).iter() {
let endpoint = elem.clone();
let path_params = value.clone();
let task_locals = task_locals.clone();
app = app.route(
&endpoint.clone(),
web::get().to(move |stream: web::Payload, req: HttpRequest| {
let endpoint_copy = endpoint.clone();
start_web_socket(
req,
stream,
path_params.clone(),
task_locals.clone(),
endpoint_copy,
)
}),
);
}
debug!("Max payload size is {}", max_payload_size);
app.app_data(web::PayloadConfig::new(max_payload_size))
.default_service(web::route().to(
move |router: web::Data<Arc<HttpRouter>>,
const_router: web::Data<Arc<ConstRouter>>,
middleware_router: web::Data<Arc<MiddlewareRouter>>,
payload: web::Payload,
global_request_headers,
global_response_headers,
response_headers_exclude_paths,
req| {
pyo3_asyncio::tokio::scope_local(task_locals.clone(), async move {
index(
router,
payload,
const_router,
middleware_router,
global_request_headers,
global_response_headers,
response_headers_exclude_paths,
req,
)
.await
})
},
))
})
.keep_alive(KeepAlive::Os)
.workers(workers)
.client_request_timeout(std::time::Duration::from_secs(0))
.listen(raw_socket.into())
.unwrap()
.run()
.await
.unwrap();
});
});
let event_loop = (*event_loop).call_method0("run_forever");
if event_loop.is_err() {
debug!("Ctrl c handler");
// executing this from the same file (and not creating a function -- like startup handler)
// to fix an issue that arises when a new async function is spooled up.
// if we create a function & move the code, the function won't run s & raises the warning:
// "unused implementer of `futures_util::Future` that must be used futures do nothing
// unless you await or poll them."
// but, adding `.await` raises the error "await is used inside non-async function,
// which is not an async context".
// which can only be solved by creating a new async function -- hence, resorting
// to this solution
if let Some(function) = shutdown_handler {
if function.is_async {
debug!("Shutdown event handler async");
pyo3_asyncio::tokio::run_until_complete(
task_locals.event_loop(py),
pyo3_asyncio::into_future_with_locals(
&task_locals.clone(),
function.handler.as_ref(py).call0()?,
)
.unwrap(),
)
.unwrap();
} else {
debug!("Shutdown event handler");
Python::with_gil(|py| function.handler.call0(py))?;
}
}
exit(0);
}
Ok(())
}
pub fn add_directory(
&mut self,
route: String,
directory_path: String,
show_files_listing: bool,
index_file: Option<String>,
) {
self.directories.write().unwrap().push(Directory {
route,
directory_path,
show_files_listing,
index_file,
});
}
/// Removes a new request header to our concurrent hashmap
/// this can be called after the server has started.
pub fn remove_header(&self, key: &str) {
self.global_request_headers.headers.remove(key);
}
/// Removes a new response header to our concurrent hashmap
/// this can be called after the server has started.
pub fn remove_response_header(&self, key: &str) {
self.global_response_headers.headers.remove(key);
}
pub fn apply_request_headers(&mut self, headers: &Headers) {
self.global_request_headers = Arc::new(headers.clone());
}
pub fn apply_response_headers(&mut self, headers: &Headers) {
self.global_response_headers = Arc::new(headers.clone());
}
pub fn set_response_headers_exclude_paths(
&mut self,
excluded_response_headers_paths: Option<Vec<String>>,
) {
self.excluded_response_headers_paths = excluded_response_headers_paths;
}
/// Add a new route to the routing tables
/// can be called after the server has been started
pub fn add_route(
&self,
py: Python,
route_type: &HttpMethod,
route: &str,
function: FunctionInfo,
is_const: bool,
) {
let second_route: String = if route.ends_with('/') {
route[0..route.len() - 1].to_string()
} else {
format!("{}/", route)
};
self._add_route(py, route_type, route, function.clone(), is_const);
self._add_route(py, route_type, &second_route, function, is_const);
}
fn _add_route(
&self,
py: Python,
route_type: &HttpMethod,
route: &str,
function: FunctionInfo,
is_const: bool,
) {
debug!("Route added for {:?} {} ", route_type, route);
let asyncio = py.import("asyncio").unwrap();
let event_loop = asyncio.call_method0("get_event_loop").unwrap();
if is_const {
match self
.const_router
.add_route(route_type, route, function, Some(event_loop))
{
Ok(_) => (),
Err(e) => {
debug!("Error adding const route {}", e);
}
}
} else {
match self.router.add_route(route_type, route, function, None) {
Ok(_) => (),
Err(e) => {
debug!("Error adding route {}", e);
}
}
}
}
/// Add a new global middleware
/// can be called after the server has been started
pub fn add_global_middleware(&self, middleware_type: &MiddlewareType, function: FunctionInfo) {
self.middleware_router
.add_global_middleware(middleware_type, function)
.unwrap();
}
/// Add a new route to the routing tables
/// can be called after the server has been started
pub fn add_middleware_route(
&self,
middleware_type: &MiddlewareType,
route: &str,
function: FunctionInfo,
) {
debug!(
"MiddleWare Route added for {:?} {} ",
middleware_type, route
);
self.middleware_router
.add_route(middleware_type, route, function, None)
.unwrap();
}
/// Add a new web socket route to the routing tables
/// can be called after the server has been started
pub fn add_web_socket_route(
&mut self,
route: &str,
connect_route: FunctionInfo,
close_route: FunctionInfo,
message_route: FunctionInfo,
) {
self.websocket_router
.add_websocket_route(route, connect_route, close_route, message_route);
}
/// Add a new startup handler
pub fn add_startup_handler(&mut self, function: FunctionInfo) {
self.startup_handler = Some(Arc::new(function));
debug!("Added startup handler {:?}", self.startup_handler);
}
/// Add a new shutdown handler
pub fn add_shutdown_handler(&mut self, function: FunctionInfo) {
self.shutdown_handler = Some(Arc::new(function));
debug!("Added shutdown handler {:?}", self.shutdown_handler);
}
}
impl Default for Server {
fn default() -> Self {
Self::new()
}
}
/// This is our service handler. It receives a Request, routes on it
/// path, and returns a Future of a Response.
#[allow(clippy::too_many_arguments)]
async fn index(
router: web::Data<Arc<HttpRouter>>,
payload: web::Payload,
const_router: web::Data<Arc<ConstRouter>>,
middleware_router: web::Data<Arc<MiddlewareRouter>>,
global_request_headers: web::Data<Arc<Headers>>,
global_response_headers: web::Data<Arc<Headers>>,
excluded_response_headers_paths: web::Data<Option<Vec<String>>>,
req: HttpRequest,
) -> impl Responder {
let mut request = Request::from_actix_request(&req, payload, &global_request_headers).await;
// Before middleware
// Global
let mut before_middlewares =
middleware_router.get_global_middlewares(&MiddlewareType::BeforeRequest);
// Route specific
if let Some((function, route_params)) =
middleware_router.get_route(&MiddlewareType::BeforeRequest, req.uri().path())
{
before_middlewares.push(function);
request.path_params = route_params;
}
for before_middleware in before_middlewares {
request = match execute_middleware_function(&request, &before_middleware).await {
Ok(MiddlewareReturn::Request(r)) => r,
Ok(MiddlewareReturn::Response(r)) => {
// If a before middleware returns a response, we abort the request and return the response
return r;
}
Err(e) => {
error!(
"Error while executing before middleware function for endpoint `{}`: {}",
req.uri().path(),
get_traceback(e.downcast_ref::<PyErr>().unwrap())
);
return Response::internal_server_error(None);
}
};
}
// Route execution
let mut response = if let Some(res) = const_router.get_route(
&HttpMethod::from_actix_method(req.method()),
req.uri().path(),
) {
res
} else if let Some((function, route_params)) = router.get_route(
&HttpMethod::from_actix_method(req.method()),
req.uri().path(),
) {
request.path_params = route_params;
match execute_http_function(&request, &function).await {
Ok(r) => r,
Err(e) => {
error!(
"Error while executing route function for endpoint `{}`: {}",
req.uri().path(),
get_traceback(&e)
);
Response::internal_server_error(None)
}
}
} else {
Response::not_found(None)
};
debug!("OG Response : {:?}", response);
response.headers.extend(&global_response_headers);
match &excluded_response_headers_paths.get_ref() {
None => {}
Some(excluded_response_headers_paths) => {
if excluded_response_headers_paths.contains(&req.uri().path().to_owned()) {
response.headers.clear();
}
}
}
debug!("Extended Response : {:?}", response);
// After middleware
// Global
let mut after_middlewares =
middleware_router.get_global_middlewares(&MiddlewareType::AfterRequest);
// Route specific
if let Some((function, _)) =
middleware_router.get_route(&MiddlewareType::AfterRequest, req.uri().path())
{
after_middlewares.push(function);
}
for after_middleware in after_middlewares {
response = match execute_middleware_function(&response, &after_middleware).await {
Ok(MiddlewareReturn::Request(_)) => {
error!("After middleware returned a request");
return Response::internal_server_error(Some(&response.headers));
}
Ok(MiddlewareReturn::Response(r)) => {
let response = r;
debug!("Response returned: {:?}", response);
response
}
Err(e) => {
error!(
"Error while executing after middleware function for endpoint `{}`: {}",
req.uri().path(),
get_traceback(e.downcast_ref::<PyErr>().unwrap())
);
return Response::internal_server_error(Some(&response.headers));
}
};
}
debug!("Response returned: {:?}", response);
response
}
fn get_traceback(error: &PyErr) -> String {
Python::with_gil(|py| -> String {
if let Some(traceback) = error.traceback(py) {
let msg = match traceback.format() {
Ok(msg) => format!("\n{msg} {error}"),
Err(e) => e.to_string(),
};
return msg;
};
error.value(py).to_string()
})
}