Start of web framework revamp

This commit is contained in:
Jef Roosens 2022-03-28 14:24:26 +02:00
parent 08821725f9
commit 0a6be87970
Signed by untrusted user: Jef Roosens
GPG key ID: B75D4F293C7052DB
4 changed files with 70 additions and 216 deletions

View file

@ -78,31 +78,31 @@ fn write_repos(path string, repos &map[string]GitRepo) ? {
['/api/repos'; get]
fn (mut app App) get_repos() web.Result {
if !app.is_authorized() {
return app.text('Unauthorized.')
return app.json(401, new_response('Unauthorized.'))
}
repos := rlock app.git_mutex {
read_repos(app.conf.repos_file) or {
app.lerror('Failed to read repos file.')
return app.server_error(500)
return app.status(500)
}
}
return app.json(repos)
return app.json(200, 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.text('Unauthorized.')
return app.json(401, new_response('Unauthorized.'))
}
repos := rlock app.git_mutex {
read_repos(app.conf.repos_file) or {
app.lerror('Failed to read repos file.')
return app.server_error(500)
return app.status(500)
}
}
@ -112,18 +112,17 @@ fn (mut app App) get_single_repo(id string) web.Result {
repo := repos[id]
return app.json(repo)
return app.json(200, 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(401, new_response('Unauthorized.'))
}
new_repo := repo_from_params(app.query) or {
app.set_status(400, err.msg)
return app.ok("")
return app.json(400, new_response(err.msg))
}
id := rand.uuid_v4()
@ -132,37 +131,37 @@ fn (mut app App) post_repo() web.Result {
read_repos(app.conf.repos_file) or {
app.lerror('Failed to read repos file.')
return app.server_error(500)
return app.status(500)
}
}
// We need to check for duplicates
for _, repo in repos {
if repo == new_repo {
return app.text('Duplicate repository.')
return app.json(400, new_response('Duplicate repository.'))
}
}
repos[id] = new_repo
lock app.git_mutex {
write_repos(app.conf.repos_file, &repos) or { return app.server_error(500) }
write_repos(app.conf.repos_file, &repos) or { return app.status(500) }
}
return app.ok('Repo added successfully.')
return app.json(200, new_response('Repo added successfully.'))
}
['/api/repos/:id'; delete]
fn (mut app App) delete_repo(id string) web.Result {
if !app.is_authorized() {
return app.text('Unauthorized.')
return app.json(401, new_response('Unauthorized.'))
}
mut repos := rlock app.git_mutex {
read_repos(app.conf.repos_file) or {
app.lerror('Failed to read repos file.')
return app.server_error(500)
return app.status(500)
}
}
@ -176,20 +175,20 @@ fn (mut app App) delete_repo(id string) web.Result {
write_repos(app.conf.repos_file, &repos) or { return app.server_error(500) }
}
return app.ok('Repo removed successfully.')
return app.json(200, 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.text('Unauthorized.')
return app.json(401, new_response('Unauthorized.'))
}
mut repos := rlock app.git_mutex {
read_repos(app.conf.repos_file) or {
app.lerror('Failed to read repos file.')
return app.server_error(500)
return app.status(500)
}
}
@ -203,5 +202,5 @@ fn (mut app App) patch_repo(id string) web.Result {
write_repos(app.conf.repos_file, &repos) or { return app.server_error(500) }
}
return app.ok('Repo updated successfully.')
return app.json(200, new_response('Repo updated successfully.'))
}

27
src/server/response.v Normal file
View file

@ -0,0 +1,27 @@
module server
struct Response<T> {
message string
data T
}
fn new_response(message string) Response<string> {
return Response<string>{
message: message
data: ""
}
}
fn new_data_response<T>(data T) Response<T> {
return Response<T>{
message: ""
data: data
}
}
fn new_full_response<T>(message string, data T) Response<T> {
return Response<T>{
message: message
data: data
}
}

View file

@ -12,7 +12,7 @@ import net.http
// server is still responsive.
['/health'; get]
pub fn (mut app App) healthcheck() web.Result {
return app.text('Healthy')
return app.json(200, new_response('Healthy.'))
}
// get_root handles a GET request for a file on the root
@ -31,7 +31,7 @@ fn (mut app App) get_root(filename string) web.Result {
// 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(200)
}
return app.not_found()
@ -43,7 +43,7 @@ fn (mut app App) get_root(filename string) web.Result {
['/publish'; post]
fn (mut app App) put_package() web.Result {
if !app.is_authorized() {
return app.text('Unauthorized.')
return app.json(401, new_response('Unauthorized.'))
}
mut pkg_path := ''
@ -64,14 +64,16 @@ fn (mut app App) put_package() 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(500, 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(411)
}
res := app.repo.add_from_path(pkg_path) or {
@ -79,17 +81,17 @@ fn (mut app App) put_package() 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(500, new_response('Failed to add package.'))
}
if !res.added {
os.rm(pkg_path) or { app.lerror("Failed to remove download '$pkg_path': $err.msg") }
app.lwarn("Duplicate package '$res.pkg.full_name()'.")
return app.text('File already exists.')
return app.json(400, new_response('File already exists.'))
}
app.linfo("Added '$res.pkg.full_name()' to repository.")
return app.text('Package added successfully.')
return app.json(200, new_response('Package added successfully.'))
}