| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- #include "json_http.hpp"
- #include "server.hpp"
- #include "svapi/version.hpp"
- #include <fstream>
- #include <sstream>
- namespace svapi {
- void registerMetaRoutes(ApiServer& s) {
- auto& svr = s.raw();
- DbGateway* db = &s.deps().db;
- std::string share = s.deps().shareDir;
- svr.Get("/healthz", [](const httplib::Request&, httplib::Response& res) {
- sendJson(res, 200, {{"status", "ok"}});
- });
- svr.Get("/readyz", [db](const httplib::Request&, httplib::Response& res) {
- if (db->healthy()) sendJson(res, 200, {{"status", "ok"}});
- else sendJson(res, 503, {{"status", "unavailable"}});
- });
- // Registered at BOTH paths, and they differ in one way worth knowing:
- // pre-routing authenticates everything under /api/, so /api/v1/version
- // needs a key (any key, scoped included) while /version at the root needs
- // none. Both are offered because a client looks under /api/v1 - a consumer
- // tried /api/v1/version, /api/v1/health and /api/v1/info and got 404 from
- // all three - while a monitor outside the API wants no credential.
- //
- // It exists because a consumer that cannot ask which server it is talking
- // to has to PROBE for a behaviour change instead: one of them discovered a
- // 0.3.0 -> 0.5.1 jump only by inserting a duplicate row to see which error
- // contract came back. A version string is not a secret, and the error
- // shapes and /openapi.json already say more than this does.
- auto version = [](const httplib::Request&, httplib::Response& res) {
- sendJson(res, 200, {{"name", "smartbotic-vectorapi"},
- {"version", svapi::VERSION},
- {"commit", svapi::GIT_COMMIT},
- {"db_client", smartbotic::database::clientLibraryVersion()}});
- };
- svr.Get("/version", version);
- svr.Get("/api/v1/version", version);
- svr.Get("/openapi.json", [share](const httplib::Request&, httplib::Response& res) {
- if (share.empty()) { sendJson(res, 404, {{"error", "not configured"}}); return; }
- std::ifstream f(share + "/openapi.json");
- if (!f) { sendJson(res, 404, {{"error", "not found"}}); return; }
- std::ostringstream ss; ss << f.rdbuf();
- res.set_content(ss.str(), "application/json");
- res.status = 200;
- });
- svr.Get("/llms.txt", [share](const httplib::Request&, httplib::Response& res) {
- if (share.empty()) { sendJson(res, 404, {{"error", "not configured"}}); return; }
- std::ifstream f(share + "/llms.txt");
- if (!f) { sendJson(res, 404, {{"error", "not found"}}); return; }
- std::ostringstream ss; ss << f.rdbuf();
- res.set_content(ss.str(), "text/plain");
- res.status = 200;
- });
- // Serve /docs (no trailing slash) -> docs/index.html so the page loads without a redirect.
- svr.Get("/docs", [share](const httplib::Request&, httplib::Response& res) {
- if (share.empty()) { sendJson(res, 404, {{"error", "not configured"}}); return; }
- std::ifstream f(share + "/docs/index.html");
- if (!f) { sendJson(res, 404, {{"error", "not found"}}); return; }
- std::ostringstream ss; ss << f.rdbuf();
- res.set_content(ss.str(), "text/html");
- res.status = 200;
- });
- }
- } // namespace svapi
|