⭐ Moe-Counter Compatible Website Hit Counter Written in Gleam mayu.due.moe
hit-counter svg moe
at main 94 lines 2.5 kB view raw
1import database 2import envoy 3import gleam/int 4import gleam/json 5import gleam/list 6import gleam/string 7import gleam/string_builder 8import svg 9import wisp 10 11fn middleware(request, handle) { 12 let request = wisp.method_override(request) 13 14 use <- wisp.log_request(request) 15 use <- wisp.rescue_crashes 16 use request <- wisp.handle_head(request) 17 18 handle(request) 19} 20 21pub fn handle(request, connection, image_cache, index_html) { 22 use _ <- middleware(request) 23 24 case wisp.path_segments(request) { 25 [] -> 26 case index_html { 27 "" -> wisp.not_found() 28 content -> 29 wisp.html_response( 30 string_builder.from_string( 31 string.replace( 32 content, 33 "{{ MAYU_VERSION }}", 34 case envoy.get("MAYU_VERSION") { 35 Ok(version) -> "(v" <> version <> ")" 36 Error(_) -> "" 37 }, 38 ), 39 ), 40 200, 41 ) 42 } 43 ["heart-beat"] -> 44 wisp.html_response(string_builder.from_string("alive"), 200) 45 ["get", "@" <> name] -> { 46 case database.get_counter(connection, name) { 47 Ok(counter) -> { 48 let query = wisp.get_query(request) 49 50 wisp.ok() 51 |> wisp.set_header("Content-Type", "image/svg+xml") 52 |> wisp.string_body( 53 svg.xml( 54 image_cache, 55 case list.key_find(query, "theme") { 56 Ok(theme) -> theme 57 _ -> "asoul" 58 }, 59 counter.num, 60 case list.key_find(query, "padding") { 61 Ok(padding) -> 62 case int.parse(padding) { 63 Ok(n) -> n 64 Error(_) -> 6 65 } 66 _ -> 6 67 }, 68 ), 69 ) 70 } 71 Error(_) -> wisp.unprocessable_entity() 72 } 73 } 74 ["record", "@" <> name] -> { 75 case database.get_counter(connection, name) { 76 Ok(counter) -> { 77 wisp.json_response( 78 json.to_string_builder( 79 json.object([ 80 #("name", json.string(counter.name)), 81 #("num", json.int(counter.num)), 82 #("updated_at", json.string(counter.updated_at)), 83 #("created_at", json.string(counter.created_at)), 84 ]), 85 ), 86 200, 87 ) 88 } 89 Error(_) -> wisp.unprocessable_entity() 90 } 91 } 92 _ -> wisp.redirect("https://github.com/Fuwn/mayu") 93 } 94}