aboutsummaryrefslogtreecommitdiff
path: root/src/mayu.gleam
blob: bb05b7739982ee1128f7b821adff301df52524c8 (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
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 assert Ok(index_html_source) = simplifile.read("index.html")
  let index_html =
    string.replace(index_html_source, "{{ MAYU_VERSION }}", version_tag)

  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()
}