feat(web/bubblegum): allow passing status as an int

The whole pass the name of the status as a string thing was mostly born
out of an overeager use of yants. It is still very neat especially for
common cases like "OK", so we'll keep it, but also allow passing the
integer variant of the status as well which probably feels more natural
for a lot of people, especially over getting the casing right for
"I'm a teapot".

Change-Id: I3f012a291447ef385efdd28132292a8b331998c0
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2850
Tested-by: BuildkiteCI
Reviewed-by: sterni <sternenseemann@systemli.org>
This commit is contained in:
sterni 2021-04-05 01:58:26 +02:00
parent 1c0f89f4ca
commit 386afdc794
2 changed files with 16 additions and 9 deletions

View file

@ -89,12 +89,12 @@ let
See the [README](./README.md) for an example. See the [README](./README.md) for an example.
Type: string -> attrs string -> string -> string Type: either int string -> attrs string -> string -> string
*/ */
respond = respond =
# response status as the textual representation in the # response status as an integer (status code) or its
# HTTP protocol. See `statusCodes` for a list of valid # textual representation in the HTTP protocol.
# options. # See `statusCodes` for a list of valid options.
statusArg: statusArg:
# headers as an attribute set of strings # headers as an attribute set of strings
headers: headers:
@ -102,7 +102,14 @@ let
bodyArg: bodyArg:
let let
status = status =
if builtins.isString statusArg then { if builtins.isInt statusArg
then {
code = statusArg;
line = lib.findFirst
(line: statusCodes."${line}" == statusArg)
null
(builtins.attrNames statusCodes);
} else if builtins.isString statusArg then {
code = statusCodes."${statusArg}" or null; code = statusCodes."${statusArg}" or null;
line = statusArg; line = statusArg;
} else { } else {

View file

@ -108,24 +108,24 @@ let
if pathInfo == "/" if pathInfo == "/"
then { then {
title = "blog"; title = "blog";
status = "OK"; status = 200;
inner = index posts; inner = index posts;
} }
else if !(validatePathInfo pathInfo) else if !(validatePathInfo pathInfo)
then { then {
title = "Bad Request"; title = "Bad Request";
status = "Bad Request"; status = 400;
inner = "No slashes in post names 😡"; inner = "No slashes in post names 😡";
} }
# CGI should already url.decode for us # CGI should already url.decode for us
else if builtins.pathExists (blogdir + "/" + pathInfo) else if builtins.pathExists (blogdir + "/" + pathInfo)
then rec { then rec {
title = parseTitle pathInfo; title = parseTitle pathInfo;
status = "OK"; status = 200;
inner = post title pathInfo; inner = post title pathInfo;
} else { } else {
title = "Not Found"; title = "Not Found";
status = "Not Found"; status = 404;
inner = "<h1>404 not found</h1>"; inner = "<h1>404 not found</h1>";
}; };
in in