aboutsummaryrefslogtreecommitdiff
path: root/src/mayu.gleam
blob: 441887d7f51ba5388b26cbed2c070384a3fab322 (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
import cache
import database
import envoy
import gleam/erlang/process
import gleam/string
import mist
import request
import simplifile
import sqlight
import wisp

pub fn main() {
  wisp.configure_logger()

  let _ = simplifile.create_directory("./data")
  let image_cache = cache.load_themes()
  let version_tag = case envoy.get("MAYU_VERSION") {
    Ok(version) -> "(v" <> version <> ")"
    Error(_) -> ""
  }
  let index_html = case simplifile.read("index.html") {
    Ok(content) -> string.replace(content, "{{ MAYU_VERSION }}", version_tag)
    Error(_) -> {
      wisp.log_error("Failed to read index.html")

      ""
    }
  }

  use connection <- sqlight.with_connection("./data/count.db")

  database.setup(connection)

  let secret_key_base = wisp.random_string(64)
  let assert Ok(_) =
    wisp.mist_handler(
      fn(request) {
        request.handle(request, connection, image_cache, index_html)
      },
      secret_key_base,
    )
    |> mist.new
    |> mist.port(3000)
    |> mist.start_http

  process.sleep_forever()
}