Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the HTML file serving #46

Merged
merged 1 commit into from
Jul 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@ use crate::types::PyFunction;
use pyo3::prelude::*;
use pyo3::types::PyDict;

use std::fs;
use std::fs::File;
use std::io::Read;

use hyper::{Body, Response, StatusCode};

// Handle message fetches the response function
// function is the response function fetched from the router
// tokio task is spawned depending on the type of function fetched (Sync/Async)

/// 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_request(function: PyFunction) -> Result<Response<Body>, hyper::Error> {
let contents = match execute_function(function).await {
Ok(res) => res,
Expand All @@ -27,8 +39,17 @@ pub async fn handle_request(function: PyFunction) -> Result<Response<Body>, hype
}

// ideally this should be async
/// 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
///
fn read_file(file_path: &str) -> String {
fs::read_to_string(file_path).expect("Something went wrong reading the html response")
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()
}

#[inline]
Expand Down