Skip to content

Static files example

examples/StaticFiles serves a wwwroot/ directory under /static using MapStaticFiles, plus a hand-written landing page at /.

Source

examples/StaticFiles/src/main.cpp:

examples/StaticFiles/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
60
61
62
63
64
#include <Baldr/Baldr.hpp>

#include <cstdlib>
#include <filesystem>
#include <string>

std::filesystem::path exeDir()
{
#if defined(_WIN32)
    return std::filesystem::path(_pgmptr).parent_path();
#else
    if (auto* argv0 = std::getenv("_"))
        return std::filesystem::path(argv0).parent_path();
    return std::filesystem::current_path();
#endif
}

std::filesystem::path resolveWebRoot()
{
    const auto nextToExe =
        std::filesystem::weakly_canonical(exeDir()) / "wwwroot";
    if (std::filesystem::is_directory(nextToExe))
        return nextToExe;

    return std::filesystem::current_path() / "wwwroot";
}

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

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

    const std::filesystem::path webRoot = resolveWebRoot();

    app->MapGet("/", [](baldr::HttpRequest&, baldr::HttpResponse&) {
        return baldr::ContentResult(
            "<!doctype html><meta charset=\"utf-8\">"
            "<title>Static files</title>"
            "<h1>Baldr static-files example</h1>"
            "<ul>"
            "<li><a href=\"/static/index.html\">/static/index.html</a></li>"
            "<li><a href=\"/static/css/site.css\">/static/css/site.css</a></li>"
            "<li><a "
            "href=\"/static/assets/app.js\">/static/assets/app.js</a></li>"
            "<li><a "
            "href=\"/static/assets/img/logo.svg\">/static/assets/img/logo.svg</"
            "a></"
            "li>"
            "<li><a "
            "href=\"/static/assets/hello.txt\">/static/assets/hello.txt</a></"
            "li>"
            "</ul>",
            "text/html",
            baldr::StatusCode::OK);
    });

    app->MapStaticFiles("/static", webRoot.string());

    app->Run();

    return 0;
}

What it shows

  • Locating the web root next to the executable at runtime (Windows uses _pgmptr, POSIX reads the _ environment variable), falling back to the working directory.
  • Mounting MapStaticFiles("/static", webRoot.string()).
  • Serving a hand-written baldr::ContentResult at / so users can browse the asset list.

Try it

cmake -S . -B build
cmake --build build
./build/StaticFiles

In another terminal:

curl http://localhost:8080/
curl http://localhost:8080/static/index.html

Next steps