███╗ ███╗ █████╗ ██████╗ ██╗
████╗ ████║██╔══██╗██╔══██╗██║
██╔████╔██║███████║██████╔╝██║
██║╚██╔╝██║██╔══██║██╔═══╝ ██║
██║ ╚═╝ ██║██║ ██║██║ ██║
╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝moon add cogna-dev/mapi{
"deps": {
"cogna-dev/mapi": "0.1.1"
}
}mapi init --spec openapi.json --out ./my-apimy-api/
├── moon.mod.json
├── src/
│ ├── generated/ # DO NOT EDIT — regenerated by mapi
│ │ ├── schemas.mbt # OpenAPI schema models
│ │ ├── operations.mbt # Typed operation inputs / outputs
│ │ ├── contract.mbt # Handler contract for server implementation
│ │ ├── router.mbt # Route registrations and server glue
│ │ ├── client.mbt # Typed HTTP client generated from spec
│ │ └── moon.pkg
│ └── handlers/ # Your code — never overwritten
│ ├── get_users.mbt
│ └── create_user.mbt
└── .mapi/
└── state.json # Spec snapshot used by diff / checkmapi generate --spec openapi.json --project ./my-apimapi check --project ./my-api
# check: all 12 handlers present ✓mapi diff --spec openapi.json --project ./my-api
# + createOrder (new handler stub will be created on next generate)
# - deleteWidget (orphaned handler — run generate to see warning)
# getUsers
# diff: +1 -1 =1mapi doctor --project ./my-api --spec openapi.json
# [ok] moon CLI found
# [ok] moon.mod.json found in ./my-api
# [ok] .mapi/state.json found in ./my-api
# [ok] spec parsed successfully: openapi.json
# doctor: all checks passed ✓// HTTP method enum — exhaustive, no stringly-typed methods
pub enum HttpMethod { Get | Post | Put | Patch | Delete | Head | Options }
// Envelopes carry the full request/response across the host boundary
pub struct RequestEnvelope { method : HttpMethod; path : String; body : String? }
pub struct ResponseEnvelope { status : Int; body : String }
// Typed error hierarchy — match on these instead of inspecting status codes
pub suberror AppError {
NotFound(String)
BadRequest(String)
Unauthorized
Forbidden
Internal(String)
}
pub suberror DecodeError {
MissingRequiredParam(String)
InvalidParamType(String, String)
InvalidBody(String)
}let app = App::new()
app.router.add(Get, "/users", fn(ctx) {
// ctx.request — the full RequestEnvelope
// ctx.params — path parameters extracted by the router
ResponseEnvelope::ok(body)
})
// Dispatch a request — call this from your host adapter
app.serve(request_envelope)let host = InMemoryHost::new(app)
// Drive any route directly
let resp = host.request(Get, "/users", None, None, None)
assert_eq!(resp.status, 200)
// Test error paths just as easily
let not_found = host.request(Get, "/users/99999", None, None, None)
assert_eq!(not_found.status, 404)┌─────────────────────────────────────────────────────┐
│ mapi CLI (main/) │
│ init · generate · check · diff · doctor │
│ Reads OpenAPI spec → writes MoonBit source │
└──────────────────────┬──────────────────────────────┘
│ generates
▼
┌─────────────────────────────────────────────────────┐
│ Generated layer (src/generated/) │
│ schemas · operations · contract · router · client │
│ Typed server glue + typed HTTP client │
└──────────────────────┬──────────────────────────────┘
│ calls
▼
┌─────────────────────────────────────────────────────┐
│ Your handlers (src/handlers/) │
│ get_users.mbt · create_order.mbt · ... │
│ Business logic — never touched by regeneration │
└──────────────────────┬──────────────────────────────┘
│ served by
▼
┌─────────────────────────────────────────────────────┐
│ Runtime core (lib/) │
│ App · Router · HttpMethod · RequestEnvelope │
│ Pure MoonBit — no I/O, no runtime assumptions │
└──────────────────────┬──────────────────────────────┘
│ adapted by
▼
┌─────────────────────────────────────────────────────┐
│ Host adapter (e.g. servers/mbt-mapi/host.ts) │
│ Bun · Node · Deno · native Wasm · InMemoryHost │
│ Translates platform I/O ↔ mapi envelopes │
└─────────────────────────────────────────────────────┘The mbt-mapi benchmark server is now a standalone MoonBit project under benchmarks/servers/mbt-mapi/, built with moon build . --target native --release. It depends on cogna-dev/mapi via a local path dependency, serves requests through moonbitlang/async/http, and routes every request through the real App::serve() pipeline while keeping benchmark-only state local to that server project.
| Item | Detail |
|---|---|
| CPU | Apple M5 |
| Memory | 32 GB |
| OS | macOS 26.3 (darwin/arm64) |
| Server | RPS | avg (ms) | p50 (ms) | p90 (ms) | p95 (ms) | max (ms) | Checks |
|---|---|---|---|---|---|---|---|
| go-gin | 3 392 | 4.31 | 0.74 | 16.39 | 22.59 | 59.91 | 100 % |
| rust-axum | 3 213 | 6.01 | 1.52 | 19.03 | 25.67 | 94.23 | 100 % |
| mbt-mapi | 467 | 188.90 | 2.18 | 869.01 | 1 113.84 | 2 780.15 | 100 % |
go-gin ████████████████████████████████▏ 3 392
rust-axum ██████████████████████████████▎ 3 213
mbt-mapi ████▎ 467
└─────────┴─────────┴─────────┴──────── RPS
0 1000 2000 3000 3500Full methodology and raw JSON results: benchmarks/
moon check # type-check the whole workspace
moon fmt # format all source files
moon build # compile
moon test # run tests
moon run main -- --help # run the CLI