Explorer

definy.replace-node-host

description
Replaces the JavaScript of the definy host at url with definy.node-host-script over HTTP (PUT /__definy/host). The host runs the new script with --self-test first, keeps the old one as host.prev.mjs and exits, and the service manager (systemd Restart=always) starts the new one. api_token is the deploy-token of the host (docs/raspberry-pi.md)
type
Fn(url: String, api_token: Secret) -> @wasi.io
expression
fn(url$0, api_token$1) =>
  #handle<@wasi.io>({
    request: @http.request-of-url(
      method: "PUT",
      url: @core.string-concat(left: $0, right: "/__definy/host"),
      headers: [{name: "Content-Type", value: "text/javascript; charset=utf-8"}],
      body: @core.string-to-bytes(value: @definy.node-host-script)
    ),
    secret_headers: [{name: "Authorization", prefix: "Bearer ", secret: $1}],
    then: fn(result$2) =>
      match $2 {
        #error(error$3) =>
          #done<@wasi.io>(@core.string-concat(left: "送れませんでした: ", right: @http.error-text(error: $3))),
        #ok(response$4) =>
          match @core.less-than(left: $4.status, right: 300) {
            true =>
              #done<@wasi.io>(@core.string-concat(
                left: "ホストを置き換えました (再起動します): ",
                right: @http.body-text(response: $4)
              )),
            false =>
              #done<@wasi.io>(@core.string-concat(left: "置き換えられませんでした: ", right: @http.body-text(response: $4)))
          }
      }
  })
visibility
public
definy.replace-node-host (project definy, branch main, snapshot l0VFjJZP174d8CbZWFoujAGTYjgsQu4uTq5uWCZaXgo)
id: vRoSnVWxsznP4A3RZGF8CuSETMf1QYZT-S0mJUNS2gg
content hash: RC4f2KRDNx2J4IiCf1iYdSMwre4gUq05b_7-lyeRel8