/// What a handler answers, as a value: a page, a fragment for htmx, a /// redirect, and the status, cookie and headers around them. `respond` /// turns a `Reply` into a `Response` inside the app's `Shell`. import std/bytes import std/html.{type Html} import std/http.{type Request, type Response, Response, Whole} import std/http/cookie.{type SetCookie} /// A handler's answer. pub type Reply { /// A whole document: `content` inside the shell's frame. Page(title: String, content: Html) /// `content` alone, for htmx to swap in. , Fragment(content: Html) /// A 303 to `location`; for htmx, a 200 with `HX-Redirect`. , Redirect(location: String) /// `reply` with the status `code`. , Status(code: Int, reply: Reply) , WithCookie(cookie: SetCookie, reply: Reply) /// `reply` with `HX-Trigger` firing `event` on the body. , Trigger(event: String, reply: Reply) , Header(name: String, value: String, reply: Reply) /// Nothing here: a 404 with the shell's `missing` notice. `otherwise` /// tries the next handler. , Missing /// A request that could not be read: a 400 with the shell's /// `unreadable` notice. , Unreadable } /// What the app shows around and instead of its replies: the `frame` a /// page's title and content go in, and the notices for `Missing` and /// `Unreadable`. pub type Shell { Shell(frame: fn(String, Html) -> Html, missing: Notice, unreadable: Notice) } /// A title and what a page shows under it. pub type Notice { Notice(title: String, content: Html) } /// The response for `reply` to `req`, its pages framed by `shell`. pub fn respond :: req: Request reply: Reply shell: Shell -> Response { match reply { Page(title, content) -> markup ("" <> html.render (shell.frame title content)) , Fragment(content) -> markup (html.render content) , Redirect(location) -> if from_htmx req { Response(200, [("HX-Redirect", location)], Whole(bytes.empty ())) } else { Response(303, [("Location", location)], Whole(bytes.empty ())) } , Status(code, inner) -> Response(..respond req inner shell, status: code) , WithCookie(c, inner) -> cookie.set_cookie (respond req inner shell) c , Trigger(event, inner) -> http.with_header (respond req inner shell) "HX-Trigger" "\{\"{event}\":\{\"target\":\"body\"\}\}" , Header(name, value, inner) -> http.with_header (respond req inner shell) name value , Missing -> respond req (noticed req 404 shell.missing) shell , Unreadable -> respond req (noticed req 400 shell.unreadable) shell } } /// A fragment for htmx, a page otherwise. pub fn view :: req: Request title: String content: Html -> Reply { if from_htmx req { Fragment(content) } else { Page(title, content) } } /// Whether htmx sent `req`. pub fn from_htmx :: req: Request -> Bool { http.header req "HX-Request" == Some("true") } /// `first`, or what `next` answers when `first` is `Missing`: chains /// handlers that each answer only their own paths. pub fn otherwise :: first: Reply next: fn() -> Reply / -> Reply / { match first { Missing -> next (), _ -> first } } /// `shown` as a 429, to be tried again in `retry_seconds`. pub fn too_many :: shown: Reply retry_seconds: Int -> Reply { Header("Retry-After", "{retry_seconds}", Status(429, shown)) } fn noticed :: req: Request code: Int notice: Notice -> Reply { Status(code, view req notice.title notice.content) } fn markup :: text: String -> Response { http.response 200 "text/html; charset=utf-8" (bytes.from_string text) }