-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
142 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
mod middleware_router; | ||
mod processor; | ||
mod router; | ||
mod routers; | ||
mod server; | ||
mod shared_socket; | ||
mod types; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod middleware_router; | ||
pub mod router; | ||
pub mod web_socket_router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use std::collections::HashMap; | ||
use std::sync::RwLock; | ||
// pyo3 modules | ||
use crate::types::PyFunction; | ||
use pyo3::prelude::*; | ||
use pyo3::types::PyAny; | ||
|
||
use actix_web::http::Method; | ||
use matchit::Node; | ||
|
||
/// Contains the thread safe hashmaps of different routes | ||
pub struct WebSocketRouter { | ||
web_socket_routes: RwLock<HashMap<String, HashMap<String, (PyFunction, u8)>>>, | ||
} | ||
|
||
impl WebSocketRouter { | ||
pub fn new() -> Self { | ||
Self { | ||
web_socket_routes: RwLock::new(HashMap::new()), | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn get_web_socket_map( | ||
&self, | ||
) -> &RwLock<HashMap<String, HashMap<String, (PyFunction, u8)>>> { | ||
&self.web_socket_routes | ||
} | ||
|
||
// Checks if the functions is an async function | ||
// Inserts them in the router according to their nature(CoRoutine/SyncFunction) | ||
pub fn add_websocket_route( | ||
&self, | ||
route: &str, | ||
connect_route: (Py<PyAny>, bool, u8), | ||
close_route: (Py<PyAny>, bool, u8), | ||
message_route: (Py<PyAny>, bool, u8), | ||
) { | ||
let table = self.get_web_socket_map(); | ||
let (connect_route_function, connect_route_is_async, connect_route_params) = connect_route; | ||
let (close_route_function, close_route_is_async, close_route_params) = close_route; | ||
let (message_route_function, message_route_is_async, message_route_params) = message_route; | ||
|
||
let insert_in_router = | ||
|handler: Py<PyAny>, is_async: bool, number_of_params: u8, socket_type: &str| { | ||
let function = if is_async { | ||
PyFunction::CoRoutine(handler) | ||
} else { | ||
PyFunction::SyncFunction(handler) | ||
}; | ||
|
||
println!("socket type is {:?} {:?}", table, route); | ||
|
||
table | ||
.write() | ||
.unwrap() | ||
.entry(route.to_string()) | ||
.or_default() | ||
.insert(socket_type.to_string(), (function, number_of_params)) | ||
}; | ||
|
||
insert_in_router( | ||
connect_route_function, | ||
connect_route_is_async, | ||
connect_route_params, | ||
"connect", | ||
); | ||
|
||
insert_in_router( | ||
close_route_function, | ||
close_route_is_async, | ||
close_route_params, | ||
"close", | ||
); | ||
|
||
insert_in_router( | ||
message_route_function, | ||
message_route_is_async, | ||
message_route_params, | ||
"message", | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters