From 34eb2e000a0415013ff816ead03a7acafe37a214 Mon Sep 17 00:00:00 2001
From: paku <paku@skyizwhite.dev>
Date: Wed, 2 Oct 2024 23:36:30 +0900
Subject: [PATCH] Improve response handling

---
 src/app.lisp              |  1 +
 src/response.lisp         | 24 +++++++++---------------
 src/routes/index.lisp     |  5 ++---
 src/routes/not-found.lisp | 12 ++++++------
 4 files changed, 18 insertions(+), 24 deletions(-)

diff --git a/src/app.lisp b/src/app.lisp
index 7dba06b..b3c42f5 100644
--- a/src/app.lisp
+++ b/src/app.lisp
@@ -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))
diff --git a/src/response.lisp b/src/response.lisp
index 05a5fa0..bef32bd 100644
--- a/src/response.lisp
+++ b/src/response.lisp
@@ -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)))))
diff --git a/src/routes/index.lisp b/src/routes/index.lisp
index 9e68226..3817195 100644
--- a/src/routes/index.lisp
+++ b/src/routes/index.lisp
@@ -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))
diff --git a/src/routes/not-found.lisp b/src/routes/not-found.lisp
index 132af41..9505348 100644
--- a/src/routes/not-found.lisp
+++ b/src/routes/not-found.lisp
@@ -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*))