Some logging improvements; fixed segfault

main
Jef Roosens 2022-01-11 18:19:41 +01:00
parent af86d91d3f
commit 3ada24ef3b
Signed by untrusted user: Jef Roosens
GPG Key ID: 955C0660072F691F
5 changed files with 51 additions and 25 deletions

View File

@ -2,6 +2,10 @@
run:
API_KEY=test REPO_DIR=data LOG_LEVEL=DEBUG v -cg run vieter
.PHONY: run-prod
run-prod:
API_KEY=test REPO_DIR=data LOG_LEVEL=DEBUG v -prod run vieter
.PHONY: watch
watch:
API_KEY=test REPO_DIR=data LOG_LEVEL=DEBUG v watch run vieter

View File

@ -1,9 +0,0 @@
module fibonacci
pub fn fib(i int) int {
if i <= 1 {
return i
}
return fib(i - 1) + fib(i - 2)
}

View File

@ -8,7 +8,7 @@ import repo
const port = 8000
const buf_size = 100_000
const buf_size = 1_000_000
const db_name = 'pieter.db.tar.gz'
@ -80,21 +80,23 @@ fn main() {
exit_with_message(1, 'No repo directory was configured.')
}
// We create the upload directory during startup
if !os.is_dir(repo_dir) {
os.mkdir_all(repo_dir) or {
exit_with_message(2, "Failed to create repo directory '$repo_dir'.")
repo := repo.Repo{
dir: repo_dir
name: db_name
}
logger.info("Created repo directory '$repo_dir'")
// We create the upload directory during startup
if !os.is_dir(repo.pkg_dir()) {
os.mkdir_all(repo.pkg_dir()) or {
exit_with_message(2, "Failed to create repo directory '$repo.pkg_dir()'.")
}
logger.info("Created package directory '$repo.pkg_dir()'.")
}
web.run(&App{
logger: logger
api_key: key
repo: repo.Repo{
dir: repo_dir
name: db_name
}
repo: repo
}, port)
}

View File

@ -4,16 +4,24 @@ import os
const pkgs_subpath = 'pkgs'
// Dummy struct to work around the fact that you can only share structs, maps &
// arrays
pub struct Dummy { x int }
// Handles management of a repository. Package files are stored in '$dir/pkgs'
// & moved there if necessary.
pub struct Repo {
mut:
mutex shared int = 0
mutex shared Dummy
pub:
dir string [required]
name string [required]
}
pub fn (r &Repo) pkg_dir() string {
return os.join_path_single(r.dir, pkgs_subpath)
}
// Returns path to the given package, prepended with the repo's path.
pub fn (r &Repo) pkg_path(pkg string) string {
return os.join_path(r.dir, pkgs_subpath, pkg)

View File

@ -3,11 +3,26 @@ module main
import web
import os
import repo
import time
const prefixes = ['B', 'KB', 'MB', 'GB']
fn pretty_bytes(bytes int) string {
mut i := 0
mut n := f32(bytes)
for n >= 1024 {
i++
n /= 1024
}
return '${n:.2}${prefixes[i]}'
}
['/pkgs/:pkg'; put]
fn (mut app App) put_package(pkg string) web.Result {
if app.repo.exists(pkg) {
app.lwarn("Tried to upload duplicate package '$pkg'")
app.lwarn("Duplicate package '$pkg'")
return app.text('File already exists.')
}
@ -15,20 +30,27 @@ fn (mut app App) put_package(pkg string) web.Result {
pkg_path := app.repo.pkg_path(pkg)
if length := app.req.header.get(.content_length) {
app.ldebug("Uploading $length bytes to package '$pkg'")
println(pkg_path)
app.ldebug("Uploading $length (${pretty_bytes(length.int())}) bytes to package '$pkg'.")
// This is used to time how long it takes to upload a file
mut sw := time.new_stopwatch(time.StopWatchOptions{auto_start: true})
reader_to_file(mut app.reader, length.int(), pkg_path) or {
app.lwarn("Failed to upload package '$pkg'")
return app.text('Failed to upload file.')
}
sw.stop()
app.ldebug("Upload of package '$pkg' completed in ${sw.elapsed().seconds():.3}s.")
} else {
app.lwarn("Tried to upload package '$pkg' without specifying a Content-Length.")
return app.text("Content-Type header isn't set.")
}
app.repo.add_package(pkg_path) or {
app.linfo("Failed to add package '$pkg' to database.")
app.lwarn("Failed to add package '$pkg' to database.")
os.rm(pkg_path) or { println('Failed to remove $pkg_path') }
@ -36,7 +58,6 @@ fn (mut app App) put_package(pkg string) web.Result {
}
app.linfo("Added '$pkg' to repository.")
app.linfo("Uploaded package '$pkg'.")
return app.text('Package added successfully.')
}