Hello Service example¶
examples/HelloService introduces dependency injection: a custom HelloService is registered with the service collection and resolved automatically when a route handler takes it as a skr::Arc<HelloService> parameter. It also demonstrates returning a std::variant of typed results to handle the empty-path-parameter case.
Source¶
examples/HelloService/src/main.cpp:
| examples/HelloService/src/main.cpp | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
examples/HelloService/src/HelloService.hpp:
| HelloService.hpp | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
examples/HelloService/src/HelloService.cpp:
| HelloService.cpp | |
|---|---|
1 2 3 4 5 6 7 | |
What it shows¶
- Registering a service via
AddTransient<HelloService>(). - Logger injection via
skr::Logger<HelloService>— Skirnir provides loggers automatically; no explicit registration. - Reading path parameters from
HttpRequest::params. - Returning a
std::variantof typed results (Payloadon success,baldr::BadRequestResulton failure) instead of throwing or hand-building error responses. - Returning a custom type that the framework serialises to JSON.
Try it¶
cmake -S . -B build
cmake --build build
./build/HelloService
In another terminal:
curl http://localhost:8080/hello/world
# {"message":"Hello, world!"}
Next steps¶
- See Dependency injection for the full DI reference.
- See Results for the typed result family returned from the
std::variant. - Browse all examples.