meta.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "json_http.hpp"
  2. #include "server.hpp"
  3. #include "svapi/version.hpp"
  4. #include <fstream>
  5. #include <sstream>
  6. namespace svapi {
  7. void registerMetaRoutes(ApiServer& s) {
  8. auto& svr = s.raw();
  9. DbGateway* db = &s.deps().db;
  10. std::string share = s.deps().shareDir;
  11. svr.Get("/healthz", [](const httplib::Request&, httplib::Response& res) {
  12. sendJson(res, 200, {{"status", "ok"}});
  13. });
  14. svr.Get("/readyz", [db](const httplib::Request&, httplib::Response& res) {
  15. if (db->healthy()) sendJson(res, 200, {{"status", "ok"}});
  16. else sendJson(res, 503, {{"status", "unavailable"}});
  17. });
  18. // Registered at BOTH paths, and they differ in one way worth knowing:
  19. // pre-routing authenticates everything under /api/, so /api/v1/version
  20. // needs a key (any key, scoped included) while /version at the root needs
  21. // none. Both are offered because a client looks under /api/v1 - a consumer
  22. // tried /api/v1/version, /api/v1/health and /api/v1/info and got 404 from
  23. // all three - while a monitor outside the API wants no credential.
  24. //
  25. // It exists because a consumer that cannot ask which server it is talking
  26. // to has to PROBE for a behaviour change instead: one of them discovered a
  27. // 0.3.0 -> 0.5.1 jump only by inserting a duplicate row to see which error
  28. // contract came back. A version string is not a secret, and the error
  29. // shapes and /openapi.json already say more than this does.
  30. auto version = [](const httplib::Request&, httplib::Response& res) {
  31. sendJson(res, 200, {{"name", "smartbotic-vectorapi"},
  32. {"version", svapi::VERSION},
  33. {"commit", svapi::GIT_COMMIT},
  34. {"db_client", smartbotic::database::clientLibraryVersion()}});
  35. };
  36. svr.Get("/version", version);
  37. svr.Get("/api/v1/version", version);
  38. svr.Get("/openapi.json", [share](const httplib::Request&, httplib::Response& res) {
  39. if (share.empty()) { sendJson(res, 404, {{"error", "not configured"}}); return; }
  40. std::ifstream f(share + "/openapi.json");
  41. if (!f) { sendJson(res, 404, {{"error", "not found"}}); return; }
  42. std::ostringstream ss; ss << f.rdbuf();
  43. res.set_content(ss.str(), "application/json");
  44. res.status = 200;
  45. });
  46. svr.Get("/llms.txt", [share](const httplib::Request&, httplib::Response& res) {
  47. if (share.empty()) { sendJson(res, 404, {{"error", "not configured"}}); return; }
  48. std::ifstream f(share + "/llms.txt");
  49. if (!f) { sendJson(res, 404, {{"error", "not found"}}); return; }
  50. std::ostringstream ss; ss << f.rdbuf();
  51. res.set_content(ss.str(), "text/plain");
  52. res.status = 200;
  53. });
  54. // Serve /docs (no trailing slash) -> docs/index.html so the page loads without a redirect.
  55. svr.Get("/docs", [share](const httplib::Request&, httplib::Response& res) {
  56. if (share.empty()) { sendJson(res, 404, {{"error", "not configured"}}); return; }
  57. std::ifstream f(share + "/docs/index.html");
  58. if (!f) { sendJson(res, 404, {{"error", "not found"}}); return; }
  59. std::ostringstream ss; ss << f.rdbuf();
  60. res.set_content(ss.str(), "text/html");
  61. res.status = 200;
  62. });
  63. }
  64. } // namespace svapi