42 lines
1.2 KiB
C++
42 lines
1.2 KiB
C++
|
#pragma once
|
||
|
|
||
|
#include <http/config.hpp>
|
||
|
#include <http/request.hpp>
|
||
|
#include <http/response.hpp>
|
||
|
#include <http/websocket_client.hpp>
|
||
|
#include <base/logger.hpp>
|
||
|
#include <base/types.hpp>
|
||
|
|
||
|
namespace base::http {
|
||
|
|
||
|
/// A HTTP server.
|
||
|
struct Server {
|
||
|
using SocketType = GenericProtocol::socket;
|
||
|
using StreamType = BeastStream<GenericProtocol>;
|
||
|
using EndpointType = GenericProtocol::endpoint;
|
||
|
|
||
|
using RouteHandler = std::function<Awaitable<Response>(Request& request)>;
|
||
|
using WebSocketRouteHandler = std::function<Awaitable<void>(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> impl;
|
||
|
};
|
||
|
|
||
|
} // namespace base::http
|