annotate src/lib/Config.luan @ 11:62dd23f0b549

start add server
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 29 Oct 2023 22:39:31 -0600
parents 43814e9f5802
children 80105b716a62
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local parse = Luan.parse or error()
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local Db = require "site:/lib/Db.luan"
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Config = {}
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local function get()
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local doc = Db.get_document("type:config")
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 return doc and parse(doc.config)
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 end
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 function Config.get()
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 return get() or error "config not set"
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 end
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 function Config.get_for_config()
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 local config = get() or {}
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 config.discord = config.discord or {}
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 config.discord.client_id = config.discord.client_id or "replace"
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 config.discord.client_secret = config.discord.client_secret or "replace"
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 return config
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 end
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 function Config.set(text)
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 parse(text) -- validate
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 Db.run_in_transaction( function()
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 local doc = Db.get_document("type:config") or {type="config"}
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 doc.config = text
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 Db.save(doc)
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 end )
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 end
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 return Config