forked from vieter-v/vieter
Compare commits
2 Commits
b70be0574e
...
c656e672e2
| Author | SHA1 | Date |
|---|---|---|
|
|
c656e672e2 | |
|
|
2aa2aa143c |
23
Makefile
23
Makefile
|
|
@ -23,13 +23,7 @@ dvieter: $(SOURCES)
|
||||||
# Run the debug build inside gdb
|
# Run the debug build inside gdb
|
||||||
.PHONY: gdb
|
.PHONY: gdb
|
||||||
gdb: dvieter
|
gdb: dvieter
|
||||||
VIETER_API_KEY=test \
|
gdb --args './dvieter -f vieter.toml server'
|
||||||
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
|
# Optimised production build
|
||||||
.PHONY: prod
|
.PHONY: prod
|
||||||
|
|
@ -46,22 +40,11 @@ c:
|
||||||
# Run the server in the default 'data' directory
|
# Run the server in the default 'data' directory
|
||||||
.PHONY: run
|
.PHONY: run
|
||||||
run: vieter
|
run: vieter
|
||||||
VIETER_API_KEY=test \
|
./vieter -f vieter.toml server
|
||||||
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
|
.PHONY: run-prod
|
||||||
run-prod: prod
|
run-prod: prod
|
||||||
VIETER_API_KEY=test \
|
./pvieter -f vieter.toml server
|
||||||
VIETER_DOWNLOAD_DIR=data/downloads \
|
|
||||||
VIETER_REPO_DIR=data/repo \
|
|
||||||
VIETER_PKG_DIR=data/pkgs \
|
|
||||||
VIETER_LOG_LEVEL=DEBUG \
|
|
||||||
./pvieter server
|
|
||||||
|
|
||||||
# =====OTHER=====
|
# =====OTHER=====
|
||||||
.PHONY: lint
|
.PHONY: lint
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ module build
|
||||||
import docker
|
import docker
|
||||||
import encoding.base64
|
import encoding.base64
|
||||||
import time
|
import time
|
||||||
import env
|
|
||||||
import net.http
|
import net.http
|
||||||
import git
|
import git
|
||||||
import json
|
import json
|
||||||
|
|
@ -62,7 +61,7 @@ fn create_build_image() ?string {
|
||||||
return image.id
|
return image.id
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build(conf env.BuildConfig) ? {
|
fn build(conf Config) ? {
|
||||||
// We get the repos list from the Vieter instance
|
// We get the repos list from the Vieter instance
|
||||||
mut req := http.new_request(http.Method.get, '$conf.address/api/repos', '') ?
|
mut req := http.new_request(http.Method.get, '$conf.address/api/repos', '') ?
|
||||||
req.add_custom_header('X-Api-Key', conf.api_key) ?
|
req.add_custom_header('X-Api-Key', conf.api_key) ?
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,12 @@ module build
|
||||||
import cli
|
import cli
|
||||||
import env
|
import env
|
||||||
|
|
||||||
|
pub struct Config {
|
||||||
|
pub:
|
||||||
|
api_key string
|
||||||
|
address string
|
||||||
|
}
|
||||||
|
|
||||||
// cmd returns the cli submodule that handles the build process
|
// cmd returns the cli submodule that handles the build process
|
||||||
pub fn cmd() cli.Command {
|
pub fn cmd() cli.Command {
|
||||||
return cli.Command{
|
return cli.Command{
|
||||||
|
|
@ -10,7 +16,7 @@ pub fn cmd() cli.Command {
|
||||||
description: 'Run the build process.'
|
description: 'Run the build process.'
|
||||||
execute: fn (cmd cli.Command) ? {
|
execute: fn (cmd cli.Command) ? {
|
||||||
config_file := cmd.flags.get_string('config-file') ?
|
config_file := cmd.flags.get_string('config-file') ?
|
||||||
conf := env.load<env.BuildConfig>(config_file) ?
|
conf := env.load<Config>(config_file) ?
|
||||||
|
|
||||||
build(conf) ?
|
build(conf) ?
|
||||||
}
|
}
|
||||||
|
|
|
||||||
46
src/env.v
46
src/env.v
|
|
@ -10,23 +10,6 @@ const prefix = 'VIETER_'
|
||||||
// instead
|
// instead
|
||||||
const file_suffix = '_FILE'
|
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 {
|
fn get_env_var(field_name string) ?string {
|
||||||
env_var_name := '$env.prefix$field_name.to_upper()'
|
env_var_name := '$env.prefix$field_name.to_upper()'
|
||||||
env_file_name := '$env.prefix$field_name.to_upper()$env.file_suffix'
|
env_file_name := '$env.prefix$field_name.to_upper()$env.file_suffix'
|
||||||
|
|
@ -65,7 +48,18 @@ pub fn load<T>(path string) ?T {
|
||||||
mut res := T{}
|
mut res := T{}
|
||||||
|
|
||||||
if os.exists(path) {
|
if os.exists(path) {
|
||||||
res = toml.parse_file(path) ?.reflect<T>()
|
// 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()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$for field in T.fields {
|
$for field in T.fields {
|
||||||
|
|
@ -80,21 +74,7 @@ pub fn load<T>(path string) ?T {
|
||||||
// If there's no value from the toml file either, we try to find a
|
// If there's no value from the toml file either, we try to find a
|
||||||
// default value
|
// default value
|
||||||
else if res.$(field.name) == '' {
|
else if res.$(field.name) == '' {
|
||||||
// We use the default instead, if it's present
|
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.")
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,17 @@ module server
|
||||||
import cli
|
import cli
|
||||||
import env
|
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
|
// cmd returns the cli submodule that handles starting the server
|
||||||
pub fn cmd() cli.Command {
|
pub fn cmd() cli.Command {
|
||||||
return cli.Command{
|
return cli.Command{
|
||||||
|
|
@ -10,7 +21,7 @@ pub fn cmd() cli.Command {
|
||||||
description: 'Start the Vieter server'
|
description: 'Start the Vieter server'
|
||||||
execute: fn (cmd cli.Command) ? {
|
execute: fn (cmd cli.Command) ? {
|
||||||
config_file := cmd.flags.get_string('config-file') ?
|
config_file := cmd.flags.get_string('config-file') ?
|
||||||
conf := env.load<env.ServerConfig>(config_file) ?
|
conf := env.load<Config>(config_file) ?
|
||||||
|
|
||||||
server(conf) ?
|
server(conf) ?
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ import web
|
||||||
import os
|
import os
|
||||||
import log
|
import log
|
||||||
import repo
|
import repo
|
||||||
import env
|
|
||||||
import util
|
import util
|
||||||
|
|
||||||
const port = 8000
|
const port = 8000
|
||||||
|
|
@ -12,7 +11,7 @@ const port = 8000
|
||||||
struct App {
|
struct App {
|
||||||
web.Context
|
web.Context
|
||||||
pub:
|
pub:
|
||||||
conf env.ServerConfig [required; web_global]
|
conf Config [required; web_global]
|
||||||
pub mut:
|
pub mut:
|
||||||
repo repo.Repo [required; web_global]
|
repo repo.Repo [required; web_global]
|
||||||
// This is used to claim the file lock on the repos file
|
// This is used to claim the file lock on the repos file
|
||||||
|
|
@ -20,7 +19,7 @@ pub mut:
|
||||||
}
|
}
|
||||||
|
|
||||||
// server starts the web server & starts listening for requests
|
// server starts the web server & starts listening for requests
|
||||||
pub fn server(conf env.ServerConfig) ? {
|
pub fn server(conf Config) ? {
|
||||||
// Configure logger
|
// Configure logger
|
||||||
log_level := log.level_from_tag(conf.log_level) or {
|
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.')
|
util.exit_with_message(1, 'Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.')
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
# 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"
|
||||||
|
|
||||||
Loading…
Reference in New Issue