highscore-server/src/main.v

66 lines
1.2 KiB
Coq
Raw Normal View History

2023-01-07 13:00:00 +01:00
module main
import vweb
import sqlite
import time
import os
2023-01-07 13:00:00 +01:00
struct App {
vweb.Context
pub mut:
2023-01-11 19:08:24 +01:00
db sqlite.DB
config shared Config
2023-01-07 13:00:00 +01:00
is_admin bool
}
fn main() {
mut app := &App{}
mut app_config := Config{}
2023-01-07 13:00:00 +01:00
lock app.config {
app.config = load_config()
app_config = app.config.copy()
}
if !os.exists(app_config.db_path) {
println('Creating database file at ' + app_config.db_path)
mut file := os.create(app_config.db_path) or { panic(err) }
file.close()
}
2023-01-07 13:00:00 +01:00
app.db = sqlite.connect(app_config.db_path) or {
println('Database error!')
2023-01-07 13:00:00 +01:00
panic(err)
}
sql_code := app.create_tables()
2023-01-07 13:00:00 +01:00
if sqlite.is_error(sql_code) {
println('Could not create tables!')
panic('Error code ' + sql_code.str())
}
2023-01-07 13:00:00 +01:00
mut host := '::'
2023-01-11 19:08:24 +01:00
if app_config.host != '' {
host = app_config.host
2023-01-07 13:00:00 +01:00
}
// Handle graceful shutdown for Docker
os.signal_opt(os.Signal.int, app.shutdown) or { panic(err) }
os.signal_opt(os.Signal.term, app.shutdown) or { panic(err) }
2023-01-07 13:00:00 +01:00
vweb.run_at(app, vweb.RunParams{
host: host
port: app_config.port
2023-01-07 13:00:00 +01:00
family: .ip6
}) or { panic(err) }
}
fn (mut app App) shutdown(sig os.Signal) {
app.db.close() or { panic(err) }
println('Shut down database gracefully')
println('Exiting...')
time.sleep(1e+9) // Sleep one second
exit(0)
}