forked from sparckles/Robyn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code clean up | Modularise rust code (sparckles#185)
* Add experimental io-uring * Code cleanup
- Loading branch information
Showing
8 changed files
with
204 additions
and
142 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use std::collections::HashMap; | ||
|
||
use std::fs::File; | ||
use std::io::Read; | ||
|
||
use actix_web::HttpResponseBuilder; | ||
|
||
// this should be something else | ||
// probably inside the submodule of the http router | ||
#[inline] | ||
pub fn apply_headers(response: &mut HttpResponseBuilder, headers: HashMap<String, String>) { | ||
for (key, val) in (headers).iter() { | ||
response.insert_header((key.clone(), val.clone())); | ||
} | ||
} | ||
|
||
/// A function to read lossy files and serve it as a html response | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `file_path` - The file path that we want the function to read | ||
/// | ||
// ideally this should be async | ||
pub fn read_file(file_path: &str) -> String { | ||
let mut file = File::open(file_path).unwrap(); | ||
let mut buf = vec![]; | ||
file.read_to_end(&mut buf).unwrap(); | ||
String::from_utf8_lossy(&buf).to_string() | ||
} |
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,4 +1,6 @@ | ||
mod processor; | ||
mod executors; | ||
mod io_helpers; | ||
mod request_handler; | ||
mod routers; | ||
mod server; | ||
mod shared_socket; | ||
|
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,115 @@ | ||
use crate::executors::{execute_http_function, execute_middleware_function}; | ||
|
||
use std::collections::HashMap; | ||
use std::str::FromStr; | ||
use std::sync::Arc; | ||
|
||
use actix_web::{web, HttpRequest, HttpResponse, HttpResponseBuilder}; | ||
// pyO3 module | ||
use crate::types::{Headers, PyFunction}; | ||
|
||
#[inline] | ||
pub fn apply_headers(response: &mut HttpResponseBuilder, headers: HashMap<String, String>) { | ||
for (key, val) in (headers).iter() { | ||
response.insert_header((key.clone(), val.clone())); | ||
} | ||
} | ||
|
||
/// This functions handles the incoming request matches it to the function and serves the response | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `function` - a PyFunction matched from the router | ||
/// | ||
/// # Errors | ||
/// | ||
/// When the route is not found. It should check if the 404 route exist and then serve it back | ||
/// There can also be PyError due to any mis processing of the files | ||
/// | ||
pub async fn handle_http_request( | ||
function: PyFunction, | ||
number_of_params: u8, | ||
headers: HashMap<String, String>, | ||
payload: &mut web::Payload, | ||
req: &HttpRequest, | ||
route_params: HashMap<String, String>, | ||
queries: HashMap<String, String>, | ||
) -> HttpResponse { | ||
let contents = match execute_http_function( | ||
function, | ||
payload, | ||
headers.clone(), | ||
req, | ||
route_params, | ||
queries, | ||
number_of_params, | ||
) | ||
.await | ||
{ | ||
Ok(res) => res, | ||
Err(err) => { | ||
println!("Error: {:?}", err); | ||
let mut response = HttpResponse::InternalServerError(); | ||
apply_headers(&mut response, headers.clone()); | ||
return response.finish(); | ||
} | ||
}; | ||
|
||
let body = contents.get("body").unwrap().to_owned(); | ||
let status_code = | ||
actix_http::StatusCode::from_str(contents.get("status_code").unwrap()).unwrap(); | ||
|
||
let headers: HashMap<String, String> = match contents.get("headers") { | ||
Some(headers) => { | ||
let h: HashMap<String, String> = serde_json::from_str(headers).unwrap(); | ||
h | ||
} | ||
None => HashMap::new(), | ||
}; | ||
|
||
println!("These are the headers from serde {:?}", headers); | ||
|
||
let mut response = HttpResponse::build(status_code); | ||
apply_headers(&mut response, headers.clone()); | ||
let final_response = if !body.is_empty() { | ||
response.body(body) | ||
} else { | ||
response.finish() | ||
}; | ||
|
||
println!( | ||
"The status code is {} and the headers are {:?}", | ||
final_response.status(), | ||
final_response.headers() | ||
); | ||
// response.body(contents.get("body").unwrap().to_owned()) | ||
final_response | ||
} | ||
|
||
pub async fn handle_http_middleware_request( | ||
function: PyFunction, | ||
number_of_params: u8, | ||
headers: &Arc<Headers>, | ||
payload: &mut web::Payload, | ||
req: &HttpRequest, | ||
route_params: HashMap<String, String>, | ||
queries: HashMap<String, String>, | ||
) -> HashMap<String, HashMap<String, String>> { | ||
let contents = match execute_middleware_function( | ||
function, | ||
payload, | ||
headers, | ||
req, | ||
route_params, | ||
queries, | ||
number_of_params, | ||
) | ||
.await | ||
{ | ||
Ok(res) => res, | ||
Err(_err) => HashMap::new(), | ||
}; | ||
|
||
println!("These are the middleware response {:?}", contents); | ||
contents | ||
} |
Oops, something went wrong.