diff --git a/vieter/main.v b/vieter/main.v index 7d88ec5..1d86109 100644 --- a/vieter/main.v +++ b/vieter/main.v @@ -14,9 +14,8 @@ const db_name = 'pieter.db.tar.gz' struct App { web.Context - api_key string [required; web_global] - repo_dir string [required; web_global] - repo shared repo.Repo [required] + api_key string [required; web_global] + repo_dir string [required; web_global] } [noreturn] @@ -52,6 +51,40 @@ fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? { } } +['/pkgs/:pkg'; put] +fn (mut app App) put_package(pkg string) web.Result { + full_path := os.join_path_single(app.repo_dir, pkg) + + if os.exists(full_path) { + app.lwarn("Tried to upload duplicate package '$pkg'") + + return app.text('File already exists.') + } + + if length := app.req.header.get(.content_length) { + reader_to_file(mut app.reader, length.int(), full_path) or { + app.lwarn("Failed to upload package '$pkg'") + + return app.text('Failed to upload file.') + } + } else { + app.lwarn("Tried to upload package '$pkg' without specifying a Content-Length.") + return app.text("Content-Type header isn't set.") + } + + repo.add_package(os.join_path_single(app.repo_dir, db_name), full_path) or { + app.linfo("Failed to add package '$pkg' to database.") + + os.rm(full_path) or { println('Failed to remove $full_path') } + + return app.text('Failed to add package to repo.') + } + + app.linfo("Uploaded package '$pkg'.") + + return app.text('Package added successfully.') +} + fn main() { // Configure logger log_level_str := os.getenv_opt('LOG_LEVEL') or { 'WARN' } @@ -66,6 +99,9 @@ fn main() { logger.set_full_logpath(log_file) logger.log_to_console_too() + // logger.set + logger.debug('Logger set up.') + logger.flush() defer { logger.info('Flushing log file') @@ -85,15 +121,12 @@ fn main() { exit_with_message(2, "Failed to create repo directory '$repo_dir'.") } - logger.info("Created repo directory '$repo_dir'") + println("Repo directory '$repo_dir' created.") } web.run(&App{ - logger: logger api_key: key repo_dir: repo_dir - repo: repo.Repo{ - path: os.join_path_single(repo_dir, db_name) - } + logger: logger }, port) } diff --git a/vieter/repo.v b/vieter/repo.v index a090207..54dfc38 100644 --- a/vieter/repo.v +++ b/vieter/repo.v @@ -2,12 +2,8 @@ module repo import os -pub struct Repo { - path string -} - -pub fn (r Repo) add_package(pkg_path string) ? { - res := os.execute("repo-add '$r.path' '$pkg_path'") +pub fn add_package(db_path string, pkg_path string) ? { + res := os.execute("repo-add '$db_path' '$pkg_path'") if res.exit_code != 0 { println(res.output) diff --git a/vieter/routes.v b/vieter/routes.v deleted file mode 100644 index 2b14920..0000000 --- a/vieter/routes.v +++ /dev/null @@ -1,44 +0,0 @@ -module main - -import web -import os -import repo - -['/pkgs/:pkg'; put] -fn (mut app App) put_package(pkg string) web.Result { - full_path := os.join_path_single(app.repo_dir, pkg) - - if os.exists(full_path) { - app.lwarn("Tried to upload duplicate package '$pkg'") - - return app.text('File already exists.') - } - - if length := app.req.header.get(.content_length) { - app.ldebug("Uploading $length bytes to package '$pkg'") - reader_to_file(mut app.reader, length.int(), full_path) or { - app.lwarn("Failed to upload package '$pkg'") - - return app.text('Failed to upload file.') - } - } else { - app.lwarn("Tried to upload package '$pkg' without specifying a Content-Length.") - return app.text("Content-Type header isn't set.") - } - - lock app.repo { - app.repo.add_package(full_path) or { - app.linfo("Failed to add package '$pkg' to database.") - - os.rm(full_path) or { println('Failed to remove $full_path') } - - return app.text('Failed to add package to repo.') - } - - app.linfo("Added '$pkg' to repository.") - } - - app.linfo("Uploaded package '$pkg'.") - - return app.text('Package added successfully.') -}