Skip to content

Examples

The examples/ directory contains small, runnable programs that demonstrate individual features of Baldr. Each example is a self-contained CMake target built when Baldr is the top-level project.

Browse by topic

  • Hello World

    The smallest possible Baldr program — one route, automatic JSON.

    Hello World

  • Hello Service

    Register a service and inject it into a route handler.

    Hello Service

  • Devices

    Return a list of Device records.

    Devices

  • Weather forecast

    Generate a random forecast and return it as JSON.

    Weather forecast

  • Static files

    Serve a directory tree under a URL prefix using MapStaticFiles.

    Static files

  • File stream

    Stream a file download and accept an upload, using FileStreamResult and Results::Json.

    File stream

  • OpenAPI example

    Generate a 3.0.3 spec from route metadata with BaldrOpenApiExtension.

    OpenAPI example

  • Todo

    A small CRUD service: DI-registered repository, controller with grouped routes, FromParams / FromBody binding, and OpenAPI.

    Todo

Building the examples

When Baldr is the top-level project, examples are built by default:

cmake -S . -B build
cmake --build build

Each example produces an executable named after its directory: ./build/HelloWorld, ./build/HelloService, ./build/Devices, ./build/WeatherForecast, ./build/StaticFiles, ./build/FileStream, ./build/OpenApiExample, ./build/Todo.

To disable examples (for example in a production build), set -DBALDR_BUILD_EXAMPLES=OFF at configure time.