diff options
| author | Fuwn <[email protected]> | 2026-01-14 03:29:38 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-01-14 03:29:38 +0000 |
| commit | b707f714ca36012d69cd513c901ed68f365f49c1 (patch) | |
| tree | 4f598eadffd94298647c25d603fe2e725a8af66f /src/request.gleam | |
| parent | chore(gleam.toml): Bump version (diff) | |
| download | mayu-b707f714ca36012d69cd513c901ed68f365f49c1.tar.xz mayu-b707f714ca36012d69cd513c901ed68f365f49c1.zip | |
perf: Optimise database queries and reduce I/O operations
Diffstat (limited to 'src/request.gleam')
| -rw-r--r-- | src/request.gleam | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/src/request.gleam b/src/request.gleam index c66e66d..5ef74c1 100644 --- a/src/request.gleam +++ b/src/request.gleam @@ -5,7 +5,6 @@ import gleam/json import gleam/list import gleam/string import gleam/string_builder -import simplifile import svg import wisp @@ -19,13 +18,14 @@ fn middleware(request, handle) { handle(request) } -pub fn handle(request, connection, image_cache) { +pub fn handle(request, connection, image_cache, index_html) { use _ <- middleware(request) case wisp.path_segments(request) { [] -> - case simplifile.read("index.html") { - Ok(content) -> + case index_html { + "" -> wisp.not_found() + content -> wisp.html_response( string_builder.from_string( string.replace( @@ -39,24 +39,25 @@ pub fn handle(request, connection, image_cache) { ), 200, ) - Error(_) -> wisp.not_found() } ["heart-beat"] -> wisp.html_response(string_builder.from_string("alive"), 200) ["get", "@" <> name] -> { case database.get_counter(connection, name) { Ok(counter) -> { + let query = wisp.get_query(request) + wisp.ok() |> wisp.set_header("Content-Type", "image/svg+xml") |> wisp.string_body( svg.xml( image_cache, - case list.key_find(wisp.get_query(request), "theme") { + case list.key_find(query, "theme") { Ok(theme) -> theme _ -> "asoul" }, counter.num, - case list.key_find(wisp.get_query(request), "padding") { + case list.key_find(query, "padding") { Ok(padding) -> case int.parse(padding) { Ok(n) -> n |