#pragma once #include "echoion/web/WatchSession.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Echoion::Web { namespace asio = boost::asio; namespace beast = boost::beast; namespace http = beast::http; namespace websocket = beast::websocket; // Serves the static browser UI over HTTP, hosts the independent // sync-watch WebSocket at GET /ws, and tunnels GET /rpc upgrades to the // local RPC server so only the web port ever needs to be exposed. It has // no handle on the music bot itself. class HttpServer { public: struct Config { uint16_t port = 8080; std::string bindAddress = "127.0.0.1"; std::string webRoot = "./web"; // Explicit RPC URL served to the browser; when empty the UI // derives ws://:. std::string rpcUrl; uint16_t rpcPort = 8765; // Server-wide switch for the Now Playing video preview, // surfaced to the browser via /config.json. bool nowPlayingVideo = true; }; HttpServer(); ~HttpServer(); bool start(const Config& config); void stop(); [[nodiscard]] bool isRunning() const { return running_; } // Token → identity lookup for the watch feature; installed by // Core, which owns the credential store. May be set before or // after start() and survives stop()/start() cycles. void setIdentityResolver( WatchSessionManager::IdentityResolver resolver) { watch_.setIdentityResolver(std::move(resolver)); } // A user's avatar image, served at GET /avatar/. Installed // by Core (which owns the TeamSpeak plugin handle); best-effort — // nullopt yields a 404 and the UI falls back to initials. Same // lifetime rules as the identity resolver. struct AvatarImage { std::string contentType; std::string bytes; }; using AvatarProvider = std::function( const std::string& uid)>; void setAvatarProvider(AvatarProvider provider) { std::lock_guard lock(avatarProviderMutex_); avatarProvider_ = std::move(provider); } // A custom emoji image, served at GET /emoji/. Same shape // and lifetime rules as the avatar provider; installed by Core // (which owns the emoji database). using EmojiProvider = std::function( const std::string& name)>; void setEmojiProvider(EmojiProvider provider) { std::lock_guard lock(avatarProviderMutex_); emojiProvider_ = std::move(provider); } private: void acceptLoop(); void doAccept(); void onAccept(asio::ip::tcp::socket socket); void handleConnection(std::shared_ptr socket); void handleWatch( std::shared_ptr> ws); // Tunnel a browser WebSocket upgrade at /rpc to the local RPC // server as raw TCP: the original upgrade request is replayed // upstream and bytes are pumped in both directions, so the real // handshake/auth all happen against the RPC server itself. void bridgeRpc(std::shared_ptr browser, http::request& req, beast::flat_buffer& residual); // Serves one request; returns true when the connection should be // kept open for the next request (HTTP keep-alive). bool serveHttp(asio::ip::tcp::socket& socket, const http::request& req); bool serveWatchProxy(asio::ip::tcp::socket& socket, const http::request& req); void sendToWatcher(uint64_t connId, const std::string& json); void syncLoop(std::stop_token stopToken); Config config_; asio::io_context ioContext_; std::optional acceptor_; std::thread serverThread_; std::atomic running_{false}; // Per-connection handlers run on a worker pool; ~ThreadPool drains // and joins them on stop. static constexpr std::size_t POOL_SIZE = 32; std::unique_ptr pool_; std::unordered_map< uint64_t, std::shared_ptr>> watchers_; std::mutex watchersMutex_; std::atomic nextConnId_{1}; // Live /rpc bridges (browser, upstream); shut down on stop() so // the blocking pump loops unblock and the pool can join. std::unordered_map, std::shared_ptr>> bridges_; std::mutex bridgesMutex_; WatchSessionManager watch_; std::jthread syncThread_; // Called from pool threads; guarded so Core can (re)install them // at any time. Both share one mutex (installed together at start). AvatarProvider avatarProvider_; EmojiProvider emojiProvider_; std::mutex avatarProviderMutex_; }; } // namespace Echoion::Web