From dd4de4d94c8fa6ca3da59592c9269016f3416af6 Mon Sep 17 00:00:00 2001 From: Craig Condon Date: Mon, 26 Sep 2022 21:23:42 -0500 Subject: [PATCH] include headers --- Cargo.lock | 2 + libs/designer/Cargo.toml | 2 + libs/designer/src/server/mod.rs | 2 +- libs/designer/src/server/routes.rs | 83 ++++++++++++++++-------------- libs/designer/src/server/server.rs | 7 +-- 5 files changed, 51 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 689a447fb..270f9749b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1135,10 +1135,12 @@ dependencies = [ "futures", "futures-core", "futures-util", + "headers", "http", "http-body", "hyper", "include_dir", + "mime_guess", "notify", "open", "paperclip_common", diff --git a/libs/designer/Cargo.toml b/libs/designer/Cargo.toml index 0c9ab2c9e..8de7c1237 100644 --- a/libs/designer/Cargo.toml +++ b/libs/designer/Cargo.toml @@ -18,7 +18,9 @@ tokio = { version = "1.0", features = [ "rt-multi-thread", "time", "fs", "macros paperclip_common = { path = "../rs-common" } paperclip_evaluator = { path = "../evaluator" } hyper = "0.14.20" +headers = "0.3.8" portpicker = "0.1.1" +mime_guess = "2.0.4" warp = "0.3.2" futures = "0.3.21" futures-core = "0.3.24" diff --git a/libs/designer/src/server/mod.rs b/libs/designer/src/server/mod.rs index 5afb669cd..18d831ee4 100644 --- a/libs/designer/src/server/mod.rs +++ b/libs/designer/src/server/mod.rs @@ -1,6 +1,6 @@ mod res_body; +mod routes; pub mod server; mod service; mod utils; -mod routes; pub use server::*; diff --git a/libs/designer/src/server/routes.rs b/libs/designer/src/server/routes.rs index 4cae7905f..fac527026 100644 --- a/libs/designer/src/server/routes.rs +++ b/libs/designer/src/server/routes.rs @@ -1,58 +1,63 @@ // https://github.com/hyperium/tonic/blob/master/examples/src/hyper_warp/server.rs +use headers::{AcceptRanges, ContentLength, ContentType, HeaderMapExt}; +use hyper::Body; use include_dir::{include_dir, Dir, File}; -use warp::reply::{Reply, Response}; -use warp::reject::{Rejection}; -use warp::path::{tail, Tail}; -use warp::{Filter}; use warp::any; -use hyper::Body; +use warp::path::{tail, Tail}; +use warp::reject::Rejection; +use warp::reply::{Reply, Response}; +use warp::Filter; -use futures_util::{future}; +use futures_util::future; static DESIGNER_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/dist"); -pub struct StaticFile(&'static File<'static>); +pub struct StaticFile(String, &'static File<'static>); impl Reply for StaticFile { - fn into_response(self) -> Response { - Response::new(Body::from(self.0.contents())) - } + fn into_response(self) -> Response { + let mime = mime_guess::from_path(&self.0).first_or_octet_stream(); + + let mut resp = Response::new(Body::from(self.1.contents())); + + resp.headers_mut() + .typed_insert(ContentLength(self.1.contents().len() as u64)); + resp.headers_mut().typed_insert(ContentType::from(mime)); + resp.headers_mut().typed_insert(AcceptRanges::bytes()); + resp + } } // Inspiration: https://github.com/seanmonstar/warp/blob/master/examples/returning.rs -pub fn routes() -> impl Filter + Clone { - let get_static_file = any() - .and(path_from_tail()) - .and_then(static_file_reply); - +pub fn routes() -> impl Filter + Clone { + let get_static_file = any().and(path_from_tail()).and_then(static_file_reply); - get_static_file + get_static_file } -fn path_from_tail() -> impl Filter + Clone { - tail().and_then(move |tail: Tail| { - println!("tailing from file"); - future::ready::>(Ok(tail.as_str().to_string())) - }) +fn path_from_tail() -> impl Filter + Clone { + tail().and_then(move |tail: Tail| { + println!("tailing from file"); + future::ready::>(Ok(tail.as_str().to_string())) + }) } +pub async fn static_file_reply(path: String) -> Result { + let resolved_path = if path == "" { + "index.html".to_string() + } else { + path.to_string() + }; -pub async fn static_file_reply( - path: String -) -> Result { - let resolved_path = if path == "" { - "index.html".to_string() - } else { - path.to_string() - }; - - let file = DESIGNER_DIR.get_file(&resolved_path); - - if let Some(file) = file { - return Ok(StaticFile(&file)); - } else { - println!("File {} not found", path); - } - Err(warp::reject::not_found()) -} \ No newline at end of file + let file = DESIGNER_DIR.get_file(&resolved_path); + + println!("{:?}", file); + + if let Some(file) = file { + return Ok(StaticFile(resolved_path, &file)); + } else { + println!("File {} not found", path); + } + Err(warp::reject::not_found()) +} diff --git a/libs/designer/src/server/server.rs b/libs/designer/src/server/server.rs index d9f18cc1c..035f02a1e 100644 --- a/libs/designer/src/server/server.rs +++ b/libs/designer/src/server/server.rs @@ -1,8 +1,8 @@ // https://github.com/hyperium/tonic/blob/master/examples/src/hyper_warp/server.rs use super::service::DesignerService; use super::utils::content_types; -use futures::future::{self, Either, TryFutureExt}; use crate::server::routes::routes; +use futures::future::{self, Either, TryFutureExt}; use hyper::{service::make_service_fn, Server}; use open; use paperclip_project::{ConfigContext, ProjectIO}; @@ -25,7 +25,6 @@ pub struct StartOptions { pub async fn start( options: StartOptions, ) -> Result<(), Box> { - let port = if let Some(port) = options.port { port } else { @@ -34,15 +33,13 @@ pub async fn start( let addr = ([127, 0, 0, 1], port).into(); - - println!("🎨 Starting design server on port {}!!!", port); + println!("🎨 Starting design server on port {}!", port); let designer = DesignerService::new(options.config_context.clone(), options.project_io.clone()); let designer_server = DesignerServer::new(designer); let designer_server = tonic_web::config().enable(designer_server); let server = Server::bind(&addr).serve(make_service_fn(move |_| { - println!("request made"); let cors = warp::cors().allow_any_origin();