diff --git a/Makefile b/Makefile index 76ab7b5..2238585 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,13 @@ dvieter: $(SOURCES) # Run the debug build inside gdb .PHONY: gdb gdb: dvieter - gdb --args './dvieter -f vieter.toml server' + VIETER_API_KEY=test \ + VIETER_DOWNLOAD_DIR=data/downloads \ + VIETER_REPO_DIR=data/repo \ + VIETER_PKG_DIR=data/pkgs \ + VIETER_LOG_LEVEL=DEBUG \ + VIETER_REPOS_FILE=data/repos.json \ + gdb --args ./dvieter # Optimised production build .PHONY: prod @@ -40,11 +46,22 @@ c: # Run the server in the default 'data' directory .PHONY: run run: vieter - ./vieter -f vieter.toml server + VIETER_API_KEY=test \ + VIETER_DOWNLOAD_DIR=data/downloads \ + VIETER_REPO_DIR=data/repo \ + VIETER_PKG_DIR=data/pkgs \ + VIETER_LOG_LEVEL=DEBUG \ + VIETER_REPOS_FILE=data/repos.json \ + ./vieter server .PHONY: run-prod run-prod: prod - ./pvieter -f vieter.toml server + VIETER_API_KEY=test \ + VIETER_DOWNLOAD_DIR=data/downloads \ + VIETER_REPO_DIR=data/repo \ + VIETER_PKG_DIR=data/pkgs \ + VIETER_LOG_LEVEL=DEBUG \ + ./pvieter server # =====OTHER===== .PHONY: lint diff --git a/src/build/build.v b/src/build/build.v index 934627f..6f2b0a8 100644 --- a/src/build/build.v +++ b/src/build/build.v @@ -3,6 +3,7 @@ module build import docker import encoding.base64 import time +import env import net.http import git import json @@ -61,7 +62,7 @@ fn create_build_image() ?string { return image.id } -fn build(conf Config) ? { +fn build(conf env.BuildConfig) ? { // We get the repos list from the Vieter instance mut req := http.new_request(http.Method.get, '$conf.address/api/repos', '') ? req.add_custom_header('X-Api-Key', conf.api_key) ? diff --git a/src/build/cli.v b/src/build/cli.v index c95d1ca..463d2ca 100644 --- a/src/build/cli.v +++ b/src/build/cli.v @@ -3,12 +3,6 @@ module build import cli import env -pub struct Config { -pub: - api_key string - address string -} - // cmd returns the cli submodule that handles the build process pub fn cmd() cli.Command { return cli.Command{ @@ -16,7 +10,7 @@ pub fn cmd() cli.Command { description: 'Run the build process.' execute: fn (cmd cli.Command) ? { config_file := cmd.flags.get_string('config-file') ? - conf := env.load(config_file) ? + conf := env.load(config_file) ? build(conf) ? } diff --git a/src/env.v b/src/env.v index 7b20765..6abd674 100644 --- a/src/env.v +++ b/src/env.v @@ -10,6 +10,23 @@ const prefix = 'VIETER_' // instead const file_suffix = '_FILE' +pub struct ServerConfig { +pub: + log_level string [default: WARN] + log_file string [default: 'vieter.log'] + pkg_dir string + download_dir string + api_key string + repo_dir string + repos_file string +} + +pub struct BuildConfig { +pub: + api_key string + address string +} + fn get_env_var(field_name string) ?string { env_var_name := '$env.prefix$field_name.to_upper()' env_file_name := '$env.prefix$field_name.to_upper()$env.file_suffix' @@ -48,18 +65,7 @@ pub fn load(path string) ?T { mut res := T{} if os.exists(path) { - // We don't use reflect here because reflect also sets any fields not - // in the toml back to "empty", which we don't want - doc := toml.parse_file(path) ? - - $for field in T.fields { - s := doc.value(field.name) - - // We currently only support strings - if s.type_name() == 'string' { - res.$(field.name) = s.string() - } - } + res = toml.parse_file(path) ?.reflect() } $for field in T.fields { @@ -74,7 +80,21 @@ pub fn load(path string) ?T { // If there's no value from the toml file either, we try to find a // default value else if res.$(field.name) == '' { - return error("Missing config variable '$field.name' with no provided default. Either add it to the config file or provide it using an environment variable.") + // We use the default instead, if it's present + mut default := '' + + for attr in field.attrs { + if attr.starts_with('default: ') { + default = attr[9..] + break + } + } + + if default == '' { + return error("Missing config variable '$field.name' with no provided default. Either add it to the config file or provide it using an environment variable.") + } + + res.$(field.name) = default } } } diff --git a/src/server/cli.v b/src/server/cli.v index 2383d9f..6e67686 100644 --- a/src/server/cli.v +++ b/src/server/cli.v @@ -3,17 +3,6 @@ module server import cli import env -struct Config { -pub: - log_level string = 'WARN' - log_file string = 'vieter.log' - pkg_dir string - download_dir string - api_key string - repo_dir string - repos_file string -} - // cmd returns the cli submodule that handles starting the server pub fn cmd() cli.Command { return cli.Command{ @@ -21,7 +10,7 @@ pub fn cmd() cli.Command { description: 'Start the Vieter server' execute: fn (cmd cli.Command) ? { config_file := cmd.flags.get_string('config-file') ? - conf := env.load(config_file) ? + conf := env.load(config_file) ? server(conf) ? } diff --git a/src/server/server.v b/src/server/server.v index ab2e46b..0738bc4 100644 --- a/src/server/server.v +++ b/src/server/server.v @@ -4,6 +4,7 @@ import web import os import log import repo +import env import util const port = 8000 @@ -11,7 +12,7 @@ const port = 8000 struct App { web.Context pub: - conf Config [required; web_global] + conf env.ServerConfig [required; web_global] pub mut: repo repo.Repo [required; web_global] // This is used to claim the file lock on the repos file @@ -19,7 +20,7 @@ pub mut: } // server starts the web server & starts listening for requests -pub fn server(conf Config) ? { +pub fn server(conf env.ServerConfig) ? { // Configure logger log_level := log.level_from_tag(conf.log_level) or { util.exit_with_message(1, 'Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.') diff --git a/vieter.toml b/vieter.toml deleted file mode 100644 index 3f95398..0000000 --- a/vieter.toml +++ /dev/null @@ -1,8 +0,0 @@ -# This file contains settings used during development -api_key = "test" -download_dir = "data/downloads" -repo_dir = "data/repo" -pkg_dir = "data/pkgs" -# log_level = "DEBUG" -repos_file = "data/repos.json" -