File-based router for ningle
Find a file
2024-12-20 15:15:04 +09:00
.github/workflows Use fiveam instead of rove 2024-06-13 21:00:50 +09:00
src Fix 2024-12-20 15:15:04 +09:00
tests Fix 2024-12-20 15:15:04 +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 Fix 2024-12-20 15:15:04 +09:00

ningle-fbr

A file-based router for ningle.

Warning

This software is still in ALPHA quality. The APIs are likely to change.

Please check the release notes for updates.

What is File-Based Routing?

File-based routing is a concept widely used in modern web frameworks like Next.js. Instead of explicitly defining routes in code or configuration, routes are automatically generated based on the file and directory structure within a designated folder (often called "pages" or "routes").

Usage

To use ningle-fbr, you need to set up your project based on the 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
                #:set-routes))
(in-package #:example/app)

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

(set-routes *app* :system :example :target-dir-path "routes")

Static Routing

Routes are generated automatically from packages under :example/routes:

  • :example/routes/index/
  • :example/routes/hello/hello
  • :example/routes/users/index/users
  • :example/routes/nested/page/nested/page

/src/routes/index.lisp example:

(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

Dynamic routes use < > to indicate parameters. For example:

/src/routes/user/<id>.lisp/user/:id

In the handlers, you can access the value of id through the params argument.

Not Found Error

:example/routes/not-found is a special package for handling 404 errors. Implement and export handle-not-found:

(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.