Skip to content

Commit 75642d8

Browse files
committed
feat(body-reader): add example for mocket body reader implementation
Implement a basic example demonstrating how to create a custom body reader for the mocket framework. Includes package configuration and main example showing CSV body parsing functionality.
1 parent a68c4eb commit 75642d8

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

src/examples/body-reader/main.mbt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
///|
2+
using @mocket {type HttpResponse}
3+
4+
///|
5+
struct Csv(String)
6+
7+
///|
8+
impl @mocket.BodyReader for Csv with from_request(req) -> Csv {
9+
let text : String = req.body()
10+
Csv(text)
11+
}
12+
13+
///|
14+
fn main {
15+
let app = @mocket.new()
16+
17+
// Text Response
18+
app.post("/", event => {
19+
let csv : Csv = event.req.body() catch { _ => Csv("") }
20+
HttpResponse::new(OK).body(csv.0)
21+
})
22+
23+
// Print Server URL
24+
for path in app.mappings.keys() {
25+
println("\{path.0} http://localhost:4000\{path.1}")
26+
}
27+
28+
// Serve
29+
app.serve(port=4000)
30+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"is-main": true,
3+
"import": ["oboard/mocket"]
4+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Generated using `moon info`, DON'T EDIT IT
2+
package "oboard/mocket/examples/responder"
3+
4+
import(
5+
"moonbitlang/core/json"
6+
)
7+
8+
// Values
9+
10+
// Errors
11+
12+
// Types and methods
13+
type Person
14+
pub impl ToJson for Person
15+
pub impl @json.FromJson for Person
16+
17+
// Type aliases
18+
19+
// Traits
20+

0 commit comments

Comments
 (0)