forked from vieter-v/vieter
Merge branch 'dev' into multi-arch-repos-v2
This commit is contained in:
commit
a893577ade
15 changed files with 266 additions and 265 deletions
29
src/server/cli.v
Normal file
29
src/server/cli.v
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
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
|
||||
data_dir string
|
||||
repos_file string
|
||||
}
|
||||
|
||||
// cmd returns the cli submodule that handles starting the server
|
||||
pub fn cmd() cli.Command {
|
||||
return cli.Command{
|
||||
name: 'server'
|
||||
description: 'Start the Vieter server.'
|
||||
execute: fn (cmd cli.Command) ? {
|
||||
config_file := cmd.flags.get_string('config-file') ?
|
||||
conf := env.load<Config>(config_file) ?
|
||||
|
||||
server(conf) ?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,46 +1,10 @@
|
|||
module server
|
||||
|
||||
import web
|
||||
import os
|
||||
import json
|
||||
import git
|
||||
|
||||
const repos_file = 'repos.json'
|
||||
|
||||
pub struct GitRepo {
|
||||
pub:
|
||||
url string [required]
|
||||
branch string [required]
|
||||
}
|
||||
|
||||
fn read_repos(path string) ?[]GitRepo {
|
||||
if !os.exists(path) {
|
||||
mut f := os.create(path) ?
|
||||
|
||||
defer {
|
||||
f.close()
|
||||
}
|
||||
|
||||
f.write_string('[]') ?
|
||||
|
||||
return []
|
||||
}
|
||||
|
||||
content := os.read_file(path) ?
|
||||
res := json.decode([]GitRepo, content) ?
|
||||
return res
|
||||
}
|
||||
|
||||
fn write_repos(path string, repos []GitRepo) ? {
|
||||
mut f := os.create(path) ?
|
||||
|
||||
defer {
|
||||
f.close()
|
||||
}
|
||||
|
||||
value := json.encode(repos)
|
||||
f.write_string(value) ?
|
||||
}
|
||||
|
||||
['/api/repos'; get]
|
||||
fn (mut app App) get_repos() web.Result {
|
||||
if !app.is_authorized() {
|
||||
|
|
@ -48,7 +12,7 @@ fn (mut app App) get_repos() web.Result {
|
|||
}
|
||||
|
||||
repos := rlock app.git_mutex {
|
||||
read_repos(app.conf.repos_file) or {
|
||||
git.read_repos(app.conf.repos_file) or {
|
||||
app.lerror('Failed to read repos file.')
|
||||
|
||||
return app.server_error(500)
|
||||
|
|
@ -68,13 +32,13 @@ fn (mut app App) post_repo() web.Result {
|
|||
return app.server_error(400)
|
||||
}
|
||||
|
||||
new_repo := GitRepo{
|
||||
new_repo := git.GitRepo{
|
||||
url: app.query['url']
|
||||
branch: app.query['branch']
|
||||
}
|
||||
|
||||
mut repos := rlock app.git_mutex {
|
||||
read_repos(app.conf.repos_file) or {
|
||||
git.read_repos(app.conf.repos_file) or {
|
||||
app.lerror('Failed to read repos file.')
|
||||
|
||||
return app.server_error(500)
|
||||
|
|
@ -91,7 +55,7 @@ fn (mut app App) post_repo() web.Result {
|
|||
repos << new_repo
|
||||
|
||||
lock app.git_mutex {
|
||||
write_repos(app.conf.repos_file, repos) or { return app.server_error(500) }
|
||||
git.write_repos(app.conf.repos_file, repos) or { return app.server_error(500) }
|
||||
}
|
||||
|
||||
return app.ok('Repo added successfully.')
|
||||
|
|
@ -107,13 +71,13 @@ fn (mut app App) delete_repo() web.Result {
|
|||
return app.server_error(400)
|
||||
}
|
||||
|
||||
repo_to_remove := GitRepo{
|
||||
repo_to_remove := git.GitRepo{
|
||||
url: app.query['url']
|
||||
branch: app.query['branch']
|
||||
}
|
||||
|
||||
mut repos := rlock app.git_mutex {
|
||||
read_repos(app.conf.repos_file) or {
|
||||
git.read_repos(app.conf.repos_file) or {
|
||||
app.lerror('Failed to read repos file.')
|
||||
|
||||
return app.server_error(500)
|
||||
|
|
@ -122,7 +86,7 @@ fn (mut app App) delete_repo() web.Result {
|
|||
filtered := repos.filter(it != repo_to_remove)
|
||||
|
||||
lock app.git_mutex {
|
||||
write_repos(app.conf.repos_file, filtered) or { return app.server_error(500) }
|
||||
git.write_repos(app.conf.repos_file, filtered) or { return app.server_error(500) }
|
||||
}
|
||||
|
||||
return app.ok('Repo removed successfully.')
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import web
|
|||
import os
|
||||
import log
|
||||
import repo
|
||||
import env
|
||||
import util
|
||||
|
||||
const port = 8000
|
||||
|
|
@ -12,7 +11,7 @@ const port = 8000
|
|||
struct App {
|
||||
web.Context
|
||||
pub:
|
||||
conf env.ServerConfig [required; web_global]
|
||||
conf Config [required; web_global]
|
||||
pub mut:
|
||||
repo repo.RepoGroupManager [required; web_global]
|
||||
// This is used to claim the file lock on the repos file
|
||||
|
|
@ -20,9 +19,7 @@ pub mut:
|
|||
}
|
||||
|
||||
// server starts the web server & starts listening for requests
|
||||
pub fn server() ? {
|
||||
conf := env.load<env.ServerConfig>() ?
|
||||
|
||||
pub fn server(conf Config) ? {
|
||||
// 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.')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue