From: Taran Nathan Date: Fri, 12 Jun 2026 02:09:29 +0000 (-0400) Subject: add support for multiple passwords X-Git-Url: https://git.taranathan.com/?a=commitdiff_plain;p=filestuffs.git add support for multiple passwords --- diff --git a/src/filestuffs.gleam b/src/filestuffs.gleam index bf5670d..4f6fbb1 100644 --- a/src/filestuffs.gleam +++ b/src/filestuffs.gleam @@ -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( diff --git a/src/router.gleam b/src/router.gleam index fca1113..b62717f 100644 --- a/src/router.gleam +++ b/src/router.gleam @@ -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 " file <> "\">" <> file <> "
" + " file <> "\">" <> file <> "
" } 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), )