Add 404 error handling
This commit is contained in:
parent
e84b170a03
commit
79496d5900
2 changed files with 33 additions and 15 deletions
18
README.md
18
README.md
|
@ -20,7 +20,7 @@ To use ningle-fbr, you must use [package-inferred-system](https://asdf.common-li
|
||||||
|
|
||||||
`/src/app.lisp`
|
`/src/app.lisp`
|
||||||
```lisp
|
```lisp
|
||||||
(uiop:define-package #:example
|
(defpackage #:example
|
||||||
(:nicknames #:example/app)
|
(:nicknames #:example/app)
|
||||||
(:use #:cl)
|
(:use #:cl)
|
||||||
(:import-from #:ningle)
|
(:import-from #:ningle)
|
||||||
|
@ -46,7 +46,7 @@ To use ningle-fbr, you must use [package-inferred-system](https://asdf.common-li
|
||||||
`/src/routes/nested/page.lisp` → `/nested/page`
|
`/src/routes/nested/page.lisp` → `/nested/page`
|
||||||
|
|
||||||
```lisp
|
```lisp
|
||||||
(uiop:define-package #:example/routes/index
|
(defpackage #:example/routes/index
|
||||||
(:use #:cl)
|
(:use #:cl)
|
||||||
(:export #:handle-get
|
(:export #:handle-get
|
||||||
#:handle-post
|
#:handle-post
|
||||||
|
@ -75,6 +75,20 @@ In the example below, the parameter `id` can be obtained from handler's params.
|
||||||
|
|
||||||
`/src/routes/user/=id.lisp` → `/user/:id`
|
`/src/routes/user/=id.lisp` → `/user/:id`
|
||||||
|
|
||||||
|
## Not found error
|
||||||
|
|
||||||
|
`not-found.lisp` is a special file to handle 404 errors. Implement `handle-not-found` function and export it.
|
||||||
|
|
||||||
|
```lisp
|
||||||
|
(defpackage #:example/routes/not-found
|
||||||
|
(:use #:cl)
|
||||||
|
(:export #:handle-not-found))
|
||||||
|
(in-package #:example/routes/not-found)
|
||||||
|
|
||||||
|
(defun handle-not-found ()
|
||||||
|
...)
|
||||||
|
```
|
||||||
|
|
||||||
# License
|
# License
|
||||||
|
|
||||||
Licensed under MIT License.
|
Licensed under MIT License.
|
||||||
|
|
|
@ -1,36 +1,36 @@
|
||||||
(uiop:define-package :ningle-fbr
|
(uiop:define-package :ningle-fbr
|
||||||
(:nicknames #:ningle-fbr/main)
|
(:nicknames #:ningle-fbr/main)
|
||||||
(:use #:cl)
|
(:use #:cl)
|
||||||
(:local-nicknames (#:re #:cl-ppcre))
|
(:import-from #:cl-ppcre)
|
||||||
(:local-nicknames (#:ng #:ningle))
|
(:import-from #:ningle)
|
||||||
(:export #:assign-routes))
|
(:export #:assign-routes))
|
||||||
(in-package :ningle-fbr)
|
(in-package :ningle-fbr)
|
||||||
|
|
||||||
(defun remove-file-type (namestr)
|
(defun remove-file-type (namestr)
|
||||||
(re:regex-replace ".lisp" namestr ""))
|
(cl-ppcre:regex-replace ".lisp" namestr ""))
|
||||||
|
|
||||||
(defun remove-index (url)
|
(defun remove-index (url)
|
||||||
(if (string= url "/index")
|
(if (string= url "/index")
|
||||||
"/"
|
"/"
|
||||||
(re:regex-replace "/index" url "")))
|
(cl-ppcre:regex-replace "/index" url "")))
|
||||||
|
|
||||||
(defun replace-dynamic-annotation (url)
|
(defun replace-dynamic-annotation (url)
|
||||||
(re:regex-replace "=" url ":"))
|
(cl-ppcre:regex-replace "=" url ":"))
|
||||||
|
|
||||||
(defun format-url (url)
|
(defun format-url (url)
|
||||||
(replace-dynamic-annotation (remove-index url)))
|
(replace-dynamic-annotation (remove-index url)))
|
||||||
|
|
||||||
(defun pathname->url (pathname dir-namestring)
|
(defun pathname->url (pathname dir-namestring)
|
||||||
(format-url
|
(format-url
|
||||||
(re:regex-replace dir-namestring
|
(cl-ppcre:regex-replace dir-namestring
|
||||||
(remove-file-type (namestring pathname))
|
(remove-file-type (namestring pathname))
|
||||||
"")))
|
"")))
|
||||||
|
|
||||||
(defun pathname->package (pathname system-path-namestring system-prefix)
|
(defun pathname->package (pathname system-path-namestring system-prefix)
|
||||||
(string-upcase
|
(string-upcase
|
||||||
(re:regex-replace system-path-namestring
|
(cl-ppcre:regex-replace system-path-namestring
|
||||||
(remove-file-type (namestring pathname))
|
(remove-file-type (namestring pathname))
|
||||||
system-prefix)))
|
system-prefix)))
|
||||||
|
|
||||||
(defun dir->pathnames (dir)
|
(defun dir->pathnames (dir)
|
||||||
(directory (concatenate 'string
|
(directory (concatenate 'string
|
||||||
|
@ -56,9 +56,13 @@
|
||||||
(loop
|
(loop
|
||||||
:for (url . pkg) :in (dir->urls-and-packages directory system)
|
:for (url . pkg) :in (dir->urls-and-packages directory system)
|
||||||
:do (ql:quickload pkg)
|
:do (ql:quickload pkg)
|
||||||
|
(if (string= url "/not-found")
|
||||||
|
(let ((handler (find-symbol "HANDLE-NOT-FOUND" pkg)))
|
||||||
|
(defmethod ningle:not-found ((app ningle:app))
|
||||||
|
(funcall handler))))
|
||||||
(loop
|
(loop
|
||||||
:for method :in *http-request-methods*
|
:for method :in *http-request-methods*
|
||||||
:do (let ((handler (find-symbol (concatenate 'string "HANDLE-" (string method))
|
:do (let ((handler (find-symbol (concatenate 'string "HANDLE-" (string method))
|
||||||
pkg)))
|
pkg)))
|
||||||
(when handler
|
(when handler
|
||||||
(setf (ng:route app url :method method) handler))))))
|
(setf (ningle:route app url :method method) handler))))))
|
||||||
|
|
Loading…
Reference in a new issue