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 | |
| parent | chore(gleam.toml): Bump version (diff) | |
| download | mayu-b707f714ca36012d69cd513c901ed68f365f49c1.tar.xz mayu-b707f714ca36012d69cd513c901ed68f365f49c1.zip | |
perf: Optimise database queries and reduce I/O operations
| -rw-r--r-- | src/database.gleam | 21 | ||||
| -rw-r--r-- | src/mayu.gleam | 12 | ||||
| -rw-r--r-- | src/request.gleam | 15 |
3 files changed, 25 insertions, 23 deletions
diff --git a/src/database.gleam b/src/database.gleam index f13d91e..07264fd 100644 --- a/src/database.gleam +++ b/src/database.gleam @@ -1,7 +1,5 @@ -import birl import gleam/dynamic import gleam/option -import gleam/string import sqlight import wisp @@ -21,7 +19,6 @@ pub fn setup(connection) { ) strict;", connection, ) - let add_column = fn(name) { let _ = sqlight.exec( @@ -38,12 +35,6 @@ pub fn setup(connection) { Nil } -fn sqlite_now() { - birl.to_iso8601(birl.utc_now()) - |> string.slice(0, 19) - |> string.replace("T", " ") -} - pub fn get_counter(connection, name) { case name { "demo" -> Ok(Counter("demo", 1_234_567_890, "", "")) @@ -51,12 +42,12 @@ pub fn get_counter(connection, name) { case sqlight.query( "INSERT INTO tb_count (name, created_at, updated_at, num) - VALUES (?1, ?2, ?2, 1) - ON CONFLICT(name) DO UPDATE SET - num = tb_count.num + 1, - updated_at = excluded.updated_at - RETURNING name, num, created_at, updated_at;", - with: [sqlight.text(name), sqlight.text(sqlite_now())], + VALUES (?1, datetime('now'), datetime('now'), 1) + ON CONFLICT(name) DO UPDATE SET + num = tb_count.num + 1, + updated_at = datetime('now') + RETURNING name, num, created_at, updated_at;", + with: [sqlight.text(name)], on: connection, expecting: dynamic.tuple4( dynamic.string, diff --git a/src/mayu.gleam b/src/mayu.gleam index 2c3d78e..b204dc0 100644 --- a/src/mayu.gleam +++ b/src/mayu.gleam @@ -12,6 +12,14 @@ pub fn main() { let _ = simplifile.create_directory("./data") let image_cache = cache.load_themes() + let index_html = case simplifile.read("index.html") { + Ok(content) -> content + Error(_) -> { + wisp.log_error("Failed to read index.html") + + "" + } + } use connection <- sqlight.with_connection("./data/count.db") @@ -20,7 +28,9 @@ pub fn main() { let secret_key_base = wisp.random_string(64) let assert Ok(_) = wisp.mist_handler( - fn(request) { request.handle(request, connection, image_cache) }, + fn(request) { + request.handle(request, connection, image_cache, index_html) + }, secret_key_base, ) |> mist.new 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 |