Skip to content

Instantly share code, notes, and snippets.

@felipesere
Created December 2, 2019 14:08
Show Gist options
  • Save felipesere/1c9c9b8e80245724827289ca42bc1520 to your computer and use it in GitHub Desktop.
Save felipesere/1c9c9b8e80245724827289ca42bc1520 to your computer and use it in GitHub Desktop.
Trying to server static files from a subfolder that is given as part of a struct that implements `Endpoint`
pub struct StaticFilesV2 {
pub root: String,
}
pub(crate) type BoxFuture<'a, T> = std::pin::Pin<Box<dyn Future<Output = T> + Send + 'a>>;
impl <STATE: Send + Sync + 'static> Endpoint<STATE> for StaticFilesV2 {
type Fut = BoxFuture<'static, Response>;
fn call(&self, req: Request<STATE>) -> Self::Fut {
let protected_request = Arc::new(Mutex::new( (req, self.root.clone()) ));
Box::pin(async {
let (request, root) = &*protected_request.lock().await;
let filename: String = request.param("filename").unwrap();
log::warn!("The filename was: {}", filename);
let real_filename = &format!("{}/{}",root , filename);
match async_std::fs::read_to_string(&real_filename).await {
Ok(content) => {
let path = std::path::Path::new(&real_filename);
match path.extension() {
Some(extension) => {
let content_type = format!("text/{}", (*extension).to_str().unwrap());
Response::new(200).body_string(content).set_header("Content-Type", content_type)
},
None => Response::new(200).body_string(content),
}
},
Err(_) => Response::new(404),
}
})
}
}
@felipesere
Copy link
Author

Current compile error:

error[E0277]: `(dyn futures_io::if_std::AsyncBufRead + std::marker::Send + 'static)` cannot be shared between threads safely
  --> src/middleware/static_files.rs:30:9
   |
30 | /         Box::pin(async {
31 | |             let (request, root) = &*protected_request.lock().await;
32 | |
33 | |             let filename: String = request.param("filename").unwrap();
...  |
50 | |             }
51 | |         })
   | |__________^ `(dyn futures_io::if_std::AsyncBufRead + std::marker::Send + 'static)` cannot be shared between threads safely
   |
   = help: the trait `std::marker::Sync` is not implemented for `(dyn futures_io::if_std::AsyncBufRead + std::marker::Send + 'static)`
   = note: required because of the requirements on the impl of `std::marker::Sync` for `std::ptr::Unique<(dyn futures_io::if_std::AsyncBufRead + std::marker::Send + 'static)>`
   = note: required because it appears within the type `std::boxed::Box<(dyn futures_io::if_std::AsyncBufRead + std::marker::Send + 'static)>`
   = note: required because it appears within the type `std::pin::Pin<std::boxed::Box<(dyn futures_io::if_std::AsyncBufRead + std::marker::Send + 'static)>>`
   = note: required because it appears within the type `http_service::Body`
   = note: required because it appears within the type `http::request::Request<http_service::Body>`
   = note: required because it appears within the type `tide::request::Request<STATE>`
   = note: required because of the requirements on the impl of `std::marker::Send` for `&tide::request::Request<STATE>`
   = note: required because it appears within the type `for<'r, 's, 't0, 't1, 't2, 't3, 't4, 't5, 't6, 't7, 't8, 't9, 't10, 't11, 't12, 't13, 't14, 't15> {&'r async_std::sync::mutex::Mutex<(tide::request::Request<STATE>, std::string::String)>, std::sync::Arc<async_std::sync::mutex::Mutex<(tide::request::Request<STATE>, std::string::String)>>, impl core::future::future::Future, impl core::future::future::Future, (), &'t3 async_std::sync::mutex::MutexGuard<'t4, (tide::request::Request<STATE>, std::string::String)>, async_std::sync::mutex::MutexGuard<'t5, (tide::request::Request<STATE>, std::string::String)>, (tide::request::Request<STATE>, std::string::String), &'t6 tide::request::Request<STATE>, &'t7 std::string::String, std::string::String, std::string::String, &'t8 std::string::String, &'t9 std::string::String, &'t10 &'t11 std::string::String, impl core::future::future::Future, impl core::future::future::Future, ()}`
   = note: required because it appears within the type `[static generator@src/middleware/static_files.rs:30:24: 51:10 protected_request:&std::sync::Arc<async_std::sync::mutex::Mutex<(tide::request::Request<STATE>, std::string::String)>> for<'r, 's, 't0, 't1, 't2, 't3, 't4, 't5, 't6, 't7, 't8, 't9, 't10, 't11, 't12, 't13, 't14, 't15> {&'r async_std::sync::mutex::Mutex<(tide::request::Request<STATE>, std::string::String)>, std::sync::Arc<async_std::sync::mutex::Mutex<(tide::request::Request<STATE>, std::string::String)>>, impl core::future::future::Future, impl core::future::future::Future, (), &'t3 async_std::sync::mutex::MutexGuard<'t4, (tide::request::Request<STATE>, std::string::String)>, async_std::sync::mutex::MutexGuard<'t5, (tide::request::Request<STATE>, std::string::String)>, (tide::request::Request<STATE>, std::string::String), &'t6 tide::request::Request<STATE>, &'t7 std::string::String, std::string::String, std::string::String, &'t8 std::string::String, &'t9 std::string::String, &'t10 &'t11 std::string::String, impl core::future::future::Future, impl core::future::future::Future, ()}]`
   = note: required because it appears within the type `std::future::GenFuture<[static generator@src/middleware/static_files.rs:30:24: 51:10 protected_request:&std::sync::Arc<async_std::sync::mutex::Mutex<(tide::request::Request<STATE>, std::string::String)>> for<'r, 's, 't0, 't1, 't2, 't3, 't4, 't5, 't6, 't7, 't8, 't9, 't10, 't11, 't12, 't13, 't14, 't15> {&'r async_std::sync::mutex::Mutex<(tide::request::Request<STATE>, std::string::String)>, std::sync::Arc<async_std::sync::mutex::Mutex<(tide::request::Request<STATE>, std::string::String)>>, impl core::future::future::Future, impl core::future::future::Future, (), &'t3 async_std::sync::mutex::MutexGuard<'t4, (tide::request::Request<STATE>, std::string::String)>, async_std::sync::mutex::MutexGuard<'t5, (tide::request::Request<STATE>, std::string::String)>, (tide::request::Request<STATE>, std::string::String), &'t6 tide::request::Request<STATE>, &'t7 std::string::String, std::string::String, std::string::String, &'t8 std::string::String, &'t9 std::string::String, &'t10 &'t11 std::string::String, impl core::future::future::Future, impl core::future::future::Future, ()}]>`
   = note: required because it appears within the type `impl core::future::future::Future`
   = note: required for the cast to the object type `dyn core::future::future::Future<Output = tide::response::Response> + std::marker::Send`

error: aborting due to previous error

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