Skip to content

Setting up a Dispatch Endpoint

Chris O'Hara edited this page Jul 1, 2024 · 10 revisions

Dispatch does not currently host functions. Dispatch is a cloud service that schedules the execution of functions across user-provided endpoints.

To get started with Dispatch, users must create an endpoint, register their functions with that endpoint, and then serve the endpoint such that the Dispatch cloud service can interact with it over the public internet.

Serving a bare endpoint

First, create a Go module, and then add the Dispatch SDK as a dependency:

go mod init bare-endpoint
go get github.com/dispatchrun/dispatch-go@latest

Using the CLI

The simplest way to serve an endpoint is using the Dispatch CLI. The Dispatch SDK is automatically configured when run under the CLI.

After installing the CLI (refer to the installation instructions), login to Dispatch using:

dispatch login

Here's the code required to create and serve an endpoint:

main.go

package main

import "github.com/dispatchrun/dispatch-go"

func main() {
    endpoint, err := dispatch.New()
    if err != nil {
        panic(err)
    }
    if err := endpoint.ListenAndServe(); err != nil {
        panic(err)
    }
}

To serve the endpoint, run:

dispatch run -- go run main.go

At this stage, nothing will happen because no functions have been registered with the endpoint.

Standalone service

To run the Dispatch endpoint as a standalone service, the following environment variables must be set:

  • DISPATCH_ENDPOINT_URL: the URL that the endpoint will be publically accessible from
  • DISPATCH_VERIFICATION_KEY: the public key used to verify that requests originate from Dispatch

Alternatively, the configuration can be passed to the dispatch.New(..) constructor (see dispatch.EndpointUrl and dispatch.VerificationKey):

endpoint, err := dispatch.New(dispatch.EndpointUrl("http://example.com"))

By default, the Dispatch SDK ships with a built-in server to serve the endpoint. To configure the address that the server serves the endpoint on, either set the DISPATCH_ENDPOINT_ADDR environment variable, or use the dispatch.ServeAddress(...) option. If neither are specified, the default address of 127.0.0.1:8000 is used. Note that the address that the endpoint is served on may not be the same as the URL that the endpoint is publically accessible from, hence the separate configuration paths.

To serve the endpoint using the built-in server:

if err := endpoint.ListenAndServe(); err != nil {
    // handle error
}

The endpoint can also be served using a standard HTTP server. (*Dispatch).Handler() returns a path string and an http.Handler value that can be used with the HTTP server from the standard library:

mux := http.NewServeMux()
mux.Handle(endpoint.Handler())

if err := http.ListenAndServe(":8000", mux); err != nil {
    // handle error
}

Next steps

See how to register a function with the endpoint.

Clone this wiki locally