tobogganSe connecter

packages

The Louise packages, each released on its own

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

110 lignes · 3569 octetsBrut · Historique
1/// What a handler answers, as a value: a page, a fragment for htmx, a
2/// redirect, and the status, cookie and headers around them. `respond`
3/// turns a `Reply` into a `Response` inside the app's `Shell`.
4
5import std/bytes
6import std/html.{type Html}
7import std/http.{type Request, type Response, Response, Whole}
8import std/http/cookie.{type SetCookie}
9
10/// A handler's answer.
11pub type Reply
12{ /// A whole document: `content` inside the shell's frame.
13 Page(title: String, content: Html)
14 /// `content` alone, for htmx to swap in.
15, Fragment(content: Html)
16 /// A 303 to `location`; for htmx, a 200 with `HX-Redirect`.
17, Redirect(location: String)
18 /// `reply` with the status `code`.
19, Status(code: Int, reply: Reply)
20, WithCookie(cookie: SetCookie, reply: Reply)
21 /// `reply` with `HX-Trigger` firing `event` on the body.
22, Trigger(event: String, reply: Reply)
23, Header(name: String, value: String, reply: Reply)
24 /// Nothing here: a 404 with the shell's `missing` notice. `otherwise`
25 /// tries the next handler.
26, Missing
27 /// A request that could not be read: a 400 with the shell's
28 /// `unreadable` notice.
29, Unreadable
30}
31
32/// What the app shows around and instead of its replies: the `frame` a
33/// page's title and content go in, and the notices for `Missing` and
34/// `Unreadable`.
35pub type Shell
36{ Shell(frame: fn(String, Html) -> Html, missing: Notice, unreadable: Notice) }
37
38/// A title and what a page shows under it.
39pub type Notice { Notice(title: String, content: Html) }
40
41/// The response for `reply` to `req`, its pages framed by `shell`.
42pub fn respond :: req: Request reply: Reply shell: Shell -> Response
43{
44 match reply
45 { Page(title, content) ->
46 markup ("<!doctype html>" <> html.render (shell.frame title content))
47 , Fragment(content) -> markup (html.render content)
48 , Redirect(location) -> if from_htmx req
49 {
50 Response(200, [("HX-Redirect", location)], Whole(bytes.empty ()))
51 }
52 else
53 {
54 Response(303, [("Location", location)], Whole(bytes.empty ()))
55 }
56 , Status(code, inner) -> Response(..respond req inner shell, status: code)
57 , WithCookie(c, inner) -> cookie.set_cookie (respond req inner shell) c
58 , Trigger(event, inner) ->
59 http.with_header
60 (respond req inner shell)
61 "HX-Trigger"
62 "\{\"{event}\":\{\"target\":\"body\"\}\}"
63 , Header(name, value, inner) ->
64 http.with_header (respond req inner shell) name value
65 , Missing -> respond req (noticed req 404 shell.missing) shell
66 , Unreadable -> respond req (noticed req 400 shell.unreadable) shell
67 }
68}
69
70/// A fragment for htmx, a page otherwise.
71pub fn view :: req: Request title: String content: Html -> Reply
72{
73 if from_htmx req
74 {
75 Fragment(content)
76 }
77 else
78 {
79 Page(title, content)
80 }
81}
82
83/// Whether htmx sent `req`.
84pub fn from_htmx :: req: Request -> Bool
85{
86 http.header req "HX-Request" == Some("true")
87}
88
89/// `first`, or what `next` answers when `first` is `Missing`: chains
90/// handlers that each answer only their own paths.
91pub fn otherwise :: first: Reply next: fn() -> Reply / <e> -> Reply / <e>
92{
93 match first { Missing -> next (), _ -> first }
94}
95
96/// `shown` as a 429, to be tried again in `retry_seconds`.
97pub fn too_many :: shown: Reply retry_seconds: Int -> Reply
98{
99 Header("Retry-After", "{retry_seconds}", Status(429, shown))
100}
101
102fn noticed :: req: Request code: Int notice: Notice -> Reply
103{
104 Status(code, view req notice.title notice.content)
105}
106
107fn markup :: text: String -> Response
108{
109 http.response 200 "text/html; charset=utf-8" (bytes.from_string text)
110}