-
Notifications
You must be signed in to change notification settings - Fork 4
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
5 changed files
with
51 additions
and
45 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
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::*; |
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,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()) | ||
} |
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