diff --git a/vieter/main.v b/vieter/main.v index 1d86109..7d88ec5 100644 --- a/vieter/main.v +++ b/vieter/main.v @@ -14,8 +14,9 @@ const db_name = 'pieter.db.tar.gz' struct App { web.Context - api_key string [required; web_global] - repo_dir string [required; web_global] + api_key string [required; web_global] + repo_dir string [required; web_global] + repo shared repo.Repo [required] } [noreturn] @@ -51,40 +52,6 @@ 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' } @@ -99,9 +66,6 @@ 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') @@ -121,12 +85,15 @@ fn main() { exit_with_message(2, "Failed to create repo directory '$repo_dir'.") } - println("Repo directory '$repo_dir' created.") + logger.info("Created repo directory '$repo_dir'") } web.run(&App{ + logger: logger api_key: key repo_dir: repo_dir - logger: logger + repo: repo.Repo{ + path: os.join_path_single(repo_dir, db_name) + } }, port) } diff --git a/vieter/repo.v b/vieter/repo.v index 54dfc38..a090207 100644 --- a/vieter/repo.v +++ b/vieter/repo.v @@ -2,8 +2,12 @@ module repo import os -pub fn add_package(db_path string, pkg_path string) ? { - res := os.execute("repo-add '$db_path' '$pkg_path'") +pub struct Repo { + path string +} + +pub fn (r Repo) add_package(pkg_path string) ? { + res := os.execute("repo-add '$r.path' '$pkg_path'") if res.exit_code != 0 { println(res.output) diff --git a/vieter/routes.v b/vieter/routes.v new file mode 100644 index 0000000..2b14920 --- /dev/null +++ b/vieter/routes.v @@ -0,0 +1,44 @@ +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.') +}