Improve
This commit is contained in:
parent
6a08fb4677
commit
fff8929b0e
4 changed files with 9 additions and 49 deletions
src
|
@ -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*
|
||||
|
|
|
@ -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/"))))
|
|
@ -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)))))
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue