Improve response handling

This commit is contained in:
Akira Tempaku 2024-10-02 23:36:30 +09:00
commit 34eb2e000a
4 changed files with 18 additions and 24 deletions

View file

@ -5,6 +5,7 @@
(:local-nicknames (#:fbr #:ningle-fbr))
(:local-nicknames (#:env #:hp/env))
(:local-nicknames (#:mw #:hp/middlewares/*))
(:import-from #:hp/response)
(:export #:start
#:stop
#:update))

View file

@ -1,9 +1,7 @@
(defpackage #:hp/response
(:use #:cl
#:hsx)
(:local-nicknames (#:jg #:jingle))
(:export #:response
#:partial-response))
(:local-nicknames (#:jg #:jingle)))
(in-package #:hp/response)
(defcomp document (&key title description children)
@ -26,15 +24,11 @@
(main :class "container mx-auto"
children))))))
(defun response (page &key status metadata)
(jg:with-html-response
(when status
(jg:set-response-status status))
(hsx:render-to-string (document metadata
page))))
(defun partial-response (component &key status)
(jg:with-html-response
(when status
(jg:set-response-status status))
(hsx:render-to-string component)))
(defmethod jg:process-response ((app jg:app) result)
(jg:set-response-header :content-type "text/html; charset=utf-8")
(call-next-method app
(hsx:render-to-string
(if (listp result)
(destructuring-bind (body metadata) result
(document metadata body))
(document result)))))

View file

@ -1,7 +1,6 @@
(defpackage #:hp/routes/index
(:use #:cl
#:hsx
#:hp/response)
#:hsx)
(:export #:handle-get))
(in-package #:hp/routes/index)
@ -12,4 +11,4 @@
(defun handle-get (params)
(declare (ignore params))
(response (page)))
(page))

View file

@ -1,7 +1,7 @@
(defpackage #:hp/routes/not-found
(:use #:cl
#:hsx
#:hp/response)
#:hsx)
(:local-nicknames (#:jg #:jingle))
(:export #:handle-not-found))
(in-package #:hp/routes/not-found)
@ -11,9 +11,9 @@
(defcomp page ()
(hsx
(h1 "404 Not Found")))
(h1 :class "text-primary"
"404 Not Found")))
(defun handle-not-found ()
(response (page)
:status :not-found
:metadata *metadata*))
(jg:set-response-status :not-found)
(list (page) *metadata*))