#pragma once #include #include #include #include #include #include namespace base::http { /// A HTTP server. struct Server { using SocketType = GenericProtocol::socket; using StreamType = BeastStream; using EndpointType = GenericProtocol::endpoint; using RouteHandler = std::function(Request& request)>; using WebSocketRouteHandler = std::function(Request& request, WebSocketClient::Ptr&& ws)>; Server(asio::any_io_executor ioc, asio::ip::tcp::endpoint ep); ~Server(); void Start(); void Stop(); bool Listening() const; /// Declare a route void On(beast::http::verb verb, std::string_view path, RouteHandler handler); void Get(std::string_view path, RouteHandler handler) { On(beast::http::verb::get, path, handler); } void GetWebSockets(std::string_view path, WebSocketRouteHandler wsRoute); void Post(std::string_view path, RouteHandler handler) { On(beast::http::verb::post, path, handler); } private: struct Impl; Unique impl; }; } // namespace base::http