Files
SpineParticlesWeb/connector/native/http_server.hpp
T
2026-11-09 10:08:29 +08:00

50 lines
1.0 KiB
C++

#pragma once
#include <atomic>
#include <cstdint>
#include <filesystem>
#include <functional>
#include <map>
#include <memory>
#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;
bool secure = false;
};
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,
const std::filesystem::path& identity = {});
void stop();
bool running() const { return running_.load(); }
private:
void accept_loop();
std::atomic<bool> running_{false};
std::intptr_t socket_ = -1;
std::shared_ptr<void> tls_identity_;
RequestHandler handler_;
void* thread_ = nullptr;
};
} // namespace connector