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

@ -12,7 +12,7 @@ import net.http
// server is still responsive.
['/health'; get]
pub fn (mut app App) healthcheck() web.Result {
return app.json(200, new_response('Healthy.'))
return app.json(http.Status.ok, 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.status(200)
return app.status(http.Status.ok)
}
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.json(401, new_response('Unauthorized.'))
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
}
mut pkg_path := ''
@ -64,7 +64,7 @@ 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.json(500, new_response('Failed to upload file.'))
return app.json(http.Status.internal_server_error, new_response('Failed to upload file.'))
}
sw.stop()
@ -73,7 +73,7 @@ fn (mut app App) put_package() web.Result {
app.lwarn('Tried to upload package without specifying a Content-Length.')
// length required
return app.status(411)
return app.status(http.Status.length_required)
}
res := app.repo.add_from_path(pkg_path) or {
@ -81,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.json(500, new_response('Failed to add package.'))
return app.json(http.Status.internal_server_error, 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.json(400, new_response('File already exists.'))
return app.json(http.Status.bad_request, new_response('File already exists.'))
}
app.linfo("Added '$res.pkg.full_name()' to repository.")
return app.json(200, new_response('Package added successfully.'))
return app.json(http.Status.ok, new_response('Package added successfully.'))
}