]> git.taranathan.com Git - filestuffs.git/commitdiff
add support for multiple passwords main
authorTaran Nathan <moogoesmeow123@gmail.com>
Fri, 12 Jun 2026 02:09:29 +0000 (22:09 -0400)
committerTaran Nathan <moogoesmeow123@gmail.com>
Fri, 12 Jun 2026 02:11:25 +0000 (22:11 -0400)
src/filestuffs.gleam
src/router.gleam

index bf5670ddca57d8f1eb2e25f472f0135b967aa56d..4f6fbb16c4b42aa81c561fed136fe8eb1acc38eb 100644 (file)
@@ -13,7 +13,7 @@ pub fn main() -> Nil {
   let args: List(String) = argv.load().arguments
   case args {
     ["greet", name] -> io.println("hello " <> name <> "!")
-    ["web", file_path, "-p", password] -> web(file_path, password)
+    ["web", file_path, "-p", ..passwords] -> web(file_path, passwords)
     _ -> io.println("bad args")
   }
 
@@ -24,7 +24,7 @@ pub fn wassup() -> Int {
   42
 }
 
-fn web(file_path: String, password: String) -> Nil {
+fn web(file_path: String, passwords: List(String)) -> Nil {
   wisp.configure_logger()
 
   let secret_key_base =
@@ -33,7 +33,7 @@ fn web(file_path: String, password: String) -> Nil {
     })
 
   let assert Ok(_) =
-    router.handle_request(_, file_path, password)
+    router.handle_request(_, file_path, passwords)
     |> wisp_mist.handler(secret_key_base)
     |> mist.new
     |> mist.port(
index fca111320a304d66eb79b7cdd1cc0b52e08023e5..b62717fc73fc46aed52d056216b8fb91f82ad3bc 100644 (file)
@@ -8,7 +8,7 @@ import wisp
 pub fn handle_request(
   req: wisp.Request,
   file_path: String,
-  password: String,
+  passwords: List(String),
 ) -> wisp.Response {
   use req <- middleware(req)
   // use <- wisp.serve_static(req, "/files", file_path)
@@ -21,7 +21,7 @@ pub fn handle_request(
       use <- wisp.serve_static(req, "/styles.css", "./static/styles.css")
       wisp.ok()
     }
-    "/new/" <> _path -> upload(req, password)
+    "/new/" <> _path -> upload(req, passwords)
     "/list" -> list(req, file_path)
     "/files/" <> path -> files(req, "/" <> path, file_path)
     _ -> wisp.not_found()
@@ -53,8 +53,7 @@ fn list(req: wisp.Request, file_path: String) -> wisp.Response {
 }
 
 fn html_linkify(file: String) -> String {
-  echo file
-  echo "<a href=\"/files/" <> file <> "\">" <> file <> "</a><br>"
+  "<a href=\"/files/" <> file <> "\">" <> file <> "</a><br>"
 }
 
 fn files(req: wisp.Request, path: String, file_path) -> wisp.Response {
@@ -63,12 +62,15 @@ fn files(req: wisp.Request, path: String, file_path) -> wisp.Response {
   wisp.ok()
 }
 
-pub fn upload(req: wisp.Request, password: String) -> wisp.Response {
+pub fn upload(req: wisp.Request, passwords: List(String)) -> wisp.Response {
   use form <- wisp.require_form(req)
 
   //early return if incorrect password
+  let input_password = result.unwrap(list.key_find(form.values, "password"), "")
   use <- bool.guard(
-    when: result.unwrap(list.key_find(form.values, "password"), "") != password,
+    when: !list.any(passwords, fn(password) -> Bool {
+      input_password == password
+    }),
     return: wisp.response(401),
   )