From 79496d59002546e800fcf0ac59e176ada74a3fb0 Mon Sep 17 00:00:00 2001 From: paku Date: Sun, 14 Apr 2024 22:57:19 +0900 Subject: [PATCH] Add 404 error handling --- README.md | 18 ++++++++++++++++-- src/main.lisp | 30 +++++++++++++++++------------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 894a571..85c0b85 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ To use ningle-fbr, you must use [package-inferred-system](https://asdf.common-li `/src/app.lisp` ```lisp -(uiop:define-package #:example +(defpackage #:example (:nicknames #:example/app) (:use #:cl) (: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` ```lisp -(uiop:define-package #:example/routes/index +(defpackage #:example/routes/index (:use #:cl) (:export #:handle-get #: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` +## 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 Licensed under MIT License.  diff --git a/src/main.lisp b/src/main.lisp index f758f1f..0c3632a 100644 --- a/src/main.lisp +++ b/src/main.lisp @@ -1,36 +1,36 @@ (uiop:define-package :ningle-fbr (:nicknames #:ningle-fbr/main) (:use #:cl) - (:local-nicknames (#:re #:cl-ppcre)) - (:local-nicknames (#:ng #:ningle)) + (:import-from #:cl-ppcre) + (:import-from #:ningle) (:export #:assign-routes)) (in-package :ningle-fbr) (defun remove-file-type (namestr) - (re:regex-replace ".lisp" namestr "")) + (cl-ppcre:regex-replace ".lisp" namestr "")) (defun remove-index (url) (if (string= url "/index") "/" - (re:regex-replace "/index" url ""))) + (cl-ppcre:regex-replace "/index" url ""))) (defun replace-dynamic-annotation (url) - (re:regex-replace "=" url ":")) + (cl-ppcre:regex-replace "=" url ":")) (defun format-url (url) (replace-dynamic-annotation (remove-index url))) (defun pathname->url (pathname dir-namestring) (format-url - (re:regex-replace dir-namestring - (remove-file-type (namestring pathname)) - ""))) + (cl-ppcre:regex-replace dir-namestring + (remove-file-type (namestring pathname)) + ""))) (defun pathname->package (pathname system-path-namestring system-prefix) (string-upcase - (re:regex-replace system-path-namestring - (remove-file-type (namestring pathname)) - system-prefix))) + (cl-ppcre:regex-replace system-path-namestring + (remove-file-type (namestring pathname)) + system-prefix))) (defun dir->pathnames (dir) (directory (concatenate 'string @@ -55,10 +55,14 @@ (defun assign-routes (app &key directory system) (loop :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 :for method :in *http-request-methods* :do (let ((handler (find-symbol (concatenate 'string "HANDLE-" (string method)) pkg))) (when handler - (setf (ng:route app url :method method) handler)))))) + (setf (ningle:route app url :method method) handler))))))