18#ifndef TRINITYCORE_HTTP_SERVICE_H
19#define TRINITYCORE_HTTP_SERVICE_H
29#include <boost/uuid/uuid.hpp>
33#include <shared_mutex>
57 explicit DispatcherService(std::string_view loggerSuffix) : _logger(
"server.http.dispatcher.")
59 _logger.append(loggerSuffix);
69 void RegisterHandler(boost::beast::http::verb method, std::string_view path,
85 explicit SessionService(std::string_view loggerSuffix) : _logger(
"server.http.session.")
87 _logger.append(loggerSuffix);
93 std::shared_ptr<SessionState> FindAndRefreshSessionState(std::string_view
id, boost::asio::ip::address
const& address);
94 void MarkSessionInactive(boost::uuids::uuid
const&
id);
97 void InitAndStoreSessionState(std::shared_ptr<SessionState> state, boost::asio::ip::address
const& address);
99 void KillInactiveSessions();
103 std::map<boost::uuids::uuid, std::shared_ptr<SessionState>>
_sessions;
112template<
typename Callable,
typename SessionImpl>
115template<
typename SessionImpl>
142 template<HttpRequestHandler<SessionImpl> Callable>
147 return handler(std::static_pointer_cast<SessionImpl>(std::move(session)), context);
154 std::shared_ptr<SessionState> state = std::make_shared<SessionState>();
179 threads[i]._service =
const_cast<HttpService*
>(
this);
std::optional< T > Optional
Optional helper class to wrap optional values within.
virtual void StopNetwork()
int32 GetNetworkThreadCount() const
HttpMethodHandlerMap _getHandlers
DispatcherService(std::string_view loggerSuffix)
HttpMethodHandlerMap _postHandlers
void RegisterHandler(boost::beast::http::verb method, std::string_view path, std::function< RequestHandlerResult(std::shared_ptr< AbstractSocket > session, RequestContext &context)> handler, RequestHandlerFlag flags=RequestHandlerFlag::None)
std::map< std::string, RequestHandler, std::less<> > HttpMethodHandlerMap
SessionService * _service
void SocketRemoved(std::shared_ptr< SessionImpl > session) override
Asio::IoContext * _ioContext
virtual std::shared_ptr< SessionState > CreateNewSessionState(boost::asio::ip::address const &address)
NetworkThread< SessionImpl > * CreateThreads() const override
bool StartNetwork(Asio::IoContext &ioContext, std::string const &bindIp, uint16 port, int32 threadCount=1) override
HttpService(std::string_view loggerSuffix)
void StopNetwork() override
void RegisterHandler(boost::beast::http::verb method, std::string_view path, Callable handler, RequestHandlerFlag flags=RequestHandlerFlag::None)
std::map< boost::uuids::uuid, std::shared_ptr< SessionState > > _sessions
std::unique_ptr< Asio::DeadlineTimer > _inactiveSessionsKillTimer
std::set< boost::uuids::uuid > _inactiveSessions
SessionService(std::string_view loggerSuffix)
std::shared_mutex _sessionsMutex
void Start(Asio::IoContext &ioContext)
void InitAndStoreSessionState(std::shared_ptr< SessionState > state, boost::asio::ip::address const &address)
void MarkSessionInactive(boost::uuids::uuid const &id)
std::mutex _inactiveSessionsMutex
@ DoNotLogResponseContent
DEFINE_ENUM_FLAG(RequestHandlerFlag)
std::function< RequestHandlerResult(std::shared_ptr< AbstractSocket > session, RequestContext &context)> Func
EnumFlag< RequestHandlerFlag > Flags