Browse Source

Attempt to add CORS

This does not work!
Apollo 9 months ago
parent
commit
11f9dc9a6c
1 changed files with 29 additions and 2 deletions
  1. 29 2
      src/main.rs

+ 29 - 2
src/main.rs

@@ -3,8 +3,34 @@ use std::{env, os::unix::fs::MetadataExt, path::PathBuf};
 use std::fs::read_dir;
 use humansize::{format_size, DECIMAL};
 
+use rocket::http::Header;
+use rocket::{Request, Response};
+use rocket::fairing::{Fairing, Info, Kind};
+
+pub struct Cors;
+
+#[rocket::async_trait]
+impl Fairing for Cors {
+    fn info(&self) -> Info {
+        Info {
+            name: "Cross-Origin-Resource-Sharing Middleware",
+            kind: Kind::Response,
+        }
+    }
+    async fn on_response<'r>(&self, _request: &'r Request<'_>, response: &mut Response<'r>) {
+        response.set_header(Header::new(
+            "access-control-allow-origin",
+            "http://127.0.0.1",
+        ));
+        response.set_header(Header::new(
+            "access-control-allow-methods",
+            "GET, PATCH, OPTIONS",
+        ));
+    }
+}
+
 #[get("/ls/<path..>")]
-fn list_dir(mut path: PathBuf) -> Value {
+fn _list_dir(mut path: PathBuf) -> Value {
     if path.to_string_lossy().is_empty() {
         path = PathBuf::from(".");
     }
@@ -80,7 +106,8 @@ fn rocket() -> _ {
 
     rock
         .configure(fig)
+        .attach(Cors)
         .mount("/", FileServer::from("."))
         // This is a DEBUG only feature, it gives the option to list the given directory
-        //.mount("/", routes![list_dir])
+        //.mount("/", routes![_list_dir])
 }