ningle-fbr/README.md

100 lines
2.4 KiB
Markdown
Raw Normal View History

2024-12-20 06:09:37 +00:00
# ningle-fbr
2024-02-27 12:09:23 +00:00
2024-12-20 06:09:37 +00:00
A file-based router for [ningle](https://github.com/fukamachi/ningle).
## Warning
This software is still in ALPHA quality. The APIs are likely to change.
Please check the [release notes](https://github.com/skyizwhite/ningle-fbr/releases) for updates.
2024-02-13 16:09:42 +00:00
2024-05-29 23:07:30 +00:00
## What is File-Based Routing?
2024-12-20 06:09:37 +00:00
File-based routing is a concept widely used in modern web frameworks like [Next.js](https://nextjs.org/). 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").
2024-05-29 23:07:30 +00:00
## Usage
2024-02-13 16:09:42 +00:00
2024-12-20 06:09:37 +00:00
To use ningle-fbr, you need to set up your project based on the [package-inferred-system](https://asdf.common-lisp.dev/asdf/The-package_002dinferred_002dsystem-extension.html).
2024-02-13 16:09:42 +00:00
2024-12-20 06:09:37 +00:00
`/example.asd`:
2024-02-13 16:09:42 +00:00
```lisp
(defsystem "example"
:class :package-inferred-system
:pathname "src"
2024-05-29 23:07:30 +00:00
:depends-on ("example/app"))
2024-02-13 16:09:42 +00:00
```
2024-12-20 06:09:37 +00:00
`/src/app.lisp`:
2024-02-13 16:09:42 +00:00
```lisp
2024-04-14 13:57:19 +00:00
(defpackage #:example
2024-02-13 16:09:42 +00:00
(:nicknames #:example/app)
(:use #:cl)
(:import-from #:ningle)
2024-04-11 14:34:28 +00:00
(:import-from #:ningle-fbr
2024-12-20 06:09:37 +00:00
#:set-routes))
2024-02-13 16:09:42 +00:00
(in-package #:example/app)
(defparameter *app* (make-instance 'ningle:<app>))
2024-12-20 06:09:37 +00:00
(set-routes *app* :system :example :target-dir-path "routes")
2024-02-13 16:09:42 +00:00
```
2024-05-29 23:07:30 +00:00
### Static Routing
2024-02-13 16:09:42 +00:00
2024-12-20 06:09:37 +00:00
Routes are generated automatically from packages under `:example/routes`:
2024-04-11 14:34:28 +00:00
2024-12-20 06:09:37 +00:00
- `:example/routes/index``/`
- `:example/routes/hello``/hello`
- `:example/routes/users/index``/users`
- `:example/routes/nested/page``/nested/page`
2024-04-11 14:34:28 +00:00
2024-12-20 06:09:37 +00:00
`/src/routes/index.lisp` example:
2024-02-13 16:09:42 +00:00
```lisp
2024-04-14 13:57:19 +00:00
(defpackage #:example/routes/index
2024-02-13 16:09:42 +00:00
(:use #:cl)
2024-04-14 07:39:43 +00:00
(:export #:handle-get
#:handle-post
#:handle-put
#:handle-delete))
2024-02-13 16:09:42 +00:00
(in-package #:example/routes/index)
2024-04-14 07:39:43 +00:00
(defun handle-get (params)
2024-02-13 16:09:42 +00:00
...)
2024-04-14 07:39:43 +00:00
(defun handle-post (params)
2024-02-13 16:09:42 +00:00
...)
2024-04-14 07:39:43 +00:00
(defun handle-put (params)
2024-02-13 16:09:42 +00:00
...)
2024-04-14 07:39:43 +00:00
(defun handle-delete (params)
2024-02-13 16:09:42 +00:00
...)
```
2024-05-29 23:07:30 +00:00
### Dynamic Routing
2024-02-13 16:09:42 +00:00
2024-12-20 06:15:04 +00:00
Dynamic routes use `< >` to indicate parameters. For example:
2024-02-13 16:09:42 +00:00
2024-12-20 06:15:04 +00:00
`/src/routes/user/<id>.lisp``/user/:id`
2024-02-13 16:09:42 +00:00
2024-12-20 06:09:37 +00:00
In the handlers, you can access the value of `id` through the `params` argument.
2024-02-13 16:09:42 +00:00
2024-05-29 23:07:30 +00:00
### Not Found Error
2024-04-14 13:57:19 +00:00
2024-12-20 06:09:37 +00:00
`:example/routes/not-found` is a special package for handling 404 errors. Implement and export `handle-not-found`:
2024-04-14 13:57:19 +00:00
```lisp
(defpackage #:example/routes/not-found
(:use #:cl)
(:export #:handle-not-found))
(in-package #:example/routes/not-found)
2024-12-20 06:09:37 +00:00
2024-04-14 13:57:19 +00:00
(defun handle-not-found ()
...)
```
2024-05-29 23:07:30 +00:00
## License
2024-02-13 16:09:42 +00:00
2024-05-29 23:07:30 +00:00
Licensed under the MIT License.
2024-02-13 16:09:42 +00:00
2024-12-20 06:09:37 +00:00
© 2024, skyizwhite.