132 lines
4.1 KiB
C++
132 lines
4.1 KiB
C++
#pragma once
|
|
|
|
#include <Void/Async/ThreadPool.hpp>
|
|
#include <boost/asio.hpp>
|
|
#include <boost/beast.hpp>
|
|
#include <boost/beast/http.hpp>
|
|
|
|
#include <atomic>
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <unordered_map>
|
|
|
|
namespace Echoion::Web
|
|
{
|
|
namespace asio = boost::asio;
|
|
namespace beast = boost::beast;
|
|
namespace http = beast::http;
|
|
|
|
// Serves the static browser UI over HTTP 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;
|
|
// External VoidWatch deployment used by the temporary
|
|
// /watch redirect and exposed to the browser in
|
|
// /config.json.
|
|
std::string watchUrl;
|
|
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_; }
|
|
|
|
// 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. It may be
|
|
// installed before or after start() and survives restarts.
|
|
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);
|
|
}
|
|
|
|
struct DashboardSnapshot {
|
|
std::string body;
|
|
std::string etag;
|
|
};
|
|
using DashboardProvider = std::function<DashboardSnapshot()>;
|
|
void setDashboardProvider(DashboardProvider provider)
|
|
{
|
|
std::lock_guard<std::mutex> lock(dashboardProviderMutex_);
|
|
dashboardProvider_ = 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);
|
|
// 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);
|
|
|
|
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::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_;
|
|
|
|
// Called from pool threads; guarded so Core can (re)install it at
|
|
// any time.
|
|
AvatarProvider avatarProvider_;
|
|
std::mutex avatarProviderMutex_;
|
|
DashboardProvider dashboardProvider_;
|
|
std::mutex dashboardProviderMutex_;
|
|
};
|
|
|
|
} // namespace Echoion::Web
|