Sign in

    daqing/moonway/http does not have a README file

    CacheStore

    pub(open) trait CacheStore {
    async fn cache_get(Self, String) -> String?
    async fn cache_set_ex(Self, String, String, Int) -> Bool
    }

    Async cache storage used by the serving layer. Implemented by @cache.RedisClient; the HTTP layer only knows this interface.

    CachePolicy

    pub(all) struct CachePolicy[S] {
    store : S
    prefix : String
    ttl : Int
    }

    Response-cache policy for GET requests, bound to a concrete store.

    CachePolicy::key

    fn[S] CachePolicy::key(self : CachePolicy[S], path : String) -> String

    CachePolicy::lookup

    async fn[S : CacheStore] CachePolicy::lookup(self : CachePolicy[S], path : String) -> CachedResponse?

    Look up a cached response; transport failures surface as raises so the serving layer can degrade gracefully.

    CachePolicy::store

    async fn[S : CacheStore] CachePolicy::store(self : CachePolicy[S], path : String, cached : CachedResponse) -> Unit

    Store a response. Transport failures raise; the serving layer ignores them (a cache write failure must not fail the request).

    CachedResponse

    pub(all) struct CachedResponse {
    content_type : String
    body : String
    }

    A cached response: the Content-Type and the body.

    Request

    pub(all) struct Request {
    verb : String
    path : String
    query : String
    headers : Array[(String, String)]
    body : String
    }

    Request::header

    fn Request::header(self : Request, name : String) -> String?

    Request::new

    fn Request::new(verb : String, path : String) -> Request

    Request::query_param

    fn Request::query_param(self : Request, name : String) -> String?

    Look up a query-string parameter by name (percent-decoded).

    Request::with_body

    fn Request::with_body(self : Request, body : String) -> Request

    Copy of the request with body replaced (query and headers kept).

    Response

    pub struct Response {
    status_code : Int
    resp_headers : Array[(String, String)]
    resp_body : String
    resp_bytes : Bytes?
    }

    Response::body

    fn Response::body(self : Response) -> String

    Response::bytes_body

    fn Response::bytes_body(self : Response) -> Bytes?

    The binary body when one was set.

    Response::header

    fn Response::header(self : Response, name : String) -> String?

    Response::headers

    fn Response::headers(self : Response) -> Array[(String, String)]

    Response::internal_error

    fn Response::internal_error() -> Response

    Response::new

    fn Response::new() -> Response

    Response::set_body

    fn Response::set_body(self : Response, body : String) -> Unit

    Response::set_body_bytes

    fn Response::set_body_bytes(self : Response, body : Bytes) -> Unit

    Set a binary body (e.g. static files). Takes precedence over the text body.

    Response::set_header

    fn Response::set_header(self : Response, name : String, value : String) -> Unit

    Response::set_status

    fn Response::set_status(self : Response, status : Int) -> Unit

    Response::status

    fn Response::status(self : Response) -> Int

    WsRoute

    pub(all) struct WsRoute {
    path : String
    handler : async (
    Conn
    ) -> Unit
    }

    A WebSocket route: GET requests whose path matches exactly are upgraded and handed to the handler, which owns the connection until it returns.

    content_type_for

    fn content_type_for(path : String) -> String

    Content-Type for a static file path, by extension. Unknown extensions default to application/octet-stream.

    decode_url_encoded

    fn decode_url_encoded(s : String) -> String

    Decode a URL-encoded string: '+' becomes space, %XX becomes the byte.

    query_param

    fn query_param(source : String, name : String) -> String?

    First value of name in a query/form string ("a=1&b=2"), decoded.

    resolve_static

    fn resolve_static(dir : String, url_path : String) -> String? raise

    Resolve a GET URL path to a file inside dir: / maps to /index.html; directories resolve to their index.html. Returns None when nothing matches or the path tries to escape dir.

    serve

    fn serve(port : Int, handler : (Request) -> Response, ws_routes? : Array[WsRoute]) -> Unit

    Serve HTTP/1.1 on 127.0.0.1:port from synchronous code: drives the async runtime's event loop until the process is interrupted.

    Error behavior: request parsing happens inside the runtime before user code runs, and a synchronous handler cannot raise (MoonBit's type system rejects raising functions where (Context) -> Context is expected), so the bridge only ever sees parsed requests and non-raising failures. Should anything still fail, the runtime aborts only the affected connection (allow_failure=true) and the server keeps serving. Response::internal_error() is available for explicit 500 responses.

    serve_async

    async fn serve_async(port : Int, handler : (Request) -> Response, ws_routes? : Array[WsRoute]) -> Unit

    Core serving loop. Must run inside the async runtime (an async fn main, or a task spawned within one). Binds 127.0.0.1:port; raises on bind failure. GET requests matching a WebSocket route are upgraded instead of dispatched.

    serve_cached

    fn[S : CacheStore] serve_cached(policy : CachePolicy[S], port : Int, handler : (Request) -> Response, ws_routes? : Array[WsRoute]) -> Unit

    Like serve, with response caching bound to a concrete cache store.

    serve_cached_async

    async fn[S : CacheStore] serve_cached_async(policy : CachePolicy[S], port : Int, handler : (Request) -> Response, ws_routes? : Array[WsRoute]) -> Unit

    Serving loop with response caching for GET requests: hits are answered before dispatch (X-Cache: HIT) and 200 responses are stored with the policy's TTL (X-Cache: MISS). Cache failures degrade to plain dispatch.

    serve_static_file

    fn serve_static_file(dir : String, url_path : String) -> Response?

    Serve a GET request from dir. Returns None when no file matches so the caller can fall back (404 or dynamic dispatch). Read errors degrade to None. Text content types are served through the string body; binary types through the bytes body.

    serve_websocket

    Upgrade and dispatch a request to the matching WebSocket route. Returns false when no route matches (the caller falls back to normal HTTP dispatch).

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io