Made repo safe to use concurrently

main
Jef Roosens 2022-01-11 14:40:25 +01:00
parent dc98c910c4
commit e0d22b195e
Signed by: Jef Roosens
GPG Key ID: 955C0660072F691F
3 changed files with 22 additions and 10 deletions

View File

@ -4,6 +4,7 @@ import web
import os import os
import log import log
import io import io
import repo
const port = 8000 const port = 8000
@ -15,6 +16,7 @@ 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]
@ -87,8 +89,11 @@ fn main() {
} }
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)
} }

View File

@ -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)

View File

@ -26,7 +26,8 @@ fn (mut app App) put_package(pkg string) web.Result {
return app.text("Content-Type header isn't set.") return app.text("Content-Type header isn't set.")
} }
repo.add_package(os.join_path_single(app.repo_dir, db_name), full_path) or { lock app.repo {
app.repo.add_package(full_path) or {
app.linfo("Failed to add package '$pkg' to database.") app.linfo("Failed to add package '$pkg' to database.")
os.rm(full_path) or { println('Failed to remove $full_path') } os.rm(full_path) or { println('Failed to remove $full_path') }
@ -34,8 +35,10 @@ fn (mut app App) put_package(pkg string) web.Result {
return app.text('Failed to add package to repo.') return app.text('Failed to add package to repo.')
} }
app.linfo("Added '$pkg' to repository.")
}
app.linfo("Uploaded package '$pkg'.") app.linfo("Uploaded package '$pkg'.")
return app.text('Package added successfully.') return app.text('Package added successfully.')
} }