⭐ Moe-Counter Compatible Website Hit Counter Written in Gleam mayu.due.moe
hit-counter svg moe
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor(request): straightforward pattern matching

Fuwn d475dde9 e828ac94

+34 -29
+4 -4
src/database.gleam
··· 67 67 68 68 pub fn get_counter(connection, name) { 69 69 case name { 70 - "demo" -> Counter("demo", 0_123_456_789, "", "") 70 + "demo" -> Ok(Counter("demo", 0_123_456_789, "", "")) 71 71 _ -> { 72 72 check_error( 73 73 sqlight.query( ··· 102 102 ) 103 103 { 104 104 Ok([first_element]) -> { 105 - Counter( 105 + Ok(Counter( 106 106 first_element.0, 107 107 first_element.1, 108 108 first_element.2, 109 109 first_element.3, 110 - ) 110 + )) 111 111 } 112 - _ -> Counter(name, 0, "", "") 112 + _ -> Error("Unreachable entity") 113 113 } 114 114 } 115 115 }
+30 -25
src/request.gleam
··· 20 20 case wisp.path_segments(request) { 21 21 ["heart-beat"] -> 22 22 wisp.html_response(string_builder.from_string("alive"), 200) 23 - ["get", "@" <> name] -> 24 - wisp.ok() 25 - |> wisp.set_header("Content-Type", "image/svg+xml") 26 - |> wisp.string_body(svg.xml( 27 - case wisp.get_query(request) { 28 - [#("theme", theme)] -> theme 29 - _ -> "asoul" 30 - }, 31 - database.get_counter(connection, name).num, 32 - )) 23 + ["get", "@" <> name] -> { 24 + case database.get_counter(connection, name) { 25 + Ok(counter) -> { 26 + wisp.ok() 27 + |> wisp.set_header("Content-Type", "image/svg+xml") 28 + |> wisp.string_body(svg.xml( 29 + case wisp.get_query(request) { 30 + [#("theme", theme)] -> theme 31 + _ -> "asoul" 32 + }, 33 + counter.num, 34 + )) 35 + } 36 + Error(_) -> wisp.unprocessable_entity() 37 + } 38 + } 33 39 ["record", "@" <> name] -> { 34 - let counter = database.get_counter(connection, name) 35 - 36 - case 37 - Ok( 38 - json.to_string_builder( 39 - json.object([ 40 - #("name", json.string(counter.name)), 41 - #("num", json.int(counter.num)), 42 - #("updated_at", json.string(counter.updated_at)), 43 - #("created_at", json.string(counter.created_at)), 44 - ]), 45 - ), 46 - ) 47 - { 48 - Ok(builder) -> wisp.json_response(builder, 200) 40 + case database.get_counter(connection, name) { 41 + Ok(counter) -> { 42 + wisp.json_response( 43 + json.to_string_builder( 44 + json.object([ 45 + #("name", json.string(counter.name)), 46 + #("num", json.int(counter.num)), 47 + #("updated_at", json.string(counter.updated_at)), 48 + #("created_at", json.string(counter.created_at)), 49 + ]), 50 + ), 51 + 200, 52 + ) 53 + } 49 54 Error(_) -> wisp.unprocessable_entity() 50 55 } 51 56 }