vieter/src/server/server.v

58 lines
1.1 KiB
Coq
Raw Normal View History

2022-02-21 20:51:41 +01:00
module server
import web
import os
import log
import repo
2022-02-21 20:40:13 +01:00
import env
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:
conf env.ServerConfig [required; web_global]
2022-02-21 20:40:13 +01:00
pub mut:
repo repo.Repo [required; web_global]
// 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
}
// server starts the web server & starts listening for requests
pub fn server(conf env.ServerConfig) ? {
// 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.')
}
mut logger := log.Log{
level: log_level
}
2022-02-21 20:40:13 +01:00
logger.set_full_logpath(conf.log_file)
logger.log_to_console_too()
defer {
logger.info('Flushing log file')
logger.flush()
logger.close()
}
// This also creates the directories if needed
2022-02-21 20:40:13 +01:00
repo := repo.new(conf.repo_dir, conf.pkg_dir) or {
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.')
}
web.run(&App{
logger: logger
2022-02-21 20:40:13 +01:00
conf: conf
repo: repo
2022-02-21 20:51:41 +01:00
}, server.port)
}