@@ -36,192 +36,6 @@ Then visit http://localhost:4000
3636
3737Minimum Example: https://github.com/oboard/mocket_example
3838
39- ## Features
40-
41- ### Dynamic Routes
42-
43- Support named parameters with ` :param ` syntax:
44-
45- ``` moonbit
46- app.get("/hello/:name", event => {
47- let name = event.params.get("name").or("World")
48- Text("Hello, \{name}!")
49- })
50- ```
51-
52- ### Wildcard Routes
53-
54- Support single and double wildcards:
55-
56- ``` moonbit
57- // Single wildcard - matches one path segment
58- app.get("/hello/*", event => {
59- let name = event.params.get("_").or("World")
60- Text("Hello, \{name}!")
61- })
62-
63- // Double wildcard - matches multiple path segments
64- app.get("/hello/**", event => {
65- let path = event.params.get("_").or("")
66- Text("Hello, \{path}!")
67- })
68- ```
69-
70- ### Async Support
71-
72- The library supports async/await for I/O operations:
73-
74- ### Async /GET Example
75-
76- ``` moonbit
77- // async json data example
78- app.get("/async_data", async fn(event) {
79- Json({ "name": "John Doe", "age": 30, "city": "New York" })
80- })
81- ```
82-
83- ### Route Groups
84-
85- Group related routes under a common base path with shared middleware:
86-
87- ``` moonbit
88- app.group("/api", group => {
89- // Add group-level middleware
90- group.use_middleware(event => println(
91- "🔒 API Group Middleware: \{event.req.reqMethod} \{event.req.url}",
92- ))
93-
94- // Routes under /api prefix
95- group.get("/hello", _ => Text("Hello from API!"))
96- group.get("/users", _ => Json({ "users": ["Alice", "Bob"] }))
97- group.post("/data", e => e.req.body)
98- })
99- ```
100-
101- This creates routes:
102- - ` GET /api/hello `
103- - ` GET /api/users `
104- - ` POST /api/data `
105-
106- All routes in the group will execute the group middleware in addition to any global middleware.
107-
108- ### WebSocket Support
109-
110- Mocket provides built-in WebSocket support with a simple event-based API.
111-
112- ``` moonbit
113- app.ws("/ws", event => match event {
114- Open(peer) => println("WS open: " + peer.to_string())
115- Message(peer, body) => {
116- let msg = match body {
117- Text(s) => s.to_string()
118- _ => ""
119- }
120- println("WS message: " + msg)
121- peer.send(msg)
122- }
123- Close(peer) => println("WS close: " + peer.to_string())
124- })
125- ```
126-
127- ## Example usage
128-
129- ``` moonbit
130- let app = @mocket.new(logger=@mocket.new_debug_logger())
131-
132- // Register global middleware
133- app
134- ..use_middleware(event => println(
135- "📝 Request: \{event.req.http_method} \{event.req.url}",
136- ))
137-
138- // Text Response
139- ..get("/", _event => Text("⚡️ Tadaa!"))
140-
141- // Hello World
142- ..on("GET", "/hello", _ => Text("Hello world!"))
143- ..group("/api", group => {
144- // 添加组级中间件
145- group.use_middleware(event => println(
146- "🔒 API Group Middleware: \{event.req.http_method} \{event.req.url}",
147- ))
148- group.get("/hello", _ => Text("Hello world!"))
149- group.get("/json", _ => Json({
150- "name": "John Doe",
151- "age": 30,
152- "city": "New York",
153- }))
154- })
155-
156- // JSON Response
157- ..get("/json", _event => Json({
158- "name": "John Doe",
159- "age": 30,
160- "city": "New York",
161- }))
162-
163- // Async Response
164- ..get("/async_data", async fn(_event) noraise {
165- Json({ "name": "John Doe", "age": 30, "city": "New York" })
166- })
167-
168- // Dynamic Routes
169- // /hello2/World = Hello, World!
170- ..get("/hello/:name", event => {
171- let name = event.params.get("name").unwrap_or("World")
172- Text("Hello, \{name}!")
173- })
174- // /hello2/World = Hello, World!
175- ..get("/hello2/*", event => {
176- let name = event.params.get("_").unwrap_or("World")
177- Text("Hello, \{name}!")
178- })
179-
180- // Wildcard Routes
181- // /hello3/World/World = Hello, World/World!
182- ..get("/hello3/**", event => {
183- let name = event.params.get("_").unwrap_or("World")
184- Text("Hello, \{name}!")
185- })
186-
187- // Echo Server
188- ..post("/echo", e => e.req.body)
189-
190- // 404 Page
191- ..get("/404", e => {
192- e.res.status_code = 404
193- HTML(
194- (
195- #|<html>
196- #|<body>
197- #| <h1>404</h1>
198- #|</body>
199- #|</html>
200- ),
201- )
202- })
203-
204- // Serve
205- ..serve(port=4000)
206-
207- // Print Server URL
208- for path in app.mappings.keys() {
209- println("\{path.0} http://localhost:4000\{path.1}")
210- }
211- ```
212-
213- ## Route Matching Examples
214-
215- | Route Pattern | URL | Parameters |
216- | ---------------| -----| ------------|
217- | ` /hello/:name ` | ` /hello/world ` | ` name: "world" ` |
218- | ` /hello/* ` | ` /hello/world ` | ` _: "world" ` |
219- | ` /hello/** ` | ` /hello/foo/bar ` | ` _: "foo/bar" ` |
220- | ` /users/:id/posts/:postId ` | ` /users/123/posts/456 ` | ` id: "123" ` , ` postId: "456" ` |
221- | ` /api/** ` | ` /api/v1/users/123 ` | ` _: "v1/users/123" ` |
222- | ` group("/api", ...).get("/hello") ` | ` /api/hello ` | - |
223- | ` group("/users", ...).get("/:id") ` | ` /users/123 ` | ` id: "123" ` |
224-
22539## Q & A
22640### Why not moonbitlang/async ?
22741
0 commit comments