feat(server): repo POST requests now return information

main
Jef Roosens 2022-09-11 21:24:29 +02:00
parent 210508f1ee
commit b6cd2f0bc2
Signed by untrusted user: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 26 additions and 39 deletions

View File

@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Build containers now explicitely set the PATH variable * Build containers now explicitely set the PATH variable
* Refactor of web framework * Refactor of web framework
* API endpoints now return id of newly created entries * API endpoints now return id of newly created entries
* Repo POST requests now return information on published package
### Removed ### Removed

View File

@ -23,8 +23,9 @@ pub:
pub struct RepoAddResult { pub struct RepoAddResult {
pub: pub:
added bool [required] name string
pkg &package.Pkg [required] version string
archs []string
} }
// new creates a new RepoGroupManager & creates the directories as needed // new creates a new RepoGroupManager & creates the directories as needed
@ -53,10 +54,10 @@ pub fn (r &RepoGroupManager) add_pkg_from_path(repo string, pkg_path string) ?Re
return error('Failed to read package file: $err.msg()') return error('Failed to read package file: $err.msg()')
} }
added := r.add_pkg_in_repo(repo, pkg)? archs := r.add_pkg_in_repo(repo, pkg)?
// If the add was successful, we move the file to the packages directory // If the add was successful, we move the file to the packages directory
for arch in added { for arch in archs {
repo_pkg_path := os.real_path(os.join_path(r.pkg_dir, repo, arch)) repo_pkg_path := os.real_path(os.join_path(r.pkg_dir, repo, arch))
dest_path := os.join_path_single(repo_pkg_path, pkg.filename()) dest_path := os.join_path_single(repo_pkg_path, pkg.filename())
@ -71,8 +72,9 @@ pub fn (r &RepoGroupManager) add_pkg_from_path(repo string, pkg_path string) ?Re
os.rm(pkg_path)? os.rm(pkg_path)?
return RepoAddResult{ return RepoAddResult{
added: added.len > 0 name: pkg.info.name
pkg: &pkg version: pkg.info.version
archs: archs
} }
} }
@ -87,11 +89,9 @@ fn (r &RepoGroupManager) add_pkg_in_repo(repo string, pkg &package.Pkg) ?[]strin
// A package not of arch 'any' can be handled easily by adding it to the // A package not of arch 'any' can be handled easily by adding it to the
// respective repo // respective repo
if pkg.info.arch != 'any' { if pkg.info.arch != 'any' {
if r.add_pkg_in_arch_repo(repo, pkg.info.arch, pkg)? { r.add_pkg_in_arch_repo(repo, pkg.info.arch, pkg)?
return [pkg.info.arch] return [pkg.info.arch]
} else {
return []
}
} }
mut arch_repos := []string{} mut arch_repos := []string{}
@ -113,25 +113,22 @@ fn (r &RepoGroupManager) add_pkg_in_repo(repo string, pkg &package.Pkg) ?[]strin
arch_repos << r.default_arch arch_repos << r.default_arch
} }
mut added := []string{} // Add the package to each found architecture
// NOTE: if any of these fail, the function fails. This means the user does
// We add the package to each repository. If any of the repositories // not know which arch-repositories did succeed in adding the package, if
// return true, the result of the function is also true. // any.
for arch in arch_repos { for arch in arch_repos {
if r.add_pkg_in_arch_repo(repo, arch, pkg)? { r.add_pkg_in_arch_repo(repo, arch, pkg)?
added << arch
}
} }
return added return arch_repos
} }
// add_pkg_in_arch_repo is the function that actually adds a package to a given // add_pkg_in_arch_repo is the function that actually adds a package to a given
// arch-repo. It records the package's data in the arch-repo's desc & files // arch-repo. It records the package's data in the arch-repo's desc & files
// files, and afterwards updates the db & files archives to reflect these // files, and afterwards updates the db & files archives to reflect these
// changes. The function returns false if the package was already present in // changes.
// the repo, and true otherwise. fn (r &RepoGroupManager) add_pkg_in_arch_repo(repo string, arch string, pkg &package.Pkg) ? {
fn (r &RepoGroupManager) add_pkg_in_arch_repo(repo string, arch string, pkg &package.Pkg) ?bool {
pkg_dir := os.join_path(r.repos_dir, repo, arch, '$pkg.info.name-$pkg.info.version') pkg_dir := os.join_path(r.repos_dir, repo, arch, '$pkg.info.name-$pkg.info.version')
// Remove the previous version of the package, if present // Remove the previous version of the package, if present
@ -151,6 +148,4 @@ fn (r &RepoGroupManager) add_pkg_in_arch_repo(repo string, arch string, pkg &pac
} }
r.sync(repo, arch)? r.sync(repo, arch)?
return true
} }

View File

@ -6,8 +6,7 @@ import repo
import time import time
import rand import rand
import util import util
import net.http import web.response { new_data_response, new_response }
import web.response { new_response }
// healthcheck just returns a string, but can be used to quickly check if the // healthcheck just returns a string, but can be used to quickly check if the
// server is still responsive. // server is still responsive.
@ -65,7 +64,7 @@ fn (mut app App) put_package(repo string) web.Result {
util.reader_to_file(mut app.reader, length.int(), pkg_path) or { util.reader_to_file(mut app.reader, length.int(), pkg_path) or {
app.lwarn("Failed to upload '$pkg_path'") app.lwarn("Failed to upload '$pkg_path'")
return app.json(http.Status.internal_server_error, new_response('Failed to upload file.')) return app.status(.internal_server_error)
} }
sw.stop() sw.stop()
@ -74,7 +73,7 @@ fn (mut app App) put_package(repo string) web.Result {
app.lwarn('Tried to upload package without specifying a Content-Length.') app.lwarn('Tried to upload package without specifying a Content-Length.')
// length required // length required
return app.status(http.Status.length_required) return app.status(.length_required)
} }
res := app.repo.add_pkg_from_path(repo, pkg_path) or { res := app.repo.add_pkg_from_path(repo, pkg_path) or {
@ -82,18 +81,10 @@ fn (mut app App) put_package(repo string) web.Result {
os.rm(pkg_path) or { app.lerror("Failed to remove download '$pkg_path': $err.msg()") } os.rm(pkg_path) or { app.lerror("Failed to remove download '$pkg_path': $err.msg()") }
return app.json(http.Status.internal_server_error, new_response('Failed to add package.')) return app.status(.internal_server_error)
} }
if !res.added { app.linfo("Added '$res.name-$res.version' to '$repo (${res.archs.join(',')})'.")
os.rm(pkg_path) or { app.lerror("Failed to remove download '$pkg_path': $err.msg()") }
app.lwarn("Duplicate package '$res.pkg.full_name()' in repo '$repo'.") return app.json(.ok, new_data_response(res))
return app.json(http.Status.bad_request, new_response('File already exists.'))
}
app.linfo("Added '$res.pkg.full_name()' to repo '$repo ($res.pkg.info.arch)'.")
return app.json(http.Status.ok, new_response('Package added successfully.'))
} }