refactor(web): some small cleanup

pull/266/head
Jef Roosens 2022-08-13 17:49:05 +02:00
parent e23635a1d3
commit 9268ef0302
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 20 additions and 19 deletions

View File

@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
providing a Git repository
* CLI commands for searching the AUR & directly adding packages
* HTTP routes for removing packages, arch-repos & repos
* All endpoints serving files now support HTTP byte range requests
### Changed
@ -25,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Branch name for 'git' targets is now optional; if not provided, the
repository will be cloned with the default branch
* Build containers now explicitely set the PATH variable
* Refactor of web framework
### Removed

View File

@ -8,7 +8,7 @@ import web.response { new_response }
['/:repo/:arch/:pkg'; delete]
fn (mut app App) delete_package(repo string, arch string, pkg string) web.Result {
if !app.is_authorized() {
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
return app.json(.unauthorized, new_response('Unauthorized.'))
}
res := app.repo.remove_pkg_from_arch_repo(repo, arch, pkg, true) or {

View File

@ -9,7 +9,7 @@ pub struct Result {}
pub const (
methods_with_form = [http.Method.post, .put, .patch]
headers_close = http.new_custom_header_from_map({
'Server': 'VWeb'
'Server': 'Vieter'
http.CommonHeader.connection.str(): 'close'
}) or { panic('should never fail') }

View File

@ -91,15 +91,22 @@ fn (mut ctx Context) send_reader(mut reader io.Reader, size u64) ? {
}
}
// send_custom_response sends the given http.Response to the client. It can be
// used to overwrite the Context object & send a completely custom
// http.Response instead.
fn (mut ctx Context) send_custom_response(resp &http.Response) ? {
ctx.send_string(resp.bytestr())?
}
// send_response_header constructs a valid HTTP response with an empty body &
// sends it to the client.
pub fn (mut ctx Context) send_response_header() ? {
mut resp := http.Response{
header: ctx.header.join(headers_close)
}
resp.set_version(.v1_1)
resp.set_status(ctx.status)
ctx.send_string(resp.bytestr())?
ctx.send_custom_response(resp)?
}
// send is a convenience function for sending the HTTP response with an empty
@ -222,10 +229,8 @@ pub fn (mut ctx Context) status(status http.Status) Result {
// server_error Response a server error
pub fn (mut ctx Context) server_error(ecode int) Result {
$if debug {
eprintln('> ctx.server_error ecode: $ecode')
}
ctx.send_string(http_500.bytestr()) or {}
ctx.send_custom_response(http_500) or {}
return Result{}
}
@ -234,23 +239,17 @@ pub fn (mut ctx Context) redirect(url string) Result {
mut resp := http_302
resp.header = resp.header.join(ctx.header)
resp.header.add(.location, url)
ctx.send_string(resp.bytestr()) or { return Result{} }
ctx.send_custom_response(resp) or {}
return Result{}
}
// not_found Send an not_found response
pub fn (mut ctx Context) not_found() Result {
return ctx.status(http.Status.not_found)
}
ctx.send_custom_response(http_404) or {}
// add_header Adds an header to the response with key and val
pub fn (mut ctx Context) add_header(key string, val string) {
ctx.header.add_custom(key, val) or {}
}
// get_header Returns the header data from the key
pub fn (ctx &Context) get_header(key string) string {
return ctx.req.header.get_custom(key) or { '' }
return Result{}
}
interface DbInterface {