Skip to content

Instantly share code, notes, and snippets.

@felipesere
Created March 3, 2020 10:52
Show Gist options
  • Save felipesere/ff1b40810e2204638cfff2f9ca5fde0b to your computer and use it in GitHub Desktop.
Save felipesere/ff1b40810e2204638cfff2f9ca5fde0b to your computer and use it in GitHub Desktop.
let mut app = tide::with_state(state.clone());
app.middleware(RequestLogger::new());
/*
app.at("/").get(tide::redirect("/files/index.html"));
app.at("/files").strip_prefix().get(StaticFilesEndpoint {
root: "./tldr-github-parcel/dist".into(),
});
*/
let root_for_files = "./tldr-github-parcel/dist";
let dir = std::fs::read_dir(root_for_files)?;
for entry in dir.into_iter() {
let entry = entry?;
let path = entry.path();
if !path.is_dir() {
if let Some(file) = path.file_name() {
if file == "index.html" {
let protected = Rc::new(path);
let x = protected.clone();
app.at("/").get(|_req: Request<State>| async move {
let content = async_std::fs::read_to_string(&x).await.expect("Could not read index.html");
Response::new(200).body_string(content).set_header("Content-Type", "text/html")
});
}
}
}
}
app.at("/").get(|_req: Request<State>| async move {
let content = async_std::fs::read_to_string("./tldr-github-parcel/dist/index.html").await.expect("Could not read index.html");
Response::new(200).body_string(content).set_header("Content-Type", "text/html")
});
app.at("/main.dd4cbad9.css").get(|_req: Request<State>| async move {
let content = async_std::fs::read_to_string("./tldr-github-parcel/dist/main.dd4cbad9.css").await.expect("Could not read css");
Response::new(200).body_string(content).set_header("Content-Type", "text/css")
});
app.at("/tldr-github.e833be49.js").get(|_req: Request<State>| async move {
let content = async_std::fs::read_to_string("./tldr-github-parcel/dist/tldr-github.e833be49.js").await.expect("Could not read js");
Response::new(200).body_string(content).set_header("Content-Type", "application/javascript")
});
@felipesere
Copy link
Author

Compile error:

error[E0277]: the trait bound `std::rc::Rc<std::path::PathBuf>: std::convert::AsRef<async_std::path::path::Path>` is not satisfied
  --> src/main.rs:93:69
   |
93 |                         let content = async_std::fs::read_to_string(&x).await.expect("Could not read index.html");
   |                                                                     ^^ the trait `std::convert::AsRef<async_std::path::path::Path>` is not implemented for `std::rc::Rc<std::path::PathBuf>`
   |
   = help: the following implementations were found:
             <std::rc::Rc<T> as std::convert::AsRef<T>>
   = note: required because of the requirements on the impl of `std::convert::AsRef<async_std::path::path::Path>` for `&std::rc::Rc<std::path::PathBuf>`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment