1 parent
1c96d5973b
commit
e80dbc2fda
38 files changed
+1352
-6335
No files matched your search
@@ -1,12 +1,9 @@
|
||||
#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>
|
||||
@@ -14,7 +11,6 @@
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <stop_token>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
@@ -24,12 +20,10 @@ 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.
|
||||
// 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:
|
||||
@@ -41,6 +35,10 @@ namespace Echoion::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.
|
||||
@@ -54,19 +52,10 @@ namespace Echoion::Web
|
||||
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.
|
||||
// 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;
|
||||
@@ -79,15 +68,15 @@ namespace Echoion::Web
|
||||
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)
|
||||
struct DashboardSnapshot {
|
||||
std::string body;
|
||||
std::string etag;
|
||||
};
|
||||
using DashboardProvider = std::function<DashboardSnapshot()>;
|
||||
void setDashboardProvider(DashboardProvider provider)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(avatarProviderMutex_);
|
||||
emojiProvider_ = std::move(provider);
|
||||
std::lock_guard<std::mutex> lock(dashboardProviderMutex_);
|
||||
dashboardProvider_ = std::move(provider);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -95,9 +84,8 @@ namespace Echoion::Web
|
||||
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);
|
||||
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
|
||||
@@ -109,10 +97,6 @@ namespace Echoion::Web
|
||||
// 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_;
|
||||
@@ -125,29 +109,23 @@ namespace Echoion::Web
|
||||
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>>>
|
||||
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).
|
||||
// Called from pool threads; guarded so Core can (re)install it at
|
||||
// any time.
|
||||
AvatarProvider avatarProvider_;
|
||||
EmojiProvider emojiProvider_;
|
||||
std::mutex avatarProviderMutex_;
|
||||
DashboardProvider dashboardProvider_;
|
||||
std::mutex dashboardProviderMutex_;
|
||||
};
|
||||
|
||||
} // namespace Echoion::Web
|
||||
Reference in new issue
Block a user