main.cpp

examples/openai-server/src/main.cpp

@file main.cpp @brief OpenAI-compat HTTP server example bridging librentropic.

Owners: @tvanfossen

main

int main(int argc, char **argv)

Program entry — bind to PORT (default 8080) and serve forever.

examples/openai-server/src/main.cpp:747

Source · 11 lines
int main(int argc, char** argv) {
    const int port = (argc > 1) ? std::atoi(argv[1]) : 8080;
    auto handle = open_engine();
    if (handle == nullptr) { return 1; }
    httplib::Server server;
    register_routes(server, handle);
    std::cerr << "[openai-server] listening on http://0.0.0.0:" << port << '\n';
    server.listen("0.0.0.0", port);
    entropic_destroy(handle);
    return 0;
}
Activity · 10 steps
lines 748–748const int port = (argc > 1) ? std::atoi(argv[1]) : 8080;lines 749–749auto handle = open_engine();lines 750–750◇ if (handle == nullptr)lines 750–750return 1;lines 751–751httplib::Server server;lines 752–752register_routes(server, handle);lines 753–753std::cerr << "[openai-server] listening on http://0.0.0.0:" << port << '\n';lines 754–754server.listen("0.0.0.0", port);lines 755–755entropic_destroy(handle);lines 756–756return 0;

Flows

make_models_listing

json anonymous_namespace{main.cpp}::make_models_listing()

Build the /v1/models response from :func: known_models .

examples/openai-server/src/main.cpp:616

Show on the map →

Called by register_routes

Calls known_models, make_model_entry

Source · 7 lines
json make_models_listing() {
    json data = json::array();
    for (const auto& id : known_models()) {
        data.push_back(make_model_entry(id));
    }
    return json{{"object", "list"}, {"data", data}};
}
Activity · 4 steps
lines 617–617json data = json::array();lines 618–618◇ for (const auto& id : known_models())lines 619–619data.push_back(make_model_entry(id));lines 621–621return json{{"object", "list"}, {"data", data}};

Flows

open_engine

entropic_handle_t anonymous_namespace{main.cpp}::open_engine()

Initialize the engine handle from the project working directory.

examples/openai-server/src/main.cpp:725

Source · 13 lines
entropic_handle_t open_engine() {
    entropic_handle_t handle = nullptr;
    if (entropic_create(&handle) != ENTROPIC_OK || handle == nullptr) {
        std::cerr << "[openai-server] entropic_create failed\n";
        return nullptr;
    }
    if (entropic_configure_dir(handle, "") != ENTROPIC_OK) {
        std::cerr << "[openai-server] entropic_configure_dir failed\n";
        entropic_destroy(handle);
        return nullptr;
    }
    return handle;
}
Activity · 9 steps
lines 726–726entropic_handle_t handle = nullptr;lines 727–727◇ if (entropic_create(&handle) != ENTROPIC_OK || handle == nullptr)lines 728–728std::cerr << "[openai-server] entropic_create failed\n";lines 729–729return nullptr;lines 731–731◇ if (entropic_configure_dir(handle, "") != ENTROPIC_OK)lines 732–732std::cerr << "[openai-server] entropic_configure_dir failed\n";lines 733–733entropic_destroy(handle);lines 734–734return nullptr;lines 736–736return handle;

Flows

register_routes

void anonymous_namespace{main.cpp}::register_routes(httplib::Server &server, entropic_handle_t handle)

Wire all routes onto a configured server instance.

examples/openai-server/src/main.cpp:704

Source · 15 lines
void register_routes(httplib::Server& server, entropic_handle_t handle) {
    server.Get("/health", [](const auto&, auto& res) {
        res.set_content(json{{"status", "ok"}}.dump(), "application/json");
    });
    server.Get("/v1/models", [](const auto&, auto& res) {
        res.set_content(make_models_listing().dump(), "application/json");
    });
    server.Get("/v1/models/:model", route_model_get);
    server.Post("/v1/chat/completions", [handle](const auto& req, auto& res) {
        route_chat_completions(handle, req, res);
    });
    server.Post("/v1/completions", [handle](const auto& req, auto& res) {
        route_completions(handle, req, res);
    });
}
Activity · 5 steps
lines 705–707server.Get("/health", [](const auto&, auto& res) { res.set_content(json{{"status", "ok"}}.dump(), "application/json"); });lines 708–710server.Get("/v1/models", [](const auto&, auto& res) { res.set_content(make_models_listing().dump(), "application/json"); });lines 711–711server.Get("/v1/models/:model", route_model_get);lines 712–714server.Post("/v1/chat/completions", [handle](const auto& req, auto& res) { route_chat_completions(handle, req, res); });lines 715–717server.Post("/v1/completions", [handle](const auto& req, auto& res) { route_completions(handle, req, res); });

Flows

route_chat_completions

void anonymous_namespace{main.cpp}::route_chat_completions(entropic_handle_t handle, const httplib::Request &req, httplib::Response &res)

Route entry: POST /v1/chat/completions.

examples/openai-server/src/main.cpp:572

Show on the map →

Called by register_routes

Calls handle_chat_blocking, handle_chat_streaming, parse_chat_body, send_error

Source · 26 lines
void route_chat_completions(entropic_handle_t handle,
                            const httplib::Request& req,
                            httplib::Response& res) {
    json body;
    if (!parse_chat_body(req, res, body)) { return; }
    const auto stream = body.value("stream", false);
    try {
        if (stream) { handle_chat_streaming(handle, body, res); }
        else { handle_chat_blocking(handle, body, res); }
    } catch (const HttpStatusError& e) {
        // gh#38: typed status passthrough — vision-tier missing → 400,
        // other engine failures → 500. send_error() picks 500 by
        // default, so handle non-500 statuses inline here.
        if (e.status == 400) {
            json b = {{"error", json{
                {"message", e.what()},
                {"type", "invalid_request_error"}}}};
            res.status = 400;
            res.set_content(b.dump(), "application/json");
        } else {
            send_error(res, e.what());
        }
    } catch (const std::exception& e) {
        send_error(res, e.what());
    }
}
Activity · 5 steps
lines 575–575json body;lines 576–576◇ if (!parse_chat_body(req, res, body))lines 576–576return;lines 577–577const auto stream = body.value("stream", false);lines 578–596try { if (stream) { handle_chat_streaming(handle, body, res); } else { handle_chat_blocking(handle, body, res); } } catch (const HttpStatusError& e) { // gh#38: typed status passthrough — vision-tier missing → 400, // other engine failures → 500. send_error() picks 500 by // default, so handle non-500 statuses inline here. if (e.status == 400) { json b = {{"error", json{ {"message", e.what()}, {"type", "invalid_request_error"}}}}; res.status = 400; res.set_content(b.dump(), "application/json"); } else { send_error(res, e.what()); } } catch (const std::exception& e) { send_error(res, e.what()); }

Flows

route_completions

void anonymous_namespace{main.cpp}::route_completions(entropic_handle_t handle, const httplib::Request &req, httplib::Response &res)

Route entry: POST /v1/completions (legacy single-turn).

examples/openai-server/src/main.cpp:683

Show on the map →

Called by register_routes

Calls make_completion_response, parse_chat_body, run_blocking, send_error

Source · 15 lines
void route_completions(entropic_handle_t handle,
                       const httplib::Request& req,
                       httplib::Response& res) {
    json body;
    if (!parse_chat_body(req, res, body)) { return; }
    const auto model = body.value("model", "primary");
    const auto prompt = body.value("prompt", std::string());
    try {
        const auto text = run_blocking(handle, prompt);
        res.set_content(make_completion_response(model, text).dump(),
                        "application/json");
    } catch (const std::exception& e) {
        send_error(res, e.what());
    }
}
Activity · 6 steps
lines 686–686json body;lines 687–687◇ if (!parse_chat_body(req, res, body))lines 687–687return;lines 688–688const auto model = body.value("model", "primary");lines 689–689const auto prompt = body.value("prompt", std::string());lines 690–696try { const auto text = run_blocking(handle, prompt); res.set_content(make_completion_response(model, text).dump(), "application/json"); } catch (const std::exception& e) { send_error(res, e.what()); }

Flows