2022-02-21 20:51:41 +01:00
|
|
|
module server
|
2022-02-17 22:00:46 +01:00
|
|
|
|
|
|
|
import web
|
|
|
|
import os
|
|
|
|
import log
|
|
|
|
import repo
|
2022-02-21 20:51:41 +01:00
|
|
|
import util
|
2022-02-21 20:40:13 +01:00
|
|
|
|
|
|
|
const port = 8000
|
|
|
|
|
|
|
|
struct App {
|
|
|
|
web.Context
|
|
|
|
pub:
|
2022-04-06 18:20:14 +02:00
|
|
|
conf Config [required; web_global]
|
2022-02-21 20:40:13 +01:00
|
|
|
pub mut:
|
2022-03-27 16:33:06 +02:00
|
|
|
repo repo.RepoGroupManager [required; web_global]
|
2022-02-21 22:22:36 +01:00
|
|
|
// This is used to claim the file lock on the repos file
|
|
|
|
git_mutex shared util.Dummy
|
2022-02-21 20:40:13 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 08:14:20 +01:00
|
|
|
// server starts the web server & starts listening for requests
|
2022-04-06 18:20:14 +02:00
|
|
|
pub fn server(conf Config) ? {
|
2022-04-07 13:47:06 +02:00
|
|
|
// Prevent using 'any' as the default arch
|
|
|
|
if conf.default_arch == 'any' {
|
|
|
|
util.exit_with_message(1, "'any' is not allowed as the value for default_arch.")
|
|
|
|
}
|
|
|
|
|
2022-02-17 22:00:46 +01:00
|
|
|
// Configure logger
|
2022-02-21 20:40:13 +01:00
|
|
|
log_level := log.level_from_tag(conf.log_level) or {
|
2022-02-21 20:51:41 +01:00
|
|
|
util.exit_with_message(1, 'Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.')
|
2022-02-17 22:00:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mut logger := log.Log{
|
|
|
|
level: log_level
|
|
|
|
}
|
|
|
|
|
2022-02-21 20:40:13 +01:00
|
|
|
logger.set_full_logpath(conf.log_file)
|
2022-02-17 22:00:46 +01:00
|
|
|
logger.log_to_console_too()
|
|
|
|
|
|
|
|
defer {
|
|
|
|
logger.info('Flushing log file')
|
|
|
|
logger.flush()
|
|
|
|
logger.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// This also creates the directories if needed
|
2022-04-07 13:47:06 +02:00
|
|
|
repo := repo.new(conf.data_dir, conf.pkg_dir, conf.default_arch) or {
|
2022-02-17 22:00:46 +01:00
|
|
|
logger.error(err.msg)
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
|
2022-02-21 20:51:41 +01:00
|
|
|
os.mkdir_all(conf.download_dir) or {
|
|
|
|
util.exit_with_message(1, 'Failed to create download directory.')
|
|
|
|
}
|
2022-02-17 22:00:46 +01:00
|
|
|
|
|
|
|
web.run(&App{
|
|
|
|
logger: logger
|
2022-02-21 20:40:13 +01:00
|
|
|
conf: conf
|
2022-02-17 22:00:46 +01:00
|
|
|
repo: repo
|
2022-02-21 20:51:41 +01:00
|
|
|
}, server.port)
|
2022-02-17 22:00:46 +01:00
|
|
|
}
|