Skip to content

Commit

Permalink
include headers
Browse files Browse the repository at this point in the history
  • Loading branch information
crcn committed Sep 27, 2022
1 parent f674815 commit dd4de4d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 45 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions libs/designer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion libs/designer/src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod res_body;
mod routes;
pub mod server;
mod service;
mod utils;
mod routes;
pub use server::*;
83 changes: 44 additions & 39 deletions libs/designer/src/server/routes.rs
Original file line number Diff line number Diff line change
@@ -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<Extract = (StaticFile,), Error = Rejection> + Clone {
let get_static_file = any()
.and(path_from_tail())
.and_then(static_file_reply);

pub fn routes() -> impl Filter<Extract = (StaticFile,), Error = Rejection> + 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<Extract = (String,), Error = Rejection> + Clone {
tail().and_then(move |tail: Tail| {
println!("tailing from file");
future::ready::<Result<String, Rejection>>(Ok(tail.as_str().to_string()))
})
fn path_from_tail() -> impl Filter<Extract = (String,), Error = Rejection> + Clone {
tail().and_then(move |tail: Tail| {
println!("tailing from file");
future::ready::<Result<String, Rejection>>(Ok(tail.as_str().to_string()))
})
}

pub async fn static_file_reply(path: String) -> Result<StaticFile, Rejection> {
let resolved_path = if path == "" {
"index.html".to_string()
} else {
path.to_string()
};

pub async fn static_file_reply(
path: String
) -> Result<StaticFile, Rejection> {
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())
}
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())
}
7 changes: 2 additions & 5 deletions libs/designer/src/server/server.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -25,7 +25,6 @@ pub struct StartOptions<IO: ProjectIO> {
pub async fn start<IO: ProjectIO + 'static>(
options: StartOptions<IO>,
) -> Result<(), Box<dyn std::error::Error>> {

let port = if let Some(port) = options.port {
port
} else {
Expand All @@ -34,15 +33,13 @@ pub async fn start<IO: ProjectIO + 'static>(

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();
Expand Down

0 comments on commit dd4de4d

Please sign in to comment.