You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<asideclass="sidebar"><divclass="brand"><spanclass="dot"></span>moonapi</div><pclass="brand-sub">MoonBit web framework — API reference</p><navclass="side-nav">
<main><headerclass="hero"><h1>moonapi</h1><pclass="tag">A typed web framework for MoonBit — FastAPI-style routing and multi-version OpenAPI, on the moonasgi SEAM. Backend-agnostic — the async transport lives in the server (mooncat) that runs the app.</p><divclass="badges"><ahref="https://github.com/Lfan-ke/moonapi/actions"><imgalt="CI" src="https://img.shields.io/github/actions/workflow/status/Lfan-ke/moonapi/ci.yml?branch=master&label=CI&logo=github"></a><imgalt="tests" src="https://img.shields.io/badge/tests-3%20passing%20%C3%974%20backends-0ca678"><ahref="https://github.com/Lfan-ke/moonapi"><imgalt="GitHub" src="https://img.shields.io/badge/GitHub-source-24292f?logo=github"></a><imgalt="license" src="https://img.shields.io/badge/license-Apache--2.0-6d5efc"></div><divclass="install"><spanclass="prompt">$</span><code>moon add Lfan-ke/moonapi</code><buttonclass="copy" data-copy="moon add Lfan-ke/moonapi">copy</button></div><divclass="contract"><h2><spanclass="spark">✶</span> The contract at a glance</h2><pre><spanclass="k">let</span> app = <spanclass="ty">App</span>::new()
<spanclass="k">let</span> spec = app.openapi_json(version=<spanclass="ty">OpenApi31</span>) // also <spanclass="ty">OpenApi30</span> / <spanclass="ty">Swagger20</span>
98
+
@mooncat.serve(app.to_asgi(), port=8000) // run it (native)</pre></div></header>
99
+
<sectionclass="pkg" id="app"><h2><spanclass="at">§</span>Application & routing</h2><pclass="pdesc">The App, its route builders (get / post / ...), :param path matching, the Context, and text / json response helpers - plus App::to_asgi to run anywhere.</p>
100
+
<divclass="item" data-k="enum"><spanclass="kind">enum</span><preclass="sig"><spanclass="k">enum</span><spanclass="ty">Method</span></pre><pclass="doc">HTTP methods a moonapi route can bind to.</p></div>
101
+
<divclass="item" data-k="struct"><spanclass="kind">struct</span><preclass="sig"><spanclass="k">struct</span><spanclass="ty">Context</span></pre><pclass="doc">The per-request context handed to a handler: the raw request plus the path parameters extracted from the matched route (<code>:name</code> segments).</p></div>
102
+
<divclass="item" data-k="fn"><spanclass="kind">fn</span><preclass="sig"><spanclass="k">fn</span><spanclass="ty">Context</span>::param(self : <spanclass="ty">Context</span>, name : <spanclass="ty">String</span>) <spanclass="op">-></span><spanclass="ty">String</span><spanclass="op">?</span></pre><pclass="doc">Look up a path parameter by name.</p></div>
<divclass="item" data-k="struct"><spanclass="kind">struct</span><preclass="sig"><spanclass="k">struct</span><spanclass="ty">App</span></pre><pclass="doc">A moonapi application: an ordered set of routes that compiles to a moonasgi <code>AsgiApp</code> any server (mooncat) can run.</p></div>
105
+
<divclass="item" data-k="fn"><spanclass="kind">fn</span><preclass="sig"><spanclass="k">fn</span><spanclass="ty">App</span>::new() <spanclass="op">-></span><spanclass="ty">App</span></pre><pclass="doc">Create an empty application.</p></div>
106
+
<divclass="item" data-k="fn"><spanclass="kind">fn</span><preclass="sig"><spanclass="k">fn</span><spanclass="ty">App</span>::route( self : <spanclass="ty">App</span>, verb : <spanclass="ty">Method</span>, path : <spanclass="ty">String</span>, handler : <spanclass="ty">ApiHandler</span>, summary<spanclass="op">?</span> : <spanclass="ty">String</span> = "") <spanclass="op">-></span><spanclass="ty">Unit</span></pre><pclass="doc">Register a route for an explicit method.</p></div>
<divclass="item" data-k="fn"><spanclass="kind">fn</span><preclass="sig"><spanclass="k">fn</span> json(status : <spanclass="ty">Int</span>, value : <spanclass="ty">Json</span>) <spanclass="op">-></span> @moonasgi.<spanclass="ty">Response</span></pre><pclass="doc">A JSON response serialised from a <code>Json</code> value.</p></div>
114
+
<divclass="item" data-k="fn"><spanclass="kind">fn</span><preclass="sig"><spanclass="k">fn</span><spanclass="ty">App</span>::handle( self : <spanclass="ty">App</span>, request : @moonasgi.<spanclass="ty">Request</span>) <spanclass="op">-></span> @moonasgi.<spanclass="ty">Response</span></pre><pclass="doc">Route a request to its handler, returning 404 when no path matches and 405 when a path matches but no method does.</p></div>
115
+
<divclass="item" data-k="fn"><spanclass="kind">fn</span><preclass="sig"><spanclass="k">fn</span><spanclass="ty">App</span>::to_asgi(self : <spanclass="ty">App</span>) <spanclass="op">-></span> @moonasgi.<spanclass="ty">AsgiApp</span></pre><pclass="doc">Compile the app to a moonasgi <code>AsgiApp</code> a server can run: drain the request body, route it, and stream the response back over the SEAM.</p></div>
116
+
</section>
117
+
<sectionclass="pkg" id="openapi"><h2><spanclass="at">§</span>OpenAPI & Swagger</h2><pclass="pdesc">Multi-version OpenAPI / Swagger generation (2.0 / 3.0 / 3.1) from the same routes, and a ready-to-serve Swagger UI page.</p>
118
+
<divclass="item" data-k="enum"><spanclass="kind">enum</span><preclass="sig"><spanclass="k">enum</span><spanclass="ty">OpenApiVersion</span></pre><pclass="doc">Target OpenAPI / Swagger document version. moonapi emits every mainstream version from the same route descriptors — a "good FastAPI" is not pinned to one spec version.</p></div>
119
+
<divclass="item" data-k="fn"><spanclass="kind">fn</span><preclass="sig"><spanclass="k">fn</span><spanclass="ty">App</span>::openapi( self : <spanclass="ty">App</span>, version<spanclass="op">?</span> : <spanclass="ty">OpenApiVersion</span> = <spanclass="ty">OpenApi31</span>, title<spanclass="op">?</span> : <spanclass="ty">String</span> = "moonapi", api_version<spanclass="op">?</span> : <spanclass="ty">String</span> = "0.1.0") <spanclass="op">-></span><spanclass="ty">Json</span></pre><pclass="doc">Build the OpenAPI / Swagger document for the app as a <code>Json</code> value, walking the registered routes once into paths → methods → operations.</p></div>
0 commit comments