module main import vweb import sqlite import time import os struct App { vweb.Context pub mut: db sqlite.DB config shared Config is_admin bool } fn main() { mut app := &App{} mut app_config := Config{} 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() } app.db = sqlite.connect(app_config.db_path) or { println('Database error!') panic(err) } sql_code := app.create_tables() if sqlite.is_error(sql_code) { println('Could not create tables!') panic('Error code ' + sql_code.str()) } mut host := '::' if app_config.host != "" { host = app_config.host } // 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) } vweb.run_at(app, vweb.RunParams{ host: host port: app_config.port 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) }