forked from vieter-v/vieter
Merge branch 'dev' into multi-arch-repos-v2
This commit is contained in:
commit
6479071fd7
11 changed files with 279 additions and 276 deletions
116
src/server/git.v
116
src/server/git.v
|
|
@ -2,92 +2,140 @@ module server
|
|||
|
||||
import web
|
||||
import git
|
||||
import net.http
|
||||
import rand
|
||||
import response { new_data_response, new_response }
|
||||
|
||||
const repos_file = 'repos.json'
|
||||
|
||||
['/api/repos'; get]
|
||||
fn (mut app App) get_repos() web.Result {
|
||||
if !app.is_authorized() {
|
||||
return app.text('Unauthorized.')
|
||||
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
||||
}
|
||||
|
||||
repos := rlock app.git_mutex {
|
||||
git.read_repos(app.conf.repos_file) or {
|
||||
app.lerror('Failed to read repos file: $err.msg')
|
||||
|
||||
return app.status(http.Status.internal_server_error)
|
||||
}
|
||||
}
|
||||
|
||||
return app.json(http.Status.ok, new_data_response(repos))
|
||||
}
|
||||
|
||||
['/api/repos/:id'; get]
|
||||
fn (mut app App) get_single_repo(id string) web.Result {
|
||||
if !app.is_authorized() {
|
||||
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
||||
}
|
||||
|
||||
repos := rlock app.git_mutex {
|
||||
git.read_repos(app.conf.repos_file) or {
|
||||
app.lerror('Failed to read repos file.')
|
||||
|
||||
return app.server_error(500)
|
||||
return app.status(http.Status.internal_server_error)
|
||||
}
|
||||
}
|
||||
|
||||
return app.json(repos)
|
||||
if id !in repos {
|
||||
return app.not_found()
|
||||
}
|
||||
|
||||
repo := repos[id]
|
||||
|
||||
return app.json(http.Status.ok, new_data_response(repo))
|
||||
}
|
||||
|
||||
['/api/repos'; post]
|
||||
fn (mut app App) post_repo() web.Result {
|
||||
if !app.is_authorized() {
|
||||
return app.text('Unauthorized.')
|
||||
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
||||
}
|
||||
|
||||
if !('url' in app.query && 'branch' in app.query) {
|
||||
return app.server_error(400)
|
||||
new_repo := git.repo_from_params(app.query) or {
|
||||
return app.json(http.Status.bad_request, new_response(err.msg))
|
||||
}
|
||||
|
||||
new_repo := git.GitRepo{
|
||||
url: app.query['url']
|
||||
branch: app.query['branch']
|
||||
}
|
||||
id := rand.uuid_v4()
|
||||
|
||||
mut repos := rlock app.git_mutex {
|
||||
git.read_repos(app.conf.repos_file) or {
|
||||
app.lerror('Failed to read repos file.')
|
||||
|
||||
return app.server_error(500)
|
||||
return app.status(http.Status.internal_server_error)
|
||||
}
|
||||
}
|
||||
|
||||
// We need to check for duplicates
|
||||
for r in repos {
|
||||
if r == new_repo {
|
||||
return app.text('Duplicate repository.')
|
||||
for _, repo in repos {
|
||||
if repo == new_repo {
|
||||
return app.json(http.Status.bad_request, new_response('Duplicate repository.'))
|
||||
}
|
||||
}
|
||||
|
||||
repos << new_repo
|
||||
repos[id] = new_repo
|
||||
|
||||
lock app.git_mutex {
|
||||
git.write_repos(app.conf.repos_file, repos) or { return app.server_error(500) }
|
||||
git.write_repos(app.conf.repos_file, &repos) or {
|
||||
return app.status(http.Status.internal_server_error)
|
||||
}
|
||||
}
|
||||
|
||||
return app.ok('Repo added successfully.')
|
||||
return app.json(http.Status.ok, new_response('Repo added successfully.'))
|
||||
}
|
||||
|
||||
['/api/repos'; delete]
|
||||
fn (mut app App) delete_repo() web.Result {
|
||||
['/api/repos/:id'; delete]
|
||||
fn (mut app App) delete_repo(id string) web.Result {
|
||||
if !app.is_authorized() {
|
||||
return app.text('Unauthorized.')
|
||||
}
|
||||
|
||||
if !('url' in app.query && 'branch' in app.query) {
|
||||
return app.server_error(400)
|
||||
}
|
||||
|
||||
repo_to_remove := git.GitRepo{
|
||||
url: app.query['url']
|
||||
branch: app.query['branch']
|
||||
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
||||
}
|
||||
|
||||
mut repos := rlock app.git_mutex {
|
||||
git.read_repos(app.conf.repos_file) or {
|
||||
app.lerror('Failed to read repos file.')
|
||||
|
||||
return app.server_error(500)
|
||||
return app.status(http.Status.internal_server_error)
|
||||
}
|
||||
}
|
||||
filtered := repos.filter(it != repo_to_remove)
|
||||
|
||||
lock app.git_mutex {
|
||||
git.write_repos(app.conf.repos_file, filtered) or { return app.server_error(500) }
|
||||
if id !in repos {
|
||||
return app.not_found()
|
||||
}
|
||||
|
||||
return app.ok('Repo removed successfully.')
|
||||
repos.delete(id)
|
||||
|
||||
lock app.git_mutex {
|
||||
git.write_repos(app.conf.repos_file, &repos) or { return app.server_error(500) }
|
||||
}
|
||||
|
||||
return app.json(http.Status.ok, new_response('Repo removed successfully.'))
|
||||
}
|
||||
|
||||
['/api/repos/:id'; patch]
|
||||
fn (mut app App) patch_repo(id string) web.Result {
|
||||
if !app.is_authorized() {
|
||||
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
||||
}
|
||||
|
||||
mut repos := rlock app.git_mutex {
|
||||
git.read_repos(app.conf.repos_file) or {
|
||||
app.lerror('Failed to read repos file.')
|
||||
|
||||
return app.status(http.Status.internal_server_error)
|
||||
}
|
||||
}
|
||||
|
||||
if id !in repos {
|
||||
return app.not_found()
|
||||
}
|
||||
|
||||
repos[id].patch_from_params(app.query)
|
||||
|
||||
lock app.git_mutex {
|
||||
git.write_repos(app.conf.repos_file, &repos) or { return app.server_error(500) }
|
||||
}
|
||||
|
||||
return app.json(http.Status.ok, new_response('Repo updated successfully.'))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,12 +7,13 @@ import time
|
|||
import rand
|
||||
import util
|
||||
import net.http
|
||||
import response { new_response }
|
||||
|
||||
// healthcheck just returns a string, but can be used to quickly check if the
|
||||
// server is still responsive.
|
||||
['/health'; get]
|
||||
pub fn (mut app App) healthcheck() web.Result {
|
||||
return app.text('Healthy')
|
||||
return app.json(http.Status.ok, new_response('Healthy.'))
|
||||
}
|
||||
|
||||
['/:repo/:arch/:filename'; get; head]
|
||||
|
|
@ -36,7 +37,7 @@ fn (mut app App) get_repo_file(repo string, arch string, filename string) web.Re
|
|||
// Scuffed way to respond to HEAD requests
|
||||
if app.req.method == http.Method.head {
|
||||
if os.exists(full_path) {
|
||||
return app.ok('')
|
||||
return app.status(http.Status.ok)
|
||||
}
|
||||
|
||||
return app.not_found()
|
||||
|
|
@ -48,7 +49,7 @@ fn (mut app App) get_repo_file(repo string, arch string, filename string) web.Re
|
|||
['/:repo/publish'; post]
|
||||
fn (mut app App) put_package(repo string) web.Result {
|
||||
if !app.is_authorized() {
|
||||
return app.text('Unauthorized.')
|
||||
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
||||
}
|
||||
|
||||
mut pkg_path := ''
|
||||
|
|
@ -69,14 +70,16 @@ fn (mut app App) put_package(repo string) web.Result {
|
|||
util.reader_to_file(mut app.reader, length.int(), pkg_path) or {
|
||||
app.lwarn("Failed to upload '$pkg_path'")
|
||||
|
||||
return app.text('Failed to upload file.')
|
||||
return app.json(http.Status.internal_server_error, new_response('Failed to upload file.'))
|
||||
}
|
||||
|
||||
sw.stop()
|
||||
app.ldebug("Upload of '$pkg_path' completed in ${sw.elapsed().seconds():.3}s.")
|
||||
} else {
|
||||
app.lwarn('Tried to upload package without specifying a Content-Length.')
|
||||
return app.text("Content-Type header isn't set.")
|
||||
|
||||
// length required
|
||||
return app.status(http.Status.length_required)
|
||||
}
|
||||
|
||||
res := app.repo.add_pkg_from_path(repo, pkg_path) or {
|
||||
|
|
@ -84,7 +87,7 @@ 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") }
|
||||
|
||||
return app.text('Failed to add package.')
|
||||
return app.json(http.Status.internal_server_error, new_response('Failed to add package.'))
|
||||
}
|
||||
|
||||
if !res.added {
|
||||
|
|
@ -92,10 +95,10 @@ fn (mut app App) put_package(repo string) web.Result {
|
|||
|
||||
app.lwarn("Duplicate package '$res.pkg.full_name()' in repo '$repo ($res.pkg.info.arch)'.")
|
||||
|
||||
return app.text('File already exists.')
|
||||
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.text('Package added successfully.')
|
||||
return app.json(http.Status.ok, new_response('Package added successfully.'))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue