连接器客户端
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
#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
|
||||
Reference in New Issue
Block a user