Skip to content

Usage overview

A Baldr application is built around three concepts:

  • An application builder (provided by Skirnir) that wires up services and extensions.
  • The Baldr extension, which registers the router, middleware provider, and HTTP server with the builder.
  • The web application, which exposes a strongly-typed API for registering routes and running the server.

The minimal program

The smallest possible Baldr application looks like this:

src/main.cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <Baldr/Baldr.hpp>

struct Payload
{
    std::string message;
};

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

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

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

    app->Run();

    return 0;
}

See the Hello World example for the complete project layout.

Application lifecycle

A Baldr program follows this lifecycle:

  1. Configure services — register custom services on the builder's service collection.
  2. Add extensions — call .WithExtension<BaldrExtension>() to wire up the router and HTTP server.
  3. Build the application.Build<WebApplication>() resolves all services and constructs the app.
  4. Register routes — call MapGet, MapPost, or other mapping helpers.
  5. Runapp->Run() starts the HTTP listener and blocks until shutdown.

Where to go next

  • Routing

    Map routes, read parameters, and return responses.

    Routing

  • Dependency injection

    Register and resolve services from the container.

    Dependency injection

  • Middleware

    Intercept requests with composable middleware.

    Middleware