From 3ada24ef3bcd58ff76a6752cb417789489768683 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Tue, 11 Jan 2022 18:19:41 +0100 Subject: [PATCH] Some logging improvements; fixed segfault --- Makefile | 4 ++++ vieter/fibonacci.v | 9 --------- vieter/main.v | 22 ++++++++++++---------- vieter/repo.v | 10 +++++++++- vieter/routes.v | 31 ++++++++++++++++++++++++++----- 5 files changed, 51 insertions(+), 25 deletions(-) delete mode 100644 vieter/fibonacci.v diff --git a/Makefile b/Makefile index 6c67733..a48a802 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/vieter/fibonacci.v b/vieter/fibonacci.v deleted file mode 100644 index c76d44d..0000000 --- a/vieter/fibonacci.v +++ /dev/null @@ -1,9 +0,0 @@ -module fibonacci - -pub fn fib(i int) int { - if i <= 1 { - return i - } - - return fib(i - 1) + fib(i - 2) -} diff --git a/vieter/main.v b/vieter/main.v index 8c2c38f..e11e2b8 100644 --- a/vieter/main.v +++ b/vieter/main.v @@ -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) } diff --git a/vieter/repo.v b/vieter/repo.v index 8d1acbd..55b38f3 100644 --- a/vieter/repo.v +++ b/vieter/repo.v @@ -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) diff --git a/vieter/routes.v b/vieter/routes.v index 7537040..3dfb1ce 100644 --- a/vieter/routes.v +++ b/vieter/routes.v @@ -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.') }