56 lines
1.4 KiB
C++
56 lines
1.4 KiB
C++
#pragma once
|
|
|
|
#include <boost/beast/core.hpp>
|
|
#include <boost/beast/http.hpp>
|
|
#include <boost/beast/version.hpp>
|
|
#include <boost/asio/ip/tcp.hpp>
|
|
#include <boost/asio/strand.hpp>
|
|
#include <memory>
|
|
#include <thread>
|
|
#include <vector>
|
|
#include <string>
|
|
|
|
#include "common.hpp"
|
|
#include "database.hpp"
|
|
|
|
namespace beast = boost::beast;
|
|
namespace http = beast::http;
|
|
namespace net = boost::asio;
|
|
|
|
|
|
class HttpSession : public std::enable_shared_from_this<HttpSession> {
|
|
public:
|
|
HttpSession(net::ip::tcp::socket&& socket, std::shared_ptr<Database> db);
|
|
void run();
|
|
|
|
private:
|
|
void readRequest();
|
|
void onRead(beast::error_code ec, std::size_t bytes_transferred);
|
|
void processRequest(http::request<http::string_body>&& req);
|
|
void writeResponse(http::response<http::string_body>&& res);
|
|
void handleError(beast::error_code ec, const char* what);
|
|
|
|
beast::tcp_stream stream;
|
|
beast::flat_buffer buffer;
|
|
std::shared_ptr<Database> db;
|
|
http::request<http::string_body> req;
|
|
};
|
|
|
|
class Server {
|
|
public:
|
|
Server(const std::string& address, int port, int threads, std::shared_ptr<Database> db);
|
|
void run();
|
|
void stop();
|
|
|
|
private:
|
|
void accept();
|
|
void onAccept(beast::error_code ec, net::ip::tcp::socket socket);
|
|
|
|
net::io_context ioc;
|
|
net::ip::tcp::acceptor acceptor;
|
|
std::shared_ptr<Database> db;
|
|
int threads;
|
|
std::vector<std::thread> threadPool;
|
|
net::steady_timer acceptTimer;
|
|
};
|