Change interface HTTPHandlerBase
This commit is contained in:
parent
3faba1ba72
commit
2677df4bdf
4 changed files with 8 additions and 12 deletions
|
@ -9,9 +9,8 @@ struct SomeComponent : public cserver::server::handlers::HTTPHandlerBaseWithAdde
|
||||||
inline constexpr SomeComponent(auto name, auto& context) :
|
inline constexpr SomeComponent(auto name, auto& context) :
|
||||||
HTTPHandlerBaseWithAdder(name, context) {};
|
HTTPHandlerBaseWithAdder(name, context) {};
|
||||||
|
|
||||||
inline auto HandleRequestThrow(const cserver::server::http::HTTPRequest& request,
|
inline auto HandleRequestThrow(const cserver::server::http::HTTPRequest& request) -> cserver::Task<cserver::server::http::HTTPResponse> {
|
||||||
cserver::server::http::HTTPResponse&) -> cserver::Task<std::string> {
|
co_return cserver::server::http::HTTPResponse{.body = request.url.data()};
|
||||||
co_return request.url.data();
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@ struct TaskProcessor {
|
||||||
if(this->ioContext.stopped()) {
|
if(this->ioContext.stopped()) {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
thread = std::jthread([&]{
|
thread = std::thread([&]{
|
||||||
if(this->ioContext.stopped()) {
|
if(this->ioContext.stopped()) {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
|
@ -15,19 +15,17 @@ struct HTTPHandlerBase {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
template <typename Self>
|
template <typename Self>
|
||||||
inline auto HandleRequest(this Self&& self, cserver::server::http::HTTPRequest&& request,
|
inline auto HandleRequest(this Self&& self, http::HTTPRequest&& request
|
||||||
cserver::server::http::HTTPResponse& response) -> Task<std::string> requires requires{self.HandleRequestThrow(std::move(request), response);} {
|
) -> Task<http::HTTPResponse> requires requires{self.HandleRequestThrow(std::move(request));} {
|
||||||
using T = std::remove_cvref_t<Self>;
|
using T = std::remove_cvref_t<Self>;
|
||||||
try {
|
try {
|
||||||
co_return co_await std::forward<Self>(self).HandleRequestThrow(std::move(request), response);
|
co_return co_await std::forward<Self>(self).HandleRequestThrow(std::move(request));
|
||||||
} catch(const std::exception& err) {
|
} catch(const std::exception& err) {
|
||||||
fmt::println("Error in handler with default name {}: {}", T::kName, err.what());
|
fmt::println("Error in handler with default name {}: {}", T::kName, err.what());
|
||||||
} catch(...) {
|
} catch(...) {
|
||||||
fmt::println("Error in handler with default name {}: Unknown Error", T::kName);
|
fmt::println("Error in handler with default name {}: Unknown Error", T::kName);
|
||||||
};
|
};
|
||||||
response.statusCode = 500;
|
co_return http::HTTPResponse{.statusCode = 500, .statusMessage = "Internal Server Error", .body = "Internal Server Error"};
|
||||||
response.statusMessage = "Internal Server Error";
|
|
||||||
co_return "Internal Server Error";
|
|
||||||
};
|
};
|
||||||
template <typename Self>
|
template <typename Self>
|
||||||
inline auto HandleRequestStream(this Self&& self, cserver::server::http::HTTPRequest&& request,
|
inline auto HandleRequestStream(this Self&& self, cserver::server::http::HTTPRequest&& request,
|
||||||
|
|
|
@ -55,8 +55,7 @@ struct Server : StopBlocker {
|
||||||
co_await Get<I>(this->handlers).HandleRequestStream(std::move(request), stream);
|
co_await Get<I>(this->handlers).HandleRequestStream(std::move(request), stream);
|
||||||
co_return;
|
co_return;
|
||||||
} else {
|
} else {
|
||||||
http::HTTPResponse response{};
|
http::HTTPResponse response = co_await Get<I>(this->handlers).HandleRequest(std::move(request));
|
||||||
response.body = co_await Get<I>(this->handlers).HandleRequest(std::move(request), response);
|
|
||||||
response.headers["Content-Length"] = std::to_string(response.body.size());
|
response.headers["Content-Length"] = std::to_string(response.body.size());
|
||||||
response.headers["Server"] = "cserver/1";
|
response.headers["Server"] = "cserver/1";
|
||||||
auto data = response.ToString();
|
auto data = response.ToString();
|
||||||
|
|
Loading…
Reference in a new issue