aboutsummaryrefslogtreecommitdiff
path: root/src/request.gleam
blob: 96c950de7c85143cde222402275c34b90e051d89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import database
import gleam/int
import gleam/json
import gleam/list
import gleam/result
import gleam/string_builder
import svg
import wisp

const default_theme = "asoul"

const default_padding = 6

const max_padding = 32

fn middleware(request, handle) {
  let request = wisp.method_override(request)

  use <- wisp.log_request(request)
  use <- wisp.rescue_crashes
  use request <- wisp.handle_head(request)

  handle(request)
}

fn query_theme(query) -> String {
  list.key_find(query, "theme") |> result.unwrap(default_theme)
}

fn query_padding(query) -> Int {
  list.key_find(query, "padding")
  |> result.then(int.parse)
  |> result.map(int.clamp(_, min: 0, max: max_padding))
  |> result.unwrap(default_padding)
}

pub fn handle(request, connection, image_cache, index_html) {
  use _ <- middleware(request)

  case wisp.path_segments(request) {
    [] -> 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) -> {
          let query = wisp.get_query(request)

          wisp.ok()
          |> wisp.set_header("Content-Type", "image/svg+xml")
          |> wisp.string_body(svg.xml(
            image_cache,
            query_theme(query),
            counter.num,
            query_padding(query),
          ))
        }
        Error(_) -> wisp.unprocessable_entity()
      }
    }
    ["record", "@" <> name] if name == "" -> wisp.bad_request()
    ["record", "@" <> name] -> {
      case database.get_counter(connection, name) {
        Ok(counter) -> {
          wisp.json_response(
            json.to_string_builder(
              json.object([
                #("name", json.string(counter.name)),
                #("num", json.int(counter.num)),
                #("updated_at", json.string(counter.updated_at)),
                #("created_at", json.string(counter.created_at)),
              ]),
            ),
            200,
          )
        }
        Error(_) -> wisp.unprocessable_entity()
      }
    }
    _ -> wisp.redirect("https://github.com/Fuwn/mayu")
  }
}