Skip to content

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
#include <Baldr/Baldr.hpp>

#include <random>

struct WeatherForecast
{
    std::string      date;
    std::string_view summary;

    int temperatureC;
    int temperatureF;
};

int random(const int min, const int max)
{
    std::random_device              rd;
    std::mt19937                    gen(rd());
    std::uniform_int_distribution<> dis(min, max);
    return dis(gen);
}

int main()
{
    auto builder = skr::ApplicationBuilder()
                       .WithExtension<baldr::BaldrExtension>()
                       .WithExtension<baldr::BaldrOpenApiExtension>();

    auto app = builder.Build<baldr::WebApplication>();

    app->MapGet("/").Handle([] {
        auto forecast = std::vector<WeatherForecast>(5);

        static auto summaries =
            std::vector { "Freezing",   "Bracing",  "Chilly", "Cool",
                          "Mild",       "Warm",     "Balmy",  "Hot",
                          "Sweltering", "Scorching" };

        for (auto& item : forecast)
        {
            const auto celsius = random(-22, 55);

            item = WeatherForecast {
                .date         = "01/01/2025",
                .summary      = summaries[random(0, summaries.size() - 1)],
                .temperatureC = celsius,
                .temperatureF = 32 + static_cast<int>(celsius / 0.5556)
            };
        }

        return forecast;
    });

    baldr::MapOpenApi(*app);
    baldr::MapScalarUi(*app);

    app->Run();

    return 0;
}

What it shows

  • Returning a std::vector<Aggregate> where each item is initialised with designated initializers.
  • Using std::string_view fields 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 legacy MapGet(path, lambda) overload.
  • Wiring the OpenAPI extension with no custom OpenApiOptions (defaults are good enough for a single endpoint) and mounting baldr::MapOpenApi and baldr::MapScalarUi on the same WebApplication.

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