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
2024-12-24 17:47:46 +00:00
This software is currently in ALPHA stage. Its APIs are subject to change.
2024-12-20 06:09:37 +00:00
2024-12-24 17:47:46 +00:00
Check the [release notes ](https://github.com/skyizwhite/ningle-fbr/releases ) for the latest updates.
2024-02-13 16:09:42 +00:00
2024-05-29 23:07:30 +00:00
## What is File-Based Routing?
2024-02-13 18:57:19 +00:00
2024-12-24 17:47:46 +00:00
File-based routing automatically creates URL routes based on a project’ s file and directory structure. Instead of manually configuring routes in a separate routing file, each file in a designated directory (e.g., `pages` or `routes` ) becomes a route. This simplifies development and maintenance since adding, removing, or renaming a route is often just a matter of modifying a file’ s name or location.
2024-02-13 18:57:19 +00:00
2024-05-29 23:07:30 +00:00
## Usage
2024-02-13 16:09:42 +00:00
2024-12-24 17:47:46 +00:00
To use ningle-fbr, set up your project using the [package-inferred-system ](https://asdf.common-lisp.dev/asdf/The-package_002dinferred_002dsystem-extension.html ) style.
2024-02-13 16:09:42 +00:00
2024-12-24 17:47:46 +00:00
**Example directory structure**:
2024-12-20 06:30:29 +00:00
```
example.asd
src/
app.lisp
routes/
index.lisp
hello.lisp
users/
index.lisp
2024-12-22 09:33:49 +00:00
< id > .lisp
2024-12-20 06:30:29 +00:00
nested/
page.lisp
```
**`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:30:29 +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-12-20 06:30:29 +00:00
(:import-from #:ningle -fbr #: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-24 17:47:46 +00:00
Routes are determined by packages located under `:example/routes` . The package’ s name corresponds directly to a URL path:
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:30:29 +00:00
**`/src/routes/index.lisp`**:
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-12-24 17:47:46 +00:00
;; Implement GET logic here
2024-12-20 06:30:29 +00:00
)
2024-02-13 16:09:42 +00:00
2024-04-14 07:39:43 +00:00
(defun handle-post (params)
2024-12-24 17:47:46 +00:00
;; Implement POST logic here
2024-12-20 06:30:29 +00:00
)
2024-02-13 16:09:42 +00:00
2024-04-14 07:39:43 +00:00
(defun handle-put (params)
2024-12-24 17:47:46 +00:00
;; Implement PUT logic here
2024-12-20 06:30:29 +00:00
)
2024-02-13 16:09:42 +00:00
2024-04-14 07:39:43 +00:00
(defun handle-delete (params)
2024-12-24 17:47:46 +00:00
;; Implement DELETE logic here
2024-12-20 06:30:29 +00:00
)
2024-02-13 16:09:42 +00:00
```
2024-12-20 06:30:29 +00:00
Handlers are chosen based on the HTTP method. If `handle-get` is exported, it will be called for `GET` requests on `/` .
2024-05-29 23:07:30 +00:00
### Dynamic Routing
2024-02-13 16:09:42 +00:00
2024-12-20 06:30:29 +00:00
To define dynamic routes, use `<>` in the file name to indicate URL parameters.
2024-02-13 16:09:42 +00:00
2024-12-20 06:30:29 +00:00
For example:
`:example/routes/user/<id>` → `/user/:id`
2024-02-13 16:09:42 +00:00
2024-12-20 06:30:29 +00:00
If a request comes in at `/user/123` , `params` will include `:id "123"` .
2024-02-13 16:09:42 +00:00
2024-12-20 06:30:29 +00:00
### 404 Handling
2024-04-14 13:57:19 +00:00
2024-12-24 17:47:46 +00:00
To handle 404 (Not Found) errors, create a special package named `:example/routes/not-found` and define `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-12-20 06:30:29 +00:00
;; Implement custom 404 logic here
)
2024-04-14 13:57:19 +00:00
```
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.