2023-01-07 13:00:00 +01:00
|
|
|
module main
|
|
|
|
|
|
|
|
import vweb
|
|
|
|
import sqlite
|
2023-01-10 11:00:00 +01:00
|
|
|
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{}
|
2023-01-10 11:00:00 +01:00
|
|
|
mut app_config := Config{}
|
2023-01-07 13:00:00 +01:00
|
|
|
|
2023-01-10 11:00:00 +01:00
|
|
|
lock app.config {
|
|
|
|
app.config = load_config()
|
|
|
|
app_config = app.config.copy()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !os.exists(app_config.db_path) {
|
2023-01-11 19:00:00 +01:00
|
|
|
println('Creating database file at ' + app_config.db_path)
|
2023-01-10 11:00:00 +01:00
|
|
|
mut file := os.create(app_config.db_path) or { panic(err) }
|
|
|
|
file.close()
|
|
|
|
}
|
2023-01-07 13:00:00 +01:00
|
|
|
|
2023-01-10 11: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)
|
|
|
|
}
|
|
|
|
|
2023-01-10 11:00:00 +01:00
|
|
|
sql_code := app.create_tables()
|
2023-01-07 13:00:00 +01:00
|
|
|
|
2023-01-10 11: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
|
|
|
|
2023-01-10 11:00:00 +01:00
|
|
|
mut host := '::'
|
2023-01-11 19:08:24 +01:00
|
|
|
if app_config.host != '' {
|
2023-01-10 11:00:00 +01:00
|
|
|
host = app_config.host
|
2023-01-07 13:00:00 +01:00
|
|
|
}
|
|
|
|
|
2023-01-10 11: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
|
2023-01-10 11:00:00 +01:00
|
|
|
port: app_config.port
|
2023-01-07 13:00:00 +01:00
|
|
|
family: .ip6
|
|
|
|
}) or { panic(err) }
|
|
|
|
}
|
2023-01-10 11:00:00 +01:00
|
|
|
|
|
|
|
fn (mut app App) shutdown(sig os.Signal) {
|
|
|
|
app.db.close() or { panic(err) }
|
2023-01-11 19:00:00 +01:00
|
|
|
println('Shut down database gracefully')
|
|
|
|
println('Exiting...')
|
2023-01-10 11:00:00 +01:00
|
|
|
time.sleep(1e+9) // Sleep one second
|
|
|
|
exit(0)
|
|
|
|
}
|