forked from vieter-v/vieter
Compare commits
No commits in common. "e13252d36826004f298a96c2ed4bb8e9df4ea61c" and "1d434db166918a19fee18c7d5e66c9c87e2ea9dd" have entirely different histories.
e13252d368
...
1d434db166
|
|
@ -18,6 +18,3 @@ test/
|
||||||
|
|
||||||
# V compiler directory
|
# V compiler directory
|
||||||
v/
|
v/
|
||||||
|
|
||||||
# gdb log file
|
|
||||||
gdb.txt
|
|
||||||
|
|
|
||||||
13
Makefile
13
Makefile
|
|
@ -16,17 +16,7 @@ vieter: $(SOURCES)
|
||||||
.PHONY: debug
|
.PHONY: debug
|
||||||
debug: dvieter
|
debug: dvieter
|
||||||
dvieter: $(SOURCES)
|
dvieter: $(SOURCES)
|
||||||
$(V_PATH) -showcc -keepc -cg -o dvieter $(SRC_DIR)
|
$(V) -keepc -cg -cc gcc -o dvieter $(SRC_DIR)
|
||||||
|
|
||||||
.PHONY: gdb
|
|
||||||
gdb: dvieter
|
|
||||||
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
|
# Optimised production build
|
||||||
.PHONY: prod
|
.PHONY: prod
|
||||||
|
|
@ -49,7 +39,6 @@ run: vieter
|
||||||
VIETER_REPO_DIR=data/repo \
|
VIETER_REPO_DIR=data/repo \
|
||||||
VIETER_PKG_DIR=data/pkgs \
|
VIETER_PKG_DIR=data/pkgs \
|
||||||
VIETER_LOG_LEVEL=DEBUG \
|
VIETER_LOG_LEVEL=DEBUG \
|
||||||
VIETER_REPOS_FILE=data/repos.json \
|
|
||||||
./vieter server
|
./vieter server
|
||||||
|
|
||||||
.PHONY: run-prod
|
.PHONY: run-prod
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
module server
|
module main
|
||||||
|
|
||||||
import net.http
|
import net.http
|
||||||
|
|
||||||
|
|
@ -6,7 +6,7 @@ import rand
|
||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import server
|
import git
|
||||||
import env
|
import env
|
||||||
|
|
||||||
const container_build_dir = '/build'
|
const container_build_dir = '/build'
|
||||||
|
|
@ -17,7 +17,7 @@ fn build() ? {
|
||||||
// Read in the repos from a json file
|
// Read in the repos from a json file
|
||||||
filename := os.join_path_single(conf.repo_dir, 'repos.json')
|
filename := os.join_path_single(conf.repo_dir, 'repos.json')
|
||||||
txt := os.read_file(filename) ?
|
txt := os.read_file(filename) ?
|
||||||
repos := json.decode([]server.GitRepo, txt) ?
|
repos := json.decode([]git.GitRepo, txt) ?
|
||||||
|
|
||||||
mut commands := [
|
mut commands := [
|
||||||
// Update repos & install required packages
|
// Update repos & install required packages
|
||||||
|
|
|
||||||
25
src/env.v
25
src/env.v
|
|
@ -11,25 +11,24 @@ const file_suffix = '_FILE'
|
||||||
|
|
||||||
pub struct ServerConfig {
|
pub struct ServerConfig {
|
||||||
pub:
|
pub:
|
||||||
log_level string [default: WARN]
|
log_level string [default: WARN]
|
||||||
log_file string [default: 'vieter.log']
|
log_file string [default: 'vieter.log']
|
||||||
pkg_dir string
|
pkg_dir string
|
||||||
download_dir string
|
download_dir string
|
||||||
api_key string
|
api_key string
|
||||||
repo_dir string
|
repo_dir string
|
||||||
repos_file string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct BuildConfig {
|
pub struct BuildConfig {
|
||||||
pub:
|
pub:
|
||||||
api_key string
|
api_key string
|
||||||
repo_dir string
|
repo_dir string
|
||||||
address 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 := '${prefix}${field_name.to_upper()}'
|
||||||
env_file_name := '$env.prefix$field_name.to_upper()$env.file_suffix'
|
env_file_name := '${prefix}${field_name.to_upper()}${file_suffix}'
|
||||||
env_var := os.getenv(env_var_name)
|
env_var := os.getenv(env_var_name)
|
||||||
env_file := os.getenv(env_file_name)
|
env_file := os.getenv(env_file_name)
|
||||||
|
|
||||||
|
|
@ -43,10 +42,7 @@ fn get_env_var(field_name string) ?string {
|
||||||
return error('Only one of $env_var_name or $env_file_name can be defined.')
|
return error('Only one of $env_var_name or $env_file_name can be defined.')
|
||||||
}
|
}
|
||||||
|
|
||||||
// If it's the env var itself, we return it.
|
// If it's the env var itself, we return it
|
||||||
// I'm pretty sure this also prevents variable ending in _FILE (e.g.
|
|
||||||
// VIETER_LOG_FILE) from being mistakingely read as an _FILE suffixed env
|
|
||||||
// var.
|
|
||||||
if env_var != '' {
|
if env_var != '' {
|
||||||
return env_var
|
return env_var
|
||||||
}
|
}
|
||||||
|
|
@ -80,5 +76,6 @@ pub fn load<T>() ?T {
|
||||||
default
|
default
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
module git
|
||||||
|
|
||||||
|
pub struct GitRepo {
|
||||||
|
pub:
|
||||||
|
url string [required]
|
||||||
|
branch string [required]
|
||||||
|
}
|
||||||
41
src/main.v
41
src/main.v
|
|
@ -1,17 +1,48 @@
|
||||||
module main
|
module main
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import server
|
import io
|
||||||
import util
|
|
||||||
|
[noreturn]
|
||||||
|
fn exit_with_message(code int, msg string) {
|
||||||
|
eprintln(msg)
|
||||||
|
exit(code)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? {
|
||||||
|
mut file := os.create(path) ?
|
||||||
|
defer {
|
||||||
|
file.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
mut buf := []byte{len: buf_size}
|
||||||
|
mut bytes_left := length
|
||||||
|
|
||||||
|
// Repeat as long as the stream still has data
|
||||||
|
for bytes_left > 0 {
|
||||||
|
// TODO check if just breaking here is safe
|
||||||
|
bytes_read := reader.read(mut buf) or { break }
|
||||||
|
bytes_left -= bytes_read
|
||||||
|
|
||||||
|
mut to_write := bytes_read
|
||||||
|
|
||||||
|
for to_write > 0 {
|
||||||
|
// TODO don't just loop infinitely here
|
||||||
|
bytes_written := file.write(buf[bytes_read - to_write..bytes_read]) or { continue }
|
||||||
|
|
||||||
|
to_write = to_write - bytes_written
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
if os.args.len == 1 {
|
if os.args.len == 1 {
|
||||||
util.exit_with_message(1, 'No action provided.')
|
exit_with_message(1, 'No action provided.')
|
||||||
}
|
}
|
||||||
|
|
||||||
match os.args[1] {
|
match os.args[1] {
|
||||||
'server' { server.server() ? }
|
'server' { server() ? }
|
||||||
'build' { build() ? }
|
'build' { build() ? }
|
||||||
else { util.exit_with_message(1, 'Unknown action: ${os.args[1]}') }
|
else { exit_with_message(1, 'Unknown action: ${os.args[1]}') }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,17 @@ module repo
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import package
|
import package
|
||||||
import util
|
|
||||||
|
// Dummy struct to work around the fact that you can only share structs, maps &
|
||||||
|
// arrays
|
||||||
|
pub struct Dummy {
|
||||||
|
x int
|
||||||
|
}
|
||||||
|
|
||||||
// This struct manages a single repository.
|
// This struct manages a single repository.
|
||||||
pub struct Repo {
|
pub struct Repo {
|
||||||
mut:
|
mut:
|
||||||
mutex shared util.Dummy
|
mutex shared Dummy
|
||||||
pub:
|
pub:
|
||||||
// Where to store repository files
|
// Where to store repository files
|
||||||
repo_dir string [required]
|
repo_dir string [required]
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,29 @@
|
||||||
module server
|
module main
|
||||||
|
|
||||||
import web
|
import web
|
||||||
import os
|
import os
|
||||||
import repo
|
import repo
|
||||||
import time
|
import time
|
||||||
import rand
|
import rand
|
||||||
import util
|
|
||||||
|
const prefixes = ['B', 'KB', 'MB', 'GB']
|
||||||
|
|
||||||
|
// pretty_bytes converts a byte count to human-readable version
|
||||||
|
fn pretty_bytes(bytes int) string {
|
||||||
|
mut i := 0
|
||||||
|
mut n := f32(bytes)
|
||||||
|
|
||||||
|
for n >= 1024 {
|
||||||
|
i++
|
||||||
|
n /= 1024
|
||||||
|
}
|
||||||
|
|
||||||
|
return '${n:.2}${prefixes[i]}'
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_pkg_name(s string) bool {
|
||||||
|
return s.contains('.pkg')
|
||||||
|
}
|
||||||
|
|
||||||
// healthcheck just returns a string, but can be used to quickly check if the
|
// healthcheck just returns a string, but can be used to quickly check if the
|
||||||
// server is still responsive.
|
// server is still responsive.
|
||||||
|
|
@ -46,12 +64,12 @@ fn (mut app App) put_package() web.Result {
|
||||||
pkg_path = os.join_path_single(app.conf.download_dir, rand.uuid_v4())
|
pkg_path = os.join_path_single(app.conf.download_dir, rand.uuid_v4())
|
||||||
}
|
}
|
||||||
|
|
||||||
app.ldebug("Uploading $length bytes (${util.pretty_bytes(length.int())}) to '$pkg_path'.")
|
app.ldebug("Uploading $length bytes (${pretty_bytes(length.int())}) to '$pkg_path'.")
|
||||||
|
|
||||||
// This is used to time how long it takes to upload a file
|
// This is used to time how long it takes to upload a file
|
||||||
mut sw := time.new_stopwatch(time.StopWatchOptions{ auto_start: true })
|
mut sw := time.new_stopwatch(time.StopWatchOptions{ auto_start: true })
|
||||||
|
|
||||||
util.reader_to_file(mut app.reader, length.int(), pkg_path) or {
|
reader_to_file(mut app.reader, length.int(), pkg_path) or {
|
||||||
app.lwarn("Failed to upload '$pkg_path'")
|
app.lwarn("Failed to upload '$pkg_path'")
|
||||||
|
|
||||||
return app.text('Failed to upload file.')
|
return app.text('Failed to upload file.')
|
||||||
|
|
@ -1,30 +1,29 @@
|
||||||
module server
|
module main
|
||||||
|
|
||||||
import web
|
import web
|
||||||
import os
|
import os
|
||||||
import log
|
import log
|
||||||
import repo
|
import repo
|
||||||
import env
|
import env
|
||||||
import util
|
|
||||||
|
|
||||||
const port = 8000
|
const port = 8000
|
||||||
|
|
||||||
|
const buf_size = 1_000_000
|
||||||
|
|
||||||
struct App {
|
struct App {
|
||||||
web.Context
|
web.Context
|
||||||
pub:
|
pub:
|
||||||
conf env.ServerConfig [required; web_global]
|
conf env.ServerConfig [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
|
|
||||||
git_mutex shared util.Dummy
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn server() ? {
|
fn server() ? {
|
||||||
conf := env.load<env.ServerConfig>() ?
|
conf := env.load<env.ServerConfig>() ?
|
||||||
|
|
||||||
// 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.')
|
exit_with_message(1, 'Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.')
|
||||||
}
|
}
|
||||||
|
|
||||||
mut logger := log.Log{
|
mut logger := log.Log{
|
||||||
|
|
@ -46,13 +45,11 @@ pub fn server() ? {
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
os.mkdir_all(conf.download_dir) or {
|
os.mkdir_all(conf.download_dir) or { exit_with_message(1, 'Failed to create download directory.') }
|
||||||
util.exit_with_message(1, 'Failed to create download directory.')
|
|
||||||
}
|
|
||||||
|
|
||||||
web.run(&App{
|
web.run(&App{
|
||||||
logger: logger
|
logger: logger
|
||||||
conf: conf
|
conf: conf
|
||||||
repo: repo
|
repo: repo
|
||||||
}, server.port)
|
}, port)
|
||||||
}
|
}
|
||||||
100
src/server/git.v
100
src/server/git.v
|
|
@ -1,100 +0,0 @@
|
||||||
module server
|
|
||||||
|
|
||||||
import web
|
|
||||||
import os
|
|
||||||
import json
|
|
||||||
|
|
||||||
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) ?
|
|
||||||
return json.decode([]GitRepo, content)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_repos(path string, repos []GitRepo) ? {
|
|
||||||
mut f := os.create(path) ?
|
|
||||||
|
|
||||||
defer {
|
|
||||||
f.close()
|
|
||||||
}
|
|
||||||
|
|
||||||
dump(repos)
|
|
||||||
value := json.encode(repos)
|
|
||||||
f.write_string(value) ?
|
|
||||||
}
|
|
||||||
|
|
||||||
['/api/repos'; get]
|
|
||||||
pub fn (mut app App) get_repos() web.Result {
|
|
||||||
if !app.is_authorized() {
|
|
||||||
return app.text('Unauthorized.')
|
|
||||||
}
|
|
||||||
|
|
||||||
repos := rlock app.git_mutex {
|
|
||||||
read_repos(app.conf.repos_file) or {
|
|
||||||
app.lerror('Failed to read repos file.')
|
|
||||||
|
|
||||||
return app.server_error(500)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return app.json(repos)
|
|
||||||
}
|
|
||||||
|
|
||||||
['/api/repos'; post]
|
|
||||||
pub fn (mut app App) post_repo() web.Result {
|
|
||||||
if !app.is_authorized() {
|
|
||||||
return app.text('Unauthorized.')
|
|
||||||
}
|
|
||||||
|
|
||||||
if !('url' in app.query && 'branch' in app.query) {
|
|
||||||
return app.server_error(400)
|
|
||||||
}
|
|
||||||
|
|
||||||
new_repo := GitRepo{
|
|
||||||
url: app.query['url']
|
|
||||||
branch: app.query['branch']
|
|
||||||
}
|
|
||||||
|
|
||||||
mut repos := rlock app.git_mutex {
|
|
||||||
read_repos(app.conf.repos_file) or {
|
|
||||||
app.lerror('Failed to read repos file.')
|
|
||||||
|
|
||||||
return app.server_error(500)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// We need to check for duplicates
|
|
||||||
for r in repos {
|
|
||||||
if r == new_repo {
|
|
||||||
return app.text('Duplicate repository.')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
repos << new_repo
|
|
||||||
|
|
||||||
lock app.git_mutex {
|
|
||||||
write_repos(app.conf.repos_file, repos) or {
|
|
||||||
return app.server_error(500)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return app.ok('Repo added successfully.')
|
|
||||||
}
|
|
||||||
57
src/util.v
57
src/util.v
|
|
@ -1,52 +1,9 @@
|
||||||
module util
|
module util
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import io
|
|
||||||
import crypto.md5
|
import crypto.md5
|
||||||
import crypto.sha256
|
import crypto.sha256
|
||||||
|
|
||||||
const reader_buf_size = 1_000_000
|
|
||||||
|
|
||||||
const prefixes = ['B', 'KB', 'MB', 'GB']
|
|
||||||
|
|
||||||
// Dummy struct to work around the fact that you can only share structs, maps &
|
|
||||||
// arrays
|
|
||||||
pub struct Dummy {
|
|
||||||
x int
|
|
||||||
}
|
|
||||||
|
|
||||||
[noreturn]
|
|
||||||
pub fn exit_with_message(code int, msg string) {
|
|
||||||
eprintln(msg)
|
|
||||||
exit(code)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? {
|
|
||||||
mut file := os.create(path) ?
|
|
||||||
defer {
|
|
||||||
file.close()
|
|
||||||
}
|
|
||||||
|
|
||||||
mut buf := []byte{len: util.reader_buf_size}
|
|
||||||
mut bytes_left := length
|
|
||||||
|
|
||||||
// Repeat as long as the stream still has data
|
|
||||||
for bytes_left > 0 {
|
|
||||||
// TODO check if just breaking here is safe
|
|
||||||
bytes_read := reader.read(mut buf) or { break }
|
|
||||||
bytes_left -= bytes_read
|
|
||||||
|
|
||||||
mut to_write := bytes_read
|
|
||||||
|
|
||||||
for to_write > 0 {
|
|
||||||
// TODO don't just loop infinitely here
|
|
||||||
bytes_written := file.write(buf[bytes_read - to_write..bytes_read]) or { continue }
|
|
||||||
|
|
||||||
to_write = to_write - bytes_written
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// hash_file returns the md5 & sha256 hash of a given file
|
// hash_file returns the md5 & sha256 hash of a given file
|
||||||
// TODO actually implement sha256
|
// TODO actually implement sha256
|
||||||
pub fn hash_file(path &string) ?(string, string) {
|
pub fn hash_file(path &string) ?(string, string) {
|
||||||
|
|
@ -75,17 +32,3 @@ pub fn hash_file(path &string) ?(string, string) {
|
||||||
|
|
||||||
return md5sum.checksum().hex(), sha256sum.checksum().hex()
|
return md5sum.checksum().hex(), sha256sum.checksum().hex()
|
||||||
}
|
}
|
||||||
|
|
||||||
// pretty_bytes converts a byte count to human-readable version
|
|
||||||
pub fn pretty_bytes(bytes int) string {
|
|
||||||
mut i := 0
|
|
||||||
mut n := f32(bytes)
|
|
||||||
|
|
||||||
for n >= 1024 {
|
|
||||||
i++
|
|
||||||
n /= 1024
|
|
||||||
}
|
|
||||||
|
|
||||||
return '${n:.2}${util.prefixes[i]}'
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue