Skip to content

Hello World example

examples/HelloWorld is the smallest possible Baldr program — one route that returns a JSON payload.

Source

examples/HelloWorld/src/main.cpp:

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

#include <variant>

struct Payload
{
    std::string message;
};

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

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

    app->MapGet("/json",
                [&]() { return Payload { .message = "Hello, World!" }; });

    app->Run();

    return 0;
}

What it shows

  • Application composition with skr::ApplicationBuilder and baldr::BaldrExtension.
  • Building the host with builder.Build<baldr::WebApplication>().
  • Route registration with MapGet(path, handler).
  • Automatic JSON serialization of a returned aggregate — Payload is serialised to {"message":"Hello, World!"} with Content-Type: application/json.

Try it

cmake -S . -B build
cmake --build build
./build/HelloWorld

In another terminal:

curl http://localhost:8080/json

Next steps