HTTP Server Provider
This capability provider example imports the wasi:http/incoming-handler interface and demonstrates how to forward HTTP requests to components that export the same interface, enabling components to accept incoming HTTP(s) requests.
The provider starts an HTTP server listening on port 8080 with two routes:
- /proxy: Forwards the request to the component- http-component
- /: Serves the request directly from the provider
👟 Run the provider
Clone the wasmCloud/go repository:
git clone https://github.com/wasmCloud/go.gitChange directory to examples/provider/http-server:
cd examples/provider/http-serverIn addition to the standard elements of a Go project, the example directory includes the following files and directories:
- /bindings: An auto-generated directory for Go bindings of interfaces. Users typically will not need to interact directly with the contents of this directory.
- /wit: Directory for WebAssembly Interface Type (WIT) packages that define interfaces.
- wadm.yaml: Declarative application manifest.
- wasmcloud.toml: Configuration file for a wasmCloud application.
Build the provider
Build the provider:
wash buildThe build process will generate a .par.gz archive file in a new /build directory.
Start a wasmCloud environment
Start a local wasmCloud environment (using the -d/--detached flag to run in the background):
wash up -dDeploy the application
The application manifest (wadm.yaml) for this example defines an application consisting of the locally built provider at ./build/http-server.par.gz and an OCI image for an HTTP "Hello World" component.
Deploy the manifest:
wash app deploy wadm.yamlTo ensure that the application has reached Deployed status, you can use wash app list:
wash app listSend a request
Send a request to the proxy route:
curl localhost:8080/proxySend a request to the index route:
curl localhost:8080Clean up
You can delete an application from your wasmCloud environment by referring either to its application name (http) or the original application manifest:
wash app delete wadm.yamlStop your local wasmCloud environment:
wash down🩻 Internals
Proxying uses a custom http.RoundTripper implementation that forwards requests to the component. This example forwards to a single target (http-http_component).
transport := wrpchttp.NewIncomingRoundTripper(wasmcloudprovider, wrpchttp.WithSingleTarget("http-http_component"))
wasiIncomingClient := &http.Client{
  Transport: transport,
}
wasiIncomingClient.Get("http://localhost:8080/proxy")You can also provide a custom Director function to select the target based on the request.
func director(r *http.Request) string {
  if r.URL.Host == "api" {
    return "http-api"
  }
  return "http-ui"
}
transport := wrpchttp.NewIncomingRoundTripper(wasmcloudprovider, wrpchttp.WithDirector(director))
wasiIncomingClient := &http.Client{
  Transport: transport,
}
// forward to http-api component
wasiIncomingClient.Get("http://api/users")
// forward to http-ui component
wasiIncomingClient.Get("http://ui/index.html")
wasiIncomingClient.Get("http://anyothername/index.html")📖 Further reading
For an introduction to capability providers, see the Providers Overview in the wasmCloud documentation.
For more on building providers, see the Provider Developer Guide in the wasmCloud documentation.