Explorer

definy.handle-request

description
(en) How the server handles an HTTP request (docs/service.md): OPTIONS, /healthz, static assets, /virtual/wasm/ and the RPC (definy.handle-rpc). The server compiles this at build time (wasm/service.wasm)
type
Fn(request: @http.request) -> @definy.service-io
expression
fn(request$0) =>
  match @core.equal(left: @http.method-text(method: $0.method), right: "OPTIONS") {
    true =>
      #done<@definy.service-io>(#respond({status: 204, headers: @definy.cors-headers, body: #empty<@definy.http-body>})),
    false =>
      match @core.equal(left: @http.path(request: $0), right: "/healthz") {
        true =>
          #read_host_settings<@definy.service-io>({
            then: fn(settings$6) =>
              @definy.json-response(
                status: 200,
                body: #object<@definy.json>([
                  {key: "status", value: #string<@definy.json>("ok")},
                  {key: "runtime", value: #string<@definy.json>("cloudflare-workers")},
                  {key: "storage", value: #string<@definy.json>($6.storage)},
                  {key: "ipVersion", value: #string<@definy.json>(@definy.client-ip-version(headers: $0.headers))}
                ])
              )
          }),
        false =>
          match @definy.is-worker-first-path(path: @http.path(request: $0)) {
            false =>
              #read_host_settings<@definy.service-io>({
                then: fn(settings$7) =>
                  match $7.has_assets {
                    true =>
                      #done<@definy.service-io>(#serve_asset),
                    false =>
                      #done<@definy.service-io>(#respond({status: 404, headers: [], body: #text<@definy.http-body>("Not Found")}))
                  }
              }),
            true =>
              @definy.ensure-seeded(
                then: fn(seeded$1) =>
                  match @core.equal(left: @http.path(request: $0), right: "/mcp") {
                    true =>
                      @definy.handle-mcp(request: $0),
                    false =>
                      match @std.string-starts-with(value: @http.path(request: $0), prefix: "/virtual/wasm/") {
                        true =>
                          @definy.serve-virtual-wasm(path: @http.path(request: $0)),
                        false =>
                          match match @std.string-starts-with(value: @http.path(request: $0), prefix: "/definy.v1.") {
                              true =>
                                @definy.handle-rpc(
                                  method: @core.string-slice(
                                    value: @http.path(request: $0),
                                    start: 11,
                                    end: @core.string-length(value: @http.path(request: $0))
                                  ),
                                  request: $0
                                ),
                              false =>
                                #none
                            } {
                            #some(handler$2) =>
                              @definy.rpc-call(
                                request: $0,
                                handler: fn(request$8) =>
                                  $2(request: $8)
                              ),
                            #none(_) =>
                              @definy.rpc-error(
                                code: "unimplemented",
                                message: @core.string-concat(
                                  left: "Endpoint ",
                                  right: @core.string-concat(left: @http.path(request: $0), right: " is not implemented")
                                )
                              )
                          }
                      }
                  }
              )
          }
      }
  }
visibility
public
definy.handle-request (project definy, branch release-time-compare, snapshot JJrRgQW_LZN6tN8CbYr1dN7HosqNTuIWWYx7ACbt1n0)
id: 7iO128nHUQqwiQMoeo0gZqAhxvbve8Apa-L4s4SyF1I
content hash: bjj6iaOtz4Wi6wW1HdHSa6My1Mf3e4tYXiCi4cXPEso