main.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. use rocket::{fs::FileServer, launch, routes};
  2. use std::env;
  3. use rocket::http::Header;
  4. use rocket::{Request, Response};
  5. use rocket::fairing::{Fairing, Info, Kind};
  6. pub struct Cors;
  7. #[rocket::async_trait]
  8. impl Fairing for Cors {
  9. fn info(&self) -> Info {
  10. Info {
  11. name: "Cross-Origin-Resource-Sharing Middleware",
  12. kind: Kind::Response,
  13. }
  14. }
  15. async fn on_response<'r>(&self, _request: &'r Request<'_>, response: &mut Response<'r>) {
  16. response.set_header(Header::new(
  17. "access-control-allow-origin",
  18. "http://127.0.0.1",
  19. ));
  20. response.set_header(Header::new(
  21. "access-control-allow-methods",
  22. "GET, PATCH, OPTIONS",
  23. ));
  24. }
  25. }
  26. #[cfg(feature="debug")]
  27. mod debug_extras {
  28. use rocket::{get, routes, serde::json::{json, Value}, Route};
  29. use std::path::PathBuf;
  30. use std::fs::read_dir;
  31. use humansize::{format_size, DECIMAL};
  32. #[get("/ls/<path..>")]
  33. fn _list_dir(mut path: PathBuf) -> Value {
  34. if path.to_string_lossy().is_empty() {
  35. path = PathBuf::from(".");
  36. }
  37. let dir = read_dir(path);
  38. match dir {
  39. Ok(ls) => {
  40. let mut v: Vec<Value> = Vec::new();
  41. for elem in ls {
  42. if elem.is_err() {
  43. continue;
  44. }
  45. let e = elem.unwrap();
  46. let p = e.path();
  47. let name = e.file_name().to_string_lossy().to_string();
  48. if p.is_file() {
  49. let meta = e.metadata().unwrap();
  50. let size = meta.len();
  51. if name.starts_with(".") {
  52. v.push(json!({"name": name.clone(), "type": "FILE", "size": format_size(size, DECIMAL), "hidden": true}));
  53. } else {
  54. v.push(json!({"name": name.clone(), "type": "FILE", "size": format_size(size, DECIMAL)}));
  55. }
  56. } else if p.is_symlink() {
  57. if name.starts_with(".") {
  58. v.push(json!({"name": name.clone(), "type": "LINK", "hidden": true}));
  59. } else {
  60. v.push(json!({"name": name.clone(), "type": "LINK"}));
  61. }
  62. } else {
  63. if name.starts_with(".") {
  64. v.push(json!({"name": name.clone(), "type": "DIR", "hidden": true}))
  65. } else {
  66. v.push(json!({"name": name.clone(), "type": "DIR"}))
  67. }
  68. }
  69. }
  70. return json!({"status": 200, "dir": v});
  71. }
  72. Err(_) => {
  73. return json!({"status": 404})
  74. }
  75. }
  76. }
  77. pub fn extras(rock: &mut Vec<Route>) {
  78. rock.append(&mut routes![_list_dir]);
  79. }
  80. }
  81. #[cfg(not(feature="debug"))]
  82. mod debug_extras {
  83. use rocket::Route;
  84. pub fn extras(_rock: &mut Vec<Route>) {}
  85. }
  86. #[launch]
  87. fn rocket() -> _ {
  88. let mut args: Vec<String> = Vec::new();
  89. for a in env::args() {
  90. args.push(a);
  91. }
  92. let mut port: u16 = 9000;
  93. if args.len() != 1 {
  94. let mut index = 0;
  95. for a in args.iter() {
  96. let ptry: Result<u16, _> = a.parse();
  97. match ptry {
  98. Ok(val) => {
  99. port = val;
  100. break;
  101. }
  102. Err(err) => {
  103. println!("arg #{} : {}", index, err);
  104. }
  105. }
  106. index += 1;
  107. }
  108. }
  109. println!("Launching @ 127.0.0.1:{}", port);
  110. let rock = rocket::build();
  111. let fig = rock.figment().clone()
  112. .merge((rocket::Config::PORT, port));
  113. let mut r = routes![];
  114. // This is a DEBUG only feature, it gives the option to list the given directory
  115. if cfg!(feature="debug") {
  116. debug_extras::extras(&mut r);
  117. }
  118. rock
  119. .configure(fig)
  120. .attach(Cors)
  121. .mount("/", FileServer::from("."))
  122. .mount("/", r)
  123. }