This example demonstrates a Service that owns a custom config category, provides a host capability, and has lifecycle hooks.
wit/greeter.wit: WIT interface implemented by the host capabilityhost/src/main.rs: Service, ConfigHandler, and HostCapability implementationgreeter/: Wasm component that imports the host interfaceconfig.toml: Component, capability, and service configuration
Change into the examples/service directory.
- Build the greeter component and host:
./build.sh- Run the example:
./run.shOutput:
[GreetingService] started
Result: "HOWDY, World!"
[GreetingService] shutdown
GreetingService implements Service with three responsibilities:
- Config handling: A
ConfigHandlerclaims the[greeting]category, parsing themessageproperty. Config is shared with the service viaArc<Mutex<...>>. - Capability provisioning: After config is parsed,
capabilities()uses.take()to pull the message out of the mutex and creates aGreetingCapabilityfactory. The factory also readsuppercasefrom the capability's properties. - Lifecycle:
start()andshutdown()are called around invocation.
- Configuration:
[greeting.default]setsmessage = "Howdy".[capability.greeting]setsuppercase = true. - Config phase:
GreetingConfigHandlerparses the greeting category and stores the message. - Capability phase:
GreetingService.capabilities()takes the stored message, creates a factory that applies theuppercasetransform. - Invocation: Guest calls
get_greeting(), host returns "HOWDY", guest formats "HOWDY, World!".