Skip to content

Commit

Permalink
➕ Use filepath
Browse files Browse the repository at this point in the history
  • Loading branch information
MystPi committed Feb 9, 2024
1 parent a6b7e05 commit cfb7144
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
1 change: 1 addition & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ gleam_http = "~> 3.5"
gleam_javascript = "~> 0.7"
marceau = "~> 1.1"
gleam_community_ansi = "~> 1.4"
filepath = "~> 1.0"

[dev-dependencies]
4 changes: 3 additions & 1 deletion manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# You typically do not need to edit this file

packages = [
{ name = "conversation", version = "1.4.3", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_http", "gleam_javascript"], otp_app = "conversation", source = "hex", outer_checksum = "908B46F60444442785A495197D482558AD8B849C3714A38FAA1940358CC8CCCD" },
{ name = "conversation", version = "1.4.3", build_tools = ["gleam"], requirements = ["gleam_javascript", "gleam_http", "gleam_stdlib"], otp_app = "conversation", source = "hex", outer_checksum = "908B46F60444442785A495197D482558AD8B849C3714A38FAA1940358CC8CCCD" },
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
{ name = "gleam_community_ansi", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "FE79E08BF97009729259B6357EC058315B6FBB916FAD1C2FF9355115FEB0D3A4" },
{ name = "gleam_community_colour", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "A49A5E3AE8B637A5ACBA80ECB9B1AFE89FD3D5351FF6410A42B84F666D40D7D5" },
{ name = "gleam_http", version = "3.5.3", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "C2FC3322203B16F897C1818D9810F5DEFCE347F0751F3B44421E1261277A7373" },
Expand All @@ -13,6 +14,7 @@ packages = [

[requirements]
conversation = { version = "~> 1.4" }
filepath = { version = "~> 1.0"}
gleam_community_ansi = { version = "~> 1.4" }
gleam_http = { version = "~> 3.5" }
gleam_javascript = { version = "~> 0.7" }
Expand Down
17 changes: 5 additions & 12 deletions src/glen.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import gleam/http/response.{
import gleam/javascript/promise.{type Promise}
import conversation
import marceau
import filepath
import gleam_community/ansi
import glen/status

Expand Down Expand Up @@ -188,8 +189,7 @@ pub fn bit_array_body(res: Response, bits: BitArray) -> Response {
pub fn file_body(res: Response, path: String) -> Response {
let content_type =
path
|> string.split(".")
|> list.last
|> filepath.extension
|> result.unwrap("")
|> string.lowercase
|> marceau.extension_to_mime_type
Expand Down Expand Up @@ -497,14 +497,6 @@ fn remove_preceeding_slashes(path: String) -> String {
}
}

fn join_path(a: String, b: String) -> String {
let b = remove_preceeding_slashes(b)
case string.ends_with(a, "/") {
True -> a <> b
False -> a <> "/" <> b
}
}

@external(javascript, "./ffi.mjs", "file_exists")
fn file_exists(path: String) -> Bool

Expand Down Expand Up @@ -533,8 +525,9 @@ pub fn static(
let path =
path
|> string.drop_left(string.length(prefix))
|> string.replace("..", "")
|> join_path(directory, _)
|> filepath.expand
|> result.unwrap("")
|> filepath.join(directory, _)

case file_exists(path) {
False -> next()
Expand Down

0 comments on commit cfb7144

Please sign in to comment.