Improve response handling
This commit is contained in:
parent
2866e2fcbc
commit
34eb2e000a
4 changed files with 18 additions and 24 deletions
|
@ -5,6 +5,7 @@
|
||||||
(:local-nicknames (#:fbr #:ningle-fbr))
|
(:local-nicknames (#:fbr #:ningle-fbr))
|
||||||
(:local-nicknames (#:env #:hp/env))
|
(:local-nicknames (#:env #:hp/env))
|
||||||
(:local-nicknames (#:mw #:hp/middlewares/*))
|
(:local-nicknames (#:mw #:hp/middlewares/*))
|
||||||
|
(:import-from #:hp/response)
|
||||||
(:export #:start
|
(:export #:start
|
||||||
#:stop
|
#:stop
|
||||||
#:update))
|
#:update))
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
(defpackage #:hp/response
|
(defpackage #:hp/response
|
||||||
(:use #:cl
|
(:use #:cl
|
||||||
#:hsx)
|
#:hsx)
|
||||||
(:local-nicknames (#:jg #:jingle))
|
(:local-nicknames (#:jg #:jingle)))
|
||||||
(:export #:response
|
|
||||||
#:partial-response))
|
|
||||||
(in-package #:hp/response)
|
(in-package #:hp/response)
|
||||||
|
|
||||||
(defcomp document (&key title description children)
|
(defcomp document (&key title description children)
|
||||||
|
@ -26,15 +24,11 @@
|
||||||
(main :class "container mx-auto"
|
(main :class "container mx-auto"
|
||||||
children))))))
|
children))))))
|
||||||
|
|
||||||
(defun response (page &key status metadata)
|
(defmethod jg:process-response ((app jg:app) result)
|
||||||
(jg:with-html-response
|
(jg:set-response-header :content-type "text/html; charset=utf-8")
|
||||||
(when status
|
(call-next-method app
|
||||||
(jg:set-response-status status))
|
(hsx:render-to-string
|
||||||
(hsx:render-to-string (document metadata
|
(if (listp result)
|
||||||
page))))
|
(destructuring-bind (body metadata) result
|
||||||
|
(document metadata body))
|
||||||
(defun partial-response (component &key status)
|
(document result)))))
|
||||||
(jg:with-html-response
|
|
||||||
(when status
|
|
||||||
(jg:set-response-status status))
|
|
||||||
(hsx:render-to-string component)))
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
(defpackage #:hp/routes/index
|
(defpackage #:hp/routes/index
|
||||||
(:use #:cl
|
(:use #:cl
|
||||||
#:hsx
|
#:hsx)
|
||||||
#:hp/response)
|
|
||||||
(:export #:handle-get))
|
(:export #:handle-get))
|
||||||
(in-package #:hp/routes/index)
|
(in-package #:hp/routes/index)
|
||||||
|
|
||||||
|
@ -12,4 +11,4 @@
|
||||||
|
|
||||||
(defun handle-get (params)
|
(defun handle-get (params)
|
||||||
(declare (ignore params))
|
(declare (ignore params))
|
||||||
(response (page)))
|
(page))
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
(defpackage #:hp/routes/not-found
|
(defpackage #:hp/routes/not-found
|
||||||
(:use #:cl
|
(:use #:cl
|
||||||
#:hsx
|
#:hsx)
|
||||||
#:hp/response)
|
(:local-nicknames (#:jg #:jingle))
|
||||||
(:export #:handle-not-found))
|
(:export #:handle-not-found))
|
||||||
(in-package #:hp/routes/not-found)
|
(in-package #:hp/routes/not-found)
|
||||||
|
|
||||||
|
@ -11,9 +11,9 @@
|
||||||
|
|
||||||
(defcomp page ()
|
(defcomp page ()
|
||||||
(hsx
|
(hsx
|
||||||
(h1 "404 Not Found")))
|
(h1 :class "text-primary"
|
||||||
|
"404 Not Found")))
|
||||||
|
|
||||||
(defun handle-not-found ()
|
(defun handle-not-found ()
|
||||||
(response (page)
|
(jg:set-response-status :not-found)
|
||||||
:status :not-found
|
(list (page) *metadata*))
|
||||||
:metadata *metadata*))
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue