44 lines
885 B
C++
44 lines
885 B
C++
#pragma once
|
|
|
|
#include <atomic>
|
|
#include <functional>
|
|
#include <map>
|
|
#include <string>
|
|
|
|
namespace connector {
|
|
|
|
struct HttpRequest {
|
|
std::string method;
|
|
std::string target;
|
|
std::string path;
|
|
std::map<std::string, std::string> query;
|
|
std::map<std::string, std::string> headers;
|
|
std::string body;
|
|
};
|
|
|
|
struct HttpResponse {
|
|
int status = 200;
|
|
std::map<std::string, std::string> headers;
|
|
std::string body;
|
|
};
|
|
|
|
using RequestHandler = std::function<HttpResponse(const HttpRequest&)>;
|
|
|
|
class HttpServer {
|
|
public:
|
|
HttpServer();
|
|
~HttpServer();
|
|
bool start(unsigned short port, RequestHandler handler, std::string& error);
|
|
void stop();
|
|
bool running() const { return running_.load(); }
|
|
|
|
private:
|
|
void accept_loop();
|
|
std::atomic<bool> running_{false};
|
|
std::intptr_t socket_ = -1;
|
|
RequestHandler handler_;
|
|
void* thread_ = nullptr;
|
|
};
|
|
|
|
} // namespace connector
|