forked from vieter-v/vieter
Switched to cli module; merged cli & vieter into single binary
This commit is contained in:
parent
09d0a40aae
commit
5b919ceeae
9 changed files with 167 additions and 65 deletions
16
src/server/cli.v
Normal file
16
src/server/cli.v
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
module server
|
||||
|
||||
import cli
|
||||
import env
|
||||
|
||||
pub fn cmd() cli.Command {
|
||||
return cli.Command{
|
||||
name: 'server'
|
||||
description: 'Start the Vieter server'
|
||||
execute: fn (cmd cli.Command) ? {
|
||||
conf := env.load<env.ServerConfig>() ?
|
||||
|
||||
server.server(conf) ?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,44 +3,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 +14,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 +34,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 +57,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 +73,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 +88,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.')
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import log
|
|||
import repo
|
||||
import env
|
||||
import util
|
||||
import cli
|
||||
|
||||
const port = 8000
|
||||
|
||||
|
|
@ -20,9 +21,7 @@ pub mut:
|
|||
}
|
||||
|
||||
// server starts the web server & starts listening for requests
|
||||
pub fn server() ? {
|
||||
conf := env.load<env.ServerConfig>() ?
|
||||
|
||||
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.')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue