加协程
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include "../Data_Source/export.h"
|
||||
#include "../State_Report/State_Report.h"
|
||||
#include "../global_include.h"
|
||||
#include "Ucoro_Drogon_Glue.h"
|
||||
#include "Config.h"
|
||||
#include <drogon/drogon.h>
|
||||
|
||||
@@ -72,6 +73,7 @@ public:
|
||||
std::string rise = "rise_";
|
||||
std::string fall = "fall_";
|
||||
using Handler = std::function<void(const drogon::HttpRequestPtr&, const drogon::HttpResponsePtr&)>;
|
||||
using CoroHandler = std::function<drogon::Task<>(const drogon::HttpRequestPtr&, const drogon::HttpResponsePtr&)>;
|
||||
bool listen(const std::string& host, int port);
|
||||
|
||||
~Web_Server() = default;
|
||||
@@ -103,6 +105,36 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
Web_Server& PostCoro(const std::string& pattern, CoroHandler handler) {
|
||||
drogon::app().registerHandler(
|
||||
pattern,
|
||||
[handler = std::move(handler)](
|
||||
const drogon::HttpRequestPtr& request,
|
||||
std::function<void(const drogon::HttpResponsePtr&)>&& callback) {
|
||||
auto resp = drogon::HttpResponse::newHttpResponse();
|
||||
resp->setContentTypeCode(drogon::CT_APPLICATION_JSON);
|
||||
|
||||
drogon::async_run(
|
||||
[handler, request, resp, callback = std::move(callback)]() mutable -> drogon::Task<> {
|
||||
try {
|
||||
co_await handler(request, resp);
|
||||
} catch (const std::exception& error) {
|
||||
resp->setStatusCode(drogon::k500InternalServerError);
|
||||
resp->setBody(Psc::JSON::object({{"error", std::string(error.what())}}).to_json_string());
|
||||
} catch (...) {
|
||||
resp->setStatusCode(drogon::k500InternalServerError);
|
||||
resp->setBody(Psc::JSON::object({{"error", "unknown error"}}).to_json_string());
|
||||
}
|
||||
callback(resp);
|
||||
co_return;
|
||||
});
|
||||
},
|
||||
{drogon::Post}
|
||||
);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Web_Server& Get(const std::string& pattern, Handler handler) {
|
||||
drogon::app().registerHandlerViaRegex(
|
||||
pattern,
|
||||
@@ -120,6 +152,36 @@ public:
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Web_Server& GetCoro(const std::string& pattern, CoroHandler handler) {
|
||||
drogon::app().registerHandlerViaRegex(
|
||||
pattern,
|
||||
[handler = std::move(handler)](
|
||||
const drogon::HttpRequestPtr& request,
|
||||
std::function<void(const drogon::HttpResponsePtr&)>&& callback) {
|
||||
auto resp = drogon::HttpResponse::newHttpResponse();
|
||||
resp->setContentTypeCode(drogon::CT_APPLICATION_JSON);
|
||||
|
||||
drogon::async_run(
|
||||
[handler, request, resp, callback = std::move(callback)]() mutable -> drogon::Task<> {
|
||||
try {
|
||||
co_await handler(request, resp);
|
||||
} catch (const std::exception& error) {
|
||||
resp->setStatusCode(drogon::k500InternalServerError);
|
||||
resp->setBody(Psc::JSON::object({{"error", std::string(error.what())}}).to_json_string());
|
||||
} catch (...) {
|
||||
resp->setStatusCode(drogon::k500InternalServerError);
|
||||
resp->setBody(Psc::JSON::object({{"error", "unknown error"}}).to_json_string());
|
||||
}
|
||||
callback(resp);
|
||||
co_return;
|
||||
});
|
||||
},
|
||||
{drogon::Get}
|
||||
);
|
||||
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user