Skip to content

Commit 4e6a6bd

Browse files
committed
feat(routing): add route groups with shared middleware support
Implement route grouping functionality to organize related routes under a common path prefix with shared middleware. This improves code organization and reduces duplication for API endpoints. Update version to 0.3.6 and document new feature in README with usage examples.
1 parent 2686d39 commit 4e6a6bd

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,31 @@ app.get("/async_data", async fn(event) {
6666
})
6767
```
6868

69+
### Route Groups
70+
71+
Group related routes under a common base path with shared middleware:
72+
73+
```moonbit
74+
app.group("/api", group => {
75+
// Add group-level middleware
76+
group.use_middleware(event => println(
77+
"🔒 API Group Middleware: \{event.req.reqMethod} \{event.req.url}",
78+
))
79+
80+
// Routes under /api prefix
81+
group.get("/hello", _ => "Hello from API!")
82+
group.get("/users", _ => { "users": ["Alice", "Bob"] })
83+
group.post("/data", e => e.req.body.to_json())
84+
})
85+
```
86+
87+
This creates routes:
88+
- `GET /api/hello`
89+
- `GET /api/users`
90+
- `POST /api/data`
91+
92+
All routes in the group will execute the group middleware in addition to any global middleware.
93+
6994
## Example usage
7095

7196
```moonbit
@@ -82,6 +107,20 @@ fn main {
82107
// Text Response
83108
..get("/", _event => "⚡️ Tadaa!")
84109
110+
// Route Groups with middleware
111+
..group("/api", group => {
112+
// Add group-level middleware
113+
group.use_middleware(event => println(
114+
"🔒 API Group Middleware: \{event.req.reqMethod} \{event.req.url}",
115+
))
116+
group.get("/hello", _ => "Hello from API!")
117+
group.get("/json", _ => {
118+
"name": "John Doe",
119+
"age": 30,
120+
"city": "New York",
121+
})
122+
})
123+
85124
// JSON Response
86125
..get("/json", _event => { "name": "John Doe", "age": 30, "city": "New York" })
87126
@@ -144,6 +183,8 @@ fn main {
144183
| `/hello/**` | `/hello/foo/bar` | `_: "foo/bar"` |
145184
| `/users/:id/posts/:postId` | `/users/123/posts/456` | `id: "123"`, `postId: "456"` |
146185
| `/api/**` | `/api/v1/users/123` | `_: "v1/users/123"` |
186+
| `group("/api", ...).get("/hello")` | `/api/hello` | - |
187+
| `group("/users", ...).get("/:id")` | `/users/123` | `id: "123"` |
147188

148189
🙌快来吧!🙌
149190

moon.mod.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "oboard/mocket",
3-
"version": "0.3.5",
3+
"version": "0.3.6",
44
"deps": {
55
"rami3l/js-ffi": "0.3.1",
66
"yj-qin/regexp": "0.3.6"

0 commit comments

Comments
 (0)