This commit is contained in:
Akira Tempaku 2025-03-29 03:46:58 +09:00
commit fff8929b0e
Signed by: paku
GPG key ID: 5B4E8402BCC50607
4 changed files with 9 additions and 49 deletions

View file

@ -3,15 +3,14 @@
(:import-from #:jingle
#:make-app
#:install-middleware
#:static-path
#:configure)
(:import-from #:ningle-fbr
#:set-routes)
(:import-from #:hp/middlewares/recoverer
#:*recoverer*)
(:import-from #:hp/middlewares/trailing-slash
(:import-from #:lack-mw
#:*trim-trailing-slash*)
(:import-from #:hp/middlewares/public-server
#:*public-server*)
(:import-from #:hp/renderer)
(:export #:*app*))
(in-package #:hp/app)
@ -21,7 +20,8 @@
(set-routes app :system :hp :target-dir-path "routes")
(install-middleware app *recoverer*)
(install-middleware app *trim-trailing-slash*)
(install-middleware app *public-server*)
(static-path app "/img/" "public/img/")
(static-path app "/style/" "public/style/")
(configure app)))
*app*

View file

@ -1,18 +0,0 @@
(defpackage #:hp/middlewares/public-server
(:use #:cl)
(:import-from #:lack.middleware.static
#:*lack-middleware-static*)
(:export #:*public-server*))
(in-package #:hp/middlewares/public-server)
(defun exist-asset-file-p (path)
(let ((pathname (probe-file (concatenate 'string "public" path))))
(and pathname (pathname-name pathname))))
(defparameter *public-server*
(lambda (app)
(funcall *lack-middleware-static*
app
:path (lambda (path)
(and (exist-asset-file-p path) path))
:root (asdf:system-relative-pathname :hp "public/"))))

View file

@ -1,25 +0,0 @@
(defpackage #:hp/middlewares/trailing-slash
(:use #:cl)
(:import-from :quri)
(:export #:*trim-trailing-slash*))
(in-package #:hp/middlewares/trailing-slash)
(defun last-string (str)
(subseq str (- (length str) 1)))
(defparameter *trim-trailing-slash*
(lambda (app)
(lambda (env)
(let* ((req-uri (quri:uri (getf env :request-uri)))
(req-path (quri:uri-path req-uri))
(req-method (getf env :request-method))
(response (funcall app env))
(res-status (first response)))
(if (and (= res-status 404)
(eq req-method :get)
(not (string= req-path "/"))
(string= (last-string req-path) "/"))
(let ((red-uri (quri:copy-uri req-uri
:path (string-right-trim "/" req-path))))
`(301 (:location ,(quri:render-uri red-uri))))
response)))))

View file

@ -11,8 +11,11 @@
(defcomp ~page ()
(hsx
(section
(h1 "404 Not Found"))))
(section :class "flex flex-col h-full items-center justify-center gap-y-6"
(h1 :class "font-bold text-2xl"
"404 Not Found")
(a :href "/" :class "text-lg text-pink-500 hover:underline"
"Back to TOP"))))
(defun handle-not-found ()
(jingle:set-response-status :not-found)