server: config, migrations, redacted logging, /healthz entrypoint (#16)

This commit is contained in:
2026-11-14 21:40:39 +01:00
parent 143cb534dc
commit f376efdc39
9 changed files with 750 additions and 0 deletions
+52
View File
@@ -0,0 +1,52 @@
## fff-vtt entrypoint. M3 slice 1 (#16): config, state dir, SQLite + migration runner, GET /healthz,
## logging. Nothing game-specific yet; later slices add /join, /ws, /tiles here.
## The listen address is the constant config.BindAddress (127.0.0.1): nginx is the only public face.
import std/os
import mummy, mummy/routers
import config, db, eventlog
proc healthz(request: Request) {.gcsafe.} =
## Liveness only in slice 1. M5 makes it report the global lock as well (DESIGN 11.3).
var headers: HttpHeaders
headers["Content-Type"] = "application/json"
request.respond(200, headers, """{"status":"ok"}""")
proc buildRouter(): Router =
result.get("/healthz", healthz)
proc main() =
var cfg: Config
try:
cfg = parseConfig(commandLineParams())
except ConfigError as e:
stderr.writeLine "fff-vtt: " & e.msg
quit 2
if cfg.help:
stdout.write usage()
quit 0
let lg = newLogger()
lg.log(lvInfo, "server_start", [("origin", cfg.publicOrigin), ("state_dir", cfg.stateDir),
("bind", BindAddress)])
try:
createDir(cfg.stateDir)
except CatchableError as e:
lg.log(lvError, "state_dir_failed", [("path", cfg.stateDir), ("error", e.msg)])
quit 1
let conn = try: openDatabase(cfg.dbPath)
except CatchableError as e:
lg.log(lvError, "db_open_failed", [("path", cfg.dbPath), ("error", e.msg)])
quit 1
let applied = try: migrate(conn)
except CatchableError as e:
lg.log(lvError, "db_migrate_failed", [("path", cfg.dbPath), ("error", e.msg)])
quit 1
lg.log(lvInfo, "db_ready", [("path", cfg.dbPath), ("schema_version", $schemaVersion(conn)),
("applied", $applied)])
let server = newServer(buildRouter().toHandler())
lg.log(lvInfo, "listen", [("addr", BindAddress), ("port", $cfg.port)])
server.serve(Port(cfg.port), BindAddress)
when isMainModule:
main()