Files
EchoionWeb/include/echoion/web/HttpServer.hpp
T
faumaray 9574aa8a23 making WEB
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-11 00:47:52 +03:00

154 lines
5.1 KiB
C++

#pragma once
#include "echoion/web/WatchSession.hpp"
#include <Void/Async/ThreadPool.hpp>
#include <boost/asio.hpp>
#include <boost/beast.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/websocket.hpp>
#include <atomic>
#include <cstdint>
#include <functional>
#include <memory>
#include <mutex>
#include <optional>
#include <stop_token>
#include <string>
#include <thread>
#include <unordered_map>
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://<page-host>:<rpcPort>.
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/<uid>. 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<std::optional<AvatarImage>(
const std::string& uid)>;
void setAvatarProvider(AvatarProvider provider)
{
std::lock_guard<std::mutex> lock(avatarProviderMutex_);
avatarProvider_ = std::move(provider);
}
// A custom emoji image, served at GET /emoji/<name>. Same shape
// and lifetime rules as the avatar provider; installed by Core
// (which owns the emoji database).
using EmojiProvider = std::function<std::optional<AvatarImage>(
const std::string& name)>;
void setEmojiProvider(EmojiProvider provider)
{
std::lock_guard<std::mutex> lock(avatarProviderMutex_);
emojiProvider_ = std::move(provider);
}
private:
void acceptLoop();
void doAccept();
void onAccept(asio::ip::tcp::socket socket);
void handleConnection(std::shared_ptr<asio::ip::tcp::socket> socket);
void handleWatch(
std::shared_ptr<websocket::stream<asio::ip::tcp::socket>> 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<asio::ip::tcp::socket> browser,
http::request<http::string_body>& 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<http::string_body>& req);
bool serveWatchProxy(asio::ip::tcp::socket& socket,
const http::request<http::string_body>& req);
void sendToWatcher(uint64_t connId, const std::string& json);
void syncLoop(std::stop_token stopToken);
Config config_;
asio::io_context ioContext_;
std::optional<asio::ip::tcp::acceptor> acceptor_;
std::thread serverThread_;
std::atomic<bool> 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<Void::Concurrency::ThreadPool> pool_;
std::unordered_map<
uint64_t,
std::shared_ptr<websocket::stream<asio::ip::tcp::socket>>>
watchers_;
std::mutex watchersMutex_;
std::atomic<uint64_t> 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<uint64_t,
std::pair<std::shared_ptr<asio::ip::tcp::socket>,
std::shared_ptr<asio::ip::tcp::socket>>>
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