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
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
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
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
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
Called by main
Calls make_models_listing, route_chat_completions, route_completions, route_model_get
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
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
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
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
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());
}
}