#pragma once #include "evo/persistence/postgres.hpp" #include "evo/server/broker/access_token.hpp" #include "evo/server/ops_http.hpp" #include #include #include #include #include #include #include #include #include // ADR-0182 read-only admin API and portal host. One world; every /api route needs a Keycloak // access token carrying the admin realm role. The portal's static files and its OIDC settings // are public; they carry no secret. // // GET /healthz ok // GET / and the portal's files the portal (index.html, app.js, style.css) // GET /config.json issuer, client id and world for the portal's sign-in // GET /api/me the signed-in admin's subject and email // GET /api/accounts every account with session, lives, deaths and resets // GET /api/accounts/ one account and its life requests // GET /api/regions the world's regions, leases and heads // GET /api/regions//view the region's live view (render_cellular_admin_view) namespace evo::server::admin { struct AdminAsset { std::string path; // "/index.html", ...; "/" serves "/index.html". std::string content_type, body; }; struct AdminConfig { std::uint64_t world{}; std::vector view_regions; // Regions whose live view can be fetched. std::string portal_config_json; // Served at /config.json. std::string content_security_policy; // Sent with every portal file. std::chrono::milliseconds db_timeout{3000}; std::chrono::milliseconds view_cache{750}; // A fetched view answers repeats this long. }; using AdminConnect = std::function()>; // The region's /admin/view body, or nullopt when it cannot be reached or is not ready. using AdminRegionView = std::function(std::uint64_t region)>; class AdminApi { public: AdminApi(AdminConfig config, broker::TokenVerifier verifier, AdminConnect connect, AdminRegionView view, std::vector assets); // `now_seconds` is wall time (token expiry); `now` paces the view cache. [[nodiscard]] OpsResponse handle(const ApiRequest& request, std::uint64_t now_seconds, std::chrono::steady_clock::time_point now); private: [[nodiscard]] OpsResponse api(std::string_view path, const broker::AccessTokenClaims& admin, std::chrono::steady_clock::time_point now); [[nodiscard]] OpsResponse accounts(std::optional account); [[nodiscard]] OpsResponse regions(); [[nodiscard]] OpsResponse view(std::uint64_t region, std::chrono::steady_clock::time_point now); [[nodiscard]] std::optional query(const std::string& sql, std::vector parameters); AdminConfig config_; broker::TokenVerifier verifier_; AdminConnect connect_; AdminRegionView view_; std::vector assets_; std::unique_ptr db_; struct CachedView { std::chrono::steady_clock::time_point at; std::string body; }; std::map views_; }; // Bounded blocking HTTP/1.1 GET of `path` from "host:port" (a name is resolved): the body of // a 200 answer of at most `limit` bytes, or nullopt on any error, other status or timeout. [[nodiscard]] std::optional http_get(std::string_view address, std::string_view path, std::chrono::milliseconds timeout, std::size_t limit); // JSON string literal (quotes included) with control characters escaped. [[nodiscard]] std::string json_string(std::string_view text); } // namespace evo::server::admin