forked from vieter-v/vieter
Compare commits
2 Commits
09d1b5458f
...
e0d22b195e
| Author | SHA1 | Date |
|---|---|---|
|
|
e0d22b195e | |
|
|
dc98c910c4 |
|
|
@ -14,8 +14,9 @@ const db_name = 'pieter.db.tar.gz'
|
||||||
|
|
||||||
struct App {
|
struct App {
|
||||||
web.Context
|
web.Context
|
||||||
api_key string [required; web_global]
|
api_key string [required; web_global]
|
||||||
repo_dir string [required; web_global]
|
repo_dir string [required; web_global]
|
||||||
|
repo shared repo.Repo [required]
|
||||||
}
|
}
|
||||||
|
|
||||||
[noreturn]
|
[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() {
|
fn main() {
|
||||||
// Configure logger
|
// Configure logger
|
||||||
log_level_str := os.getenv_opt('LOG_LEVEL') or { 'WARN' }
|
log_level_str := os.getenv_opt('LOG_LEVEL') or { 'WARN' }
|
||||||
|
|
@ -99,9 +66,6 @@ fn main() {
|
||||||
|
|
||||||
logger.set_full_logpath(log_file)
|
logger.set_full_logpath(log_file)
|
||||||
logger.log_to_console_too()
|
logger.log_to_console_too()
|
||||||
// logger.set
|
|
||||||
logger.debug('Logger set up.')
|
|
||||||
logger.flush()
|
|
||||||
|
|
||||||
defer {
|
defer {
|
||||||
logger.info('Flushing log file')
|
logger.info('Flushing log file')
|
||||||
|
|
@ -121,12 +85,15 @@ fn main() {
|
||||||
exit_with_message(2, "Failed to create repo directory '$repo_dir'.")
|
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{
|
web.run(&App{
|
||||||
|
logger: logger
|
||||||
api_key: key
|
api_key: key
|
||||||
repo_dir: repo_dir
|
repo_dir: repo_dir
|
||||||
logger: logger
|
repo: repo.Repo{
|
||||||
|
path: os.join_path_single(repo_dir, db_name)
|
||||||
|
}
|
||||||
}, port)
|
}, port)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,12 @@ module repo
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
pub fn add_package(db_path string, pkg_path string) ? {
|
pub struct Repo {
|
||||||
res := os.execute("repo-add '$db_path' '$pkg_path'")
|
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 {
|
if res.exit_code != 0 {
|
||||||
println(res.output)
|
println(res.output)
|
||||||
|
|
|
||||||
|
|
@ -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.')
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue