aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2026-05-27 09:19:25 +0000
committerFuwn <[email protected]>2026-05-27 09:19:25 +0000
commit63f5166e3dcba0c884f112848a4a4e7886ca1b99 (patch)
tree3e35acf33f435c98dae1f73b73956d71e28ccd43
parentbuild: Replace Earthfile with Dockerfile (diff)
downloadmayu-63f5166e3dcba0c884f112848a4a4e7886ca1b99.tar.xz
mayu-63f5166e3dcba0c884f112848a4a4e7886ca1b99.zip
refactor: Tighten naming, drop dead paths, reject empty names
-rw-r--r--src/cache.gleam4
-rw-r--r--src/mayu.gleam4
-rw-r--r--src/request.gleam8
-rw-r--r--src/svg.gleam43
4 files changed, 28 insertions, 31 deletions
diff --git a/src/cache.gleam b/src/cache.gleam
index 7e4ff21..7cd40a5 100644
--- a/src/cache.gleam
+++ b/src/cache.gleam
@@ -2,7 +2,6 @@ import gleam/bit_array
import gleam/dict.{type Dict}
import gleam/int
import gleam/list
-import gleam/option.{type Option}
import gleam/result
import gleam/string
import image
@@ -85,8 +84,7 @@ fn load_cached_image(path) {
}
}
-pub fn get_image(cache, theme, digit) -> Option(CachedImage) {
+pub fn get_image(cache, theme, digit) -> Result(CachedImage, Nil) {
dict.get(cache, theme)
|> result.then(fn(theme_images) { dict.get(theme_images, digit) })
- |> option.from_result
}
diff --git a/src/mayu.gleam b/src/mayu.gleam
index bb05b77..0d9fcf9 100644
--- a/src/mayu.gleam
+++ b/src/mayu.gleam
@@ -29,8 +29,8 @@ 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, index_html)
+ fn(incoming_request) {
+ request.handle(incoming_request, connection, image_cache, index_html)
},
secret_key_base,
)
diff --git a/src/request.gleam b/src/request.gleam
index a56780b..eae8bdd 100644
--- a/src/request.gleam
+++ b/src/request.gleam
@@ -20,13 +20,10 @@ pub fn handle(request, connection, image_cache, index_html) {
use _ <- middleware(request)
case wisp.path_segments(request) {
- [] ->
- case index_html {
- "" -> wisp.not_found()
- content -> wisp.html_response(string_builder.from_string(content), 200)
- }
+ [] -> wisp.html_response(string_builder.from_string(index_html), 200)
["heart-beat"] ->
wisp.html_response(string_builder.from_string("alive"), 200)
+ ["get", "@" <> name] if name == "" -> wisp.bad_request()
["get", "@" <> name] -> {
case database.get_counter(connection, name) {
Ok(counter) -> {
@@ -56,6 +53,7 @@ pub fn handle(request, connection, image_cache, index_html) {
Error(_) -> wisp.unprocessable_entity()
}
}
+ ["record", "@" <> name] if name == "" -> wisp.bad_request()
["record", "@" <> name] -> {
case database.get_counter(connection, name) {
Ok(counter) -> {
diff --git a/src/svg.gleam b/src/svg.gleam
index 4014910..22706dd 100644
--- a/src/svg.gleam
+++ b/src/svg.gleam
@@ -1,7 +1,6 @@
import cache
import gleam/int
import gleam/list
-import gleam/option.{Some}
import gleam/string_builder.{type StringBuilder}
import image
@@ -9,19 +8,21 @@ type XmlImages {
XmlImages(xml: StringBuilder, width: Int, height: Int)
}
-fn append_image(svgs, base64, image: image.ImageInformation, width) {
- svgs
- |> string_builder.append("<image height=\"")
- |> string_builder.append(int.to_string(image.height))
- |> string_builder.append("\" width=\"")
- |> string_builder.append(int.to_string(image.width))
- |> string_builder.append("\" x=\"")
- |> string_builder.append(int.to_string(width))
- |> string_builder.append("\" y=\"0\" xlink:href=\"data:image/")
- |> string_builder.append(image.extension)
- |> string_builder.append(";base64,")
- |> string_builder.append(base64)
- |> string_builder.append("\"/>")
+fn append_image(svgs, base64, image: image.ImageInformation, x_offset) {
+ string_builder.append(
+ svgs,
+ "<image height=\""
+ <> int.to_string(image.height)
+ <> "\" width=\""
+ <> int.to_string(image.width)
+ <> "\" x=\""
+ <> int.to_string(x_offset)
+ <> "\" y=\"0\" xlink:href=\"data:image/"
+ <> image.extension
+ <> ";base64,"
+ <> base64
+ <> "\"/>",
+ )
}
fn images(image_cache, theme, digits, width, height, svgs) {
@@ -29,14 +30,14 @@ fn images(image_cache, theme, digits, width, height, svgs) {
[] -> XmlImages(svgs, width, height)
[digit, ..rest] ->
case cache.get_image(image_cache, theme, digit) {
- Some(cached) ->
+ Ok(cached_image) ->
images(
image_cache,
theme,
rest,
- width + cached.info.width,
- int.max(height, cached.info.height),
- append_image(svgs, cached.base64, cached.info, width),
+ width + cached_image.info.width,
+ int.max(height, cached_image.info.height),
+ append_image(svgs, cached_image.base64, cached_image.info, width),
)
_ -> images(image_cache, theme, rest, width, height, svgs)
}
@@ -52,9 +53,9 @@ pub fn xml(image_cache, theme, number, padding) {
let assert Ok(digits) = int.digits(int.absolute_value(number), 10)
let digits_padding = padding - list.length(digits)
- case digits_padding {
- n if n > 0 -> list.concat([list.repeat(0, digits_padding), digits])
- _ -> digits
+ case digits_padding > 0 {
+ True -> list.append(list.repeat(0, digits_padding), digits)
+ False -> digits
}
},
0,