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) :
|
||||
HTTPHandlerBaseWithAdder(name, context) {};
|
||||
|
||||
inline auto HandleRequestThrow(const cserver::server::http::HTTPRequest& request,
|
||||
cserver::server::http::HTTPResponse&) -> cserver::Task<std::string> {
|
||||
co_return request.url.data();
|
||||
inline auto HandleRequestThrow(const cserver::server::http::HTTPRequest& request) -> cserver::Task<cserver::server::http::HTTPResponse> {
|
||||
co_return cserver::server::http::HTTPResponse{.body = request.url.data()};
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ struct TaskProcessor {
|
|||
if(this->ioContext.stopped()) {
|
||||
return;
|
||||
};
|
||||
thread = std::jthread([&]{
|
||||
thread = std::thread([&]{
|
||||
if(this->ioContext.stopped()) {
|
||||
return;
|
||||
};
|
||||
|
|
|
@ -15,19 +15,17 @@ struct HTTPHandlerBase {
|
|||
});
|
||||
};
|
||||
template <typename Self>
|
||||
inline auto HandleRequest(this Self&& self, cserver::server::http::HTTPRequest&& request,
|
||||
cserver::server::http::HTTPResponse& response) -> Task<std::string> requires requires{self.HandleRequestThrow(std::move(request), response);} {
|
||||
inline auto HandleRequest(this Self&& self, http::HTTPRequest&& request
|
||||
) -> Task<http::HTTPResponse> requires requires{self.HandleRequestThrow(std::move(request));} {
|
||||
using T = std::remove_cvref_t<Self>;
|
||||
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) {
|
||||
fmt::println("Error in handler with default name {}: {}", T::kName, err.what());
|
||||
} catch(...) {
|
||||
fmt::println("Error in handler with default name {}: Unknown Error", T::kName);
|
||||
};
|
||||
response.statusCode = 500;
|
||||
response.statusMessage = "Internal Server Error";
|
||||
co_return "Internal Server Error";
|
||||
co_return http::HTTPResponse{.statusCode = 500, .statusMessage = "Internal Server Error", .body = "Internal Server Error"};
|
||||
};
|
||||
template <typename Self>
|
||||
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_return;
|
||||
} else {
|
||||
http::HTTPResponse response{};
|
||||
response.body = co_await Get<I>(this->handlers).HandleRequest(std::move(request), response);
|
||||
http::HTTPResponse response = co_await Get<I>(this->handlers).HandleRequest(std::move(request));
|
||||
response.headers["Content-Length"] = std::to_string(response.body.size());
|
||||
response.headers["Server"] = "cserver/1";
|
||||
auto data = response.ToString();
|
||||
|
|
Loading…
Reference in a new issue