ningle-fbr/README.md

97 lines
2.2 KiB
Markdown
Raw Normal View History

2024-02-13 16:09:42 +00:00
# ningle-fbr (WIP)
2024-02-27 12:09:23 +00:00
An utility for [ningle](https://github.com/fukamachi/ningle) and [jingle](https://github.com/dnaeon/cl-jingle) to enable file-based routing
2024-02-13 16:09:42 +00:00
# What is file-based routing?
File-based routing is a concept commonly used in modern web frameworks such as [Next.js](https://nextjs.org/). Instead of explicitly defining routes through configuration or code, the framework automatically sets up routes based on the file hierarchy of a particular directory (usually the "pages" or "routes" directory).
2024-02-13 16:09:42 +00:00
# Usage
To use ningle-fbr, you must use [package-inferred-system](https://asdf.common-lisp.dev/asdf/The-package_002dinferred_002dsystem-extension.html).
`/example.asd`
```lisp
(defsystem "example"
:class :package-inferred-system
:pathname "src"
:depends-on ("example/app")
```
`/src/app.lisp`
```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
          #:assign-routes))
2024-02-13 16:09:42 +00:00
(in-package #:example/app)
(defparameter *app* (make-instance 'ningle:<app>))
2024-04-11 14:34:28 +00:00
(assign-routes *app*
:directory "src/routes"
:system "example")
2024-02-13 16:09:42 +00:00
```
## Static routing
`/src/routes/index.lisp``/`
2024-04-11 14:34:28 +00:00
`/src/routes/hello.lisp``/hello`
`/src/routes/users/index.lisp``/users`
`/src/routes/nested/page.lisp``/nested/page`
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
...)
```
## Dynamic routing
A file or directory name prefixed with '=' indicates a dynamic path.
2024-04-11 14:34:28 +00:00
In the example below, the parameter `id` can be obtained from handler's params.
2024-02-13 16:09:42 +00:00
2024-04-11 14:34:28 +00:00
`/src/routes/user/=id.lisp``/user/:id`
2024-02-13 16:09:42 +00:00
2024-04-14 13:57:19 +00:00
## 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 ()
...)
```
2024-02-13 16:09:42 +00:00
# License
Licensed under MIT License. 
Copyright (c) 2024, skyizwhite.