Switched to net.http.Status for status codes

This commit is contained in:
Jef Roosens 2022-03-28 14:44:23 +02:00
parent 0a6be87970
commit e5a630e990
Signed by untrusted user: Jef Roosens
GPG key ID: B75D4F293C7052DB
3 changed files with 34 additions and 33 deletions

View file

@ -4,6 +4,7 @@ import web
import os
import json
import rand
import net.http
pub struct GitRepo {
pub mut:
@ -78,31 +79,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.json(401, new_response('Unauthorized.'))
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
}
repos := rlock app.git_mutex {
read_repos(app.conf.repos_file) or {
app.lerror('Failed to read repos file.')
return app.status(500)
return app.status(http.Status.internal_server_error)
}
}
return app.json(200, new_data_response(repos))
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(401, new_response('Unauthorized.'))
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
}
repos := rlock app.git_mutex {
read_repos(app.conf.repos_file) or {
app.lerror('Failed to read repos file.')
return app.status(500)
return app.status(http.Status.internal_server_error)
}
}
@ -112,17 +113,17 @@ fn (mut app App) get_single_repo(id string) web.Result {
repo := repos[id]
return app.json(200, new_data_response(repo))
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.json(401, new_response('Unauthorized.'))
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
}
new_repo := repo_from_params(app.query) or {
return app.json(400, new_response(err.msg))
return app.json(http.Status.bad_request, new_response(err.msg))
}
id := rand.uuid_v4()
@ -131,37 +132,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.status(500)
return app.status(http.Status.internal_server_error)
}
}
// We need to check for duplicates
for _, repo in repos {
if repo == new_repo {
return app.json(400, new_response('Duplicate repository.'))
return app.json(http.Status.bad_request, new_response('Duplicate repository.'))
}
}
repos[id] = new_repo
lock app.git_mutex {
write_repos(app.conf.repos_file, &repos) or { return app.status(500) }
write_repos(app.conf.repos_file, &repos) or { return app.status(http.Status.internal_server_error) }
}
return app.json(200, new_response('Repo added successfully.'))
return app.json(http.Status.ok, 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.json(401, new_response('Unauthorized.'))
return app.json(http.Status.unauthorized, 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.status(500)
return app.status(http.Status.internal_server_error)
}
}
@ -175,20 +176,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.json(200, new_response('Repo removed successfully.'))
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(401, new_response('Unauthorized.'))
return app.json(http.Status.unauthorized, 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.status(500)
return app.status(http.Status.internal_server_error)
}
}
@ -202,5 +203,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.json(200, new_response('Repo updated successfully.'))
return app.json(http.Status.ok, new_response('Repo updated successfully.'))
}