Weather forecast example¶
examples/WeatherForecast generates a random weather forecast, returns it as JSON, and exposes the same surface as an OpenAPI 3.0.3 document and Scalar UI.
Source¶
examples/WeatherForecast/src/main.cpp:
| examples/WeatherForecast/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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
What it shows¶
- Returning a
std::vector<Aggregate>where each item is initialised with designated initializers. - Using
std::string_viewfields that serialise cleanly to JSON without copying. - Composing simple computation in the handler without any state.
- The fluent route API:
MapGet("/").Handle(lambda)instead of the legacyMapGet(path, lambda)overload. - Wiring the OpenAPI extension with no custom
OpenApiOptions(defaults are good enough for a single endpoint) and mountingbaldr::MapOpenApiandbaldr::MapScalarUion the sameWebApplication.
Try it¶
cmake -S . -B build
cmake --build build
./build/WeatherForecast
In another terminal:
curl http://localhost:8080/
curl http://localhost:8080/openapi.json | jq '.paths, .components.schemas'
# Scalar UI is served at /scalar (open in a browser)
Next steps¶
- See OpenAPI extension for
OpenApiOptionsand the schema dialect. - See Route options for the metadata setters used by the extension.
- Browse all examples.