File-based router for ningle
Find a file
paku b1dee071ff Refactor (#4)
* Update system info

* Update qlfile

* Organize code

* Add test

* Implement uri-mapper and package-mapper

* Change type of package from string to keyword

* Implement router
2024-12-20 15:09:24 +09:00
.github/workflows Use fiveam instead of rove 2024-06-13 21:00:50 +09:00
src Refactor (#4) 2024-12-20 15:09:24 +09:00
tests Refactor (#4) 2024-12-20 15:09:24 +09:00
.gitignore Setup lisp env 2024-02-13 16:32:48 +09:00
LICENSE Update README 2024-02-14 01:46:42 +09:00
ningle-fbr-test.asd Refactor (#4) 2024-12-20 15:09:24 +09:00
ningle-fbr.asd Refactor (#4) 2024-12-20 15:09:24 +09:00
qlfile Refactor (#4) 2024-12-20 15:09:24 +09:00
qlfile.lock Refactor (#4) 2024-12-20 15:09:24 +09:00
README.md Amend README 2024-05-30 08:07:30 +09:00

ningle-fbr (WIP)

A utility for ningle and jingle to enable file-based routing.

What is File-Based Routing?

File-based routing is a concept commonly used in modern web frameworks such as Next.js. Instead of explicitly defining routes through configuration or code, the framework automatically sets up routes based on the file hierarchy of a specific directory (usually the "pages" or "routes" directory).

Usage

To use ningle-fbr, you need to use package-inferred-system.

/example.asd

(defsystem "example"
  :class :package-inferred-system
  :pathname "src"
  :depends-on ("example/app"))

/src/app.lisp

(defpackage #:example
  (:nicknames #:example/app)
  (:use #:cl)
  (:import-from #:ningle)
  (:import-from #:ningle-fbr
                #:assign-routes))
(in-package #:example/app)

(defparameter *app* (make-instance 'ningle:<app>))

(assign-routes *app*
               :directory "src/routes"
               :system "example")

Static Routing

/src/routes/index.lisp/

/src/routes/hello.lisp/hello

/src/routes/users/index.lisp/users

/src/routes/nested/page.lisp/nested/page

(defpackage #:example/routes/index
  (:use #:cl)
  (:export #:handle-get
           #:handle-post
           #:handle-put
           #:handle-delete))
(in-package #:example/routes/index)

(defun handle-get (params)
  ...)

(defun handle-post (params)
  ...)

(defun handle-put (params)
  ...)

(defun handle-delete (params)
  ...)

Dynamic Routing

A file or directory name prefixed with '=' indicates a dynamic path.

In the example below, the parameter id can be obtained from the 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 the handle-not-found function and export it.

(defpackage #:example/routes/not-found
  (:use #:cl)
  (:export #:handle-not-found))
(in-package #:example/routes/not-found)
  
(defun handle-not-found ()
  ...)

License

Licensed under the MIT License.

© 2024, skyizwhite.