This example demonstrates a composable HTTP server with two types of routes:
- a component route that invokes a function directly
- a channel route that publishes a message for a subscribed component
Because the HTTP Server applies inversion of control, neither component has any built-in HTTP request handling. In fact, both routes target the simple hello-world greeter component.
config.toml: server, component, and route configurationrun-server.sh: starts the Composable Runtime with an HTTP Servergreet.sh: sends requests to both routes viacurl
Change into the examples/http-server directory.
- Start the server:
./run-server.shThat will build the hello-world example's greeter component if not already built.
Then it starts a long-running Composable Runtime instance with the HTTP Server support enabled, and configures info-level logging.
- In another terminal, send requests:
./greet.shOutput:
--- GET /hello/world (component route) ---
"Hello, World!"
--- POST /bonjour (channel route -> subscription -> bonjour component) ---
In the server log, you will see the channel route result logged asynchronously:
... invocation complete component=bonjour function=greet result="Bonjour, le Monde!"
- Component route:
GET /hello/{name}maps the{name}path segment to thenameargument ofhello.greet(name)and returns the result directly. - Channel route:
POST /bonjourpublishes the request body to the "names" channel. Thebonjourcomponent subscribes to that channel and is invoked asynchronously. - Configuration: The
bonjourcomponent definition includesconfig.greeting = "Bonjour"to differentiate its greeting in the log output.
[server.api]
type = "http"
port = 8888
[server.api.route.hello]
path = "/hello/{name}"
component = "hello"
function = "greet"
[server.api.route.bonjour]
path = "/bonjour"
channel = "names"
[component.hello]
uri = "../hello-world/greeter/target/wasm32-unknown-unknown/release/greeter.wasm"
[component.bonjour]
uri = "../hello-world/greeter/target/wasm32-unknown-unknown/release/greeter.wasm"
config.greeting = "Bonjour"
subscription = "names"