Skip to content

Commit

Permalink
-wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kamchatka-volcano committed Jan 3, 2024
1 parent 5458f2f commit 8c084dc
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ jobs:
lunchtoast_exec: "lunchtoast.exe",
shell_command: -shell="msys2 -c",
tags: -skip=linux,
nginx_exec: "c:/tools/nginx-1.25.2/nginx.exe",
nginx_exec: "c:/tools/nginx-1.25.3/nginx.exe",
nginx_cfg: "nginx_windows.conf"
}
- {
Expand Down
14 changes: 7 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ set(ASYNCGI_FCGI_RESPONDER_OBJECT_LIB ON)
SealLake_Bundle(
NAME asyncgi_fcgi_responder
GIT_REPOSITORY https://github.com/kamchatka-volcano/fcgi_responder.git
GIT_TAG v1.6.1
GIT_TAG v1.6.2
TEXT_REPLACEMENTS
"namespace fcgi" "namespace asyncgi::fcgi"
)

SealLake_Bundle(
NAME asyncgi_whaleroute
GIT_REPOSITORY https://github.com/kamchatka-volcano/whaleroute.git
GIT_TAG v2.0.1
DESTINATION include/asyncgi/detail/external
DIRECTORIES include/whaleroute
TEXT_REPLACEMENTS
NAME asyncgi_whaleroute
GIT_REPOSITORY https://github.com/kamchatka-volcano/whaleroute.git
GIT_TAG dev
DESTINATION include/asyncgi/detail/external
DIRECTORIES include/whaleroute
TEXT_REPLACEMENTS
"namespace whaleroute" "namespace asyncgi::whaleroute"
"WHALEROUTE_" "ASYNCGI_WHALEROUTE_"
)
Expand Down
21 changes: 16 additions & 5 deletions examples/example_request_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@

namespace http = asyncgi::http;

void guestBookPage(const asyncgi::Request& request, asyncgi::Response& response)
//void guestBookPage(const asyncgi::Request& request, asyncgi::Response& response)
//{
// if (request.path() == "/")
// response.send(R"(
// <h1>Guest book</h1>
// <p>No messages</p>
// )");
// else
// response.send(http::ResponseStatus::_404_Not_Found);
//}

http::Response guestBookPage(const asyncgi::Request& request)
{
if (request.path() == "/")
response.send(R"(
return std::string{R"(
<h1>Guest book</h1>
<p>No messages</p>
)");
else
response.send(http::ResponseStatus::_404_Not_Found);
)"};

return http::ResponseStatus::_404_Not_Found;
}

int main()
Expand Down
4 changes: 2 additions & 2 deletions examples/example_router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class GuestBookPage {
{
}

void operator()(const asyncgi::Request&, asyncgi::Response& response)
http::Response operator()(const asyncgi::Request&)
{
auto messages = state_->messages();
auto page = "<h1>Guest book</h1>"s;
Expand All @@ -46,7 +46,7 @@ class GuestBookPage {
"<input id=\"msg\" name=\"msg\" value=\"\">"
"<input value=\"Submit\" data-popup=\"true\" type=\"submit\">"
"</form>";
response.send(page);
return page;
}

private:
Expand Down
47 changes: 35 additions & 12 deletions include/asyncgi/requestprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@ namespace asyncgi {
namespace detail {
class ResponseContext;

template<typename TRequestProcessorArgs>
constexpr void checkRequestProcessorSignature(const TRequestProcessorArgs&)
template<typename TRequestProcessorReturnType, typename TRequestProcessorArgs>
constexpr void checkRequestProcessorSignature()
{
constexpr auto args = TRequestProcessorArgs{};
static_assert(args.size() == 2);
static_assert(std::is_same_v<const asyncgi::Request&, typename decltype(sfun::get<args.size() - 2>(args))::type>);
static_assert(std::is_same_v<asyncgi::Response&, typename decltype(sfun::get<args.size() - 1>(args))::type>);
if constexpr (std::is_same_v<TRequestProcessorReturnType, void>) {
constexpr auto args = TRequestProcessorArgs{};
static_assert(args.size() == 2);
static_assert(std::is_same_v<const asyncgi::Request&, typename decltype(sfun::get<0>(args))::type>);
static_assert(std::is_same_v<asyncgi::Response&, typename decltype(sfun::get<1>(args))::type>);
}
else {
static_assert(
std::is_same_v<TRequestProcessorReturnType, http::Response> ||
std::is_same_v<TRequestProcessorReturnType, fastcgi::Response>);
constexpr auto args = TRequestProcessorArgs{};
static_assert(args.size() == 1);
static_assert(std::is_same_v<const asyncgi::Request&, typename decltype(sfun::get<0>(args))::type>);
}
}
} // namespace detail

Expand All @@ -29,25 +39,38 @@ class RequestProcessor {
!std::is_same_v<std::remove_reference_t<TRequestProcessorFunc>, RequestProcessor>>* = nullptr>
RequestProcessor(TRequestProcessorFunc&& requestProcessor)
{
constexpr auto args = sfun::callable_args<TRequestProcessorFunc>{};
detail::checkRequestProcessorSignature(args);
detail::checkRequestProcessorSignature<
sfun::callable_return_type<TRequestProcessorFunc>,
sfun::callable_args<TRequestProcessorFunc>>();

if constexpr (std::is_lvalue_reference_v<TRequestProcessorFunc>) {
requestProcessorInvoker_ = [&requestProcessor](
const Request& request,
std::shared_ptr<detail::ResponseContext> responseContext)
{
auto response = Response{std::move(responseContext)};
requestProcessor(request, response);
if constexpr (std::is_same_v<sfun::callable_return_type<TRequestProcessorFunc>, void>) {
auto response = Response{std::move(responseContext)};
requestProcessor(request, response);
}
else {
auto response = requestProcessor(request);
Response{std::move(responseContext)}.send(response);
}
};
}
else {
requestProcessorInvoker_ = [requestProcessor = std::forward<TRequestProcessorFunc>(requestProcessor)](
const Request& request,
std::shared_ptr<detail::ResponseContext> responseContext)
{
auto response = Response{std::move(responseContext)};
requestProcessor(request, response);
if constexpr (std::is_same_v<sfun::callable_return_type<TRequestProcessorFunc>, void>) {
auto response = Response{std::move(responseContext)};
requestProcessor(request, response);
}
else {
auto response = requestProcessor(request);
Response{std::move(responseContext)}.send(response);
}
};
}
}
Expand Down
33 changes: 26 additions & 7 deletions include/asyncgi/router.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "types.h"
#include "detail/external/sfun/functional.h"
#include "detail/external/sfun/interface.h"
#include "detail/external/sfun/type_traits.h"
#include "detail/external/whaleroute/requestrouter.h"
#include "detail/routeresponsecontextaccessor.h"
#include "http/response.h"
Expand All @@ -27,8 +28,30 @@ class RouterIOAccess {
}
};

namespace detail {
struct ResponseSender {
template<typename TResponse>
void operator()(Response& responseSender, const TResponse& response)
{
if constexpr (sfun::is_optional_v<TResponse>) {
if (response.has_value())
responseSender.send(response.value());
}
else {
responseSender.send(response);
}
}

template<typename... TResponse>
void operator()(Response& responseSender, TResponse&&... response)
{
responseSender.send(response...);
}
};
} //namespace detail

template<typename TRouteContext = _>
class Router : public whaleroute::RequestRouter<Request, Response, http::Response, TRouteContext> {
class Router : public whaleroute::RequestRouter<Request, Response, detail::ResponseSender, TRouteContext> {
public:
explicit Router(IO& io)
: eventHandler_{io.eventHandler(RouterIOAccess::makeToken<TRouteContext>(sfun::access_token{*this}))}
Expand All @@ -40,8 +63,9 @@ class Router : public whaleroute::RequestRouter<Request, Response, http::Respons
auto requestProcessorQueuePtr = std::make_shared<whaleroute::RequestProcessorQueue>();
detail::RouterResponseContextAccessor::setRequestProcessorQueue(response, requestProcessorQueuePtr);
auto requestProcessorQueue =
whaleroute::RequestRouter<asyncgi::Request, asyncgi::Response, http::Response, TRouteContext>::
whaleroute::RequestRouter<asyncgi::Request, asyncgi::Response, detail::ResponseSender, TRouteContext>::
makeRequestProcessorQueue(request, response);

*requestProcessorQueuePtr = requestProcessorQueue;
requestProcessorQueuePtr->launch();
}
Expand All @@ -62,11 +86,6 @@ class Router : public whaleroute::RequestRouter<Request, Response, http::Respons
return response.isSent();
}

void setResponseValue(Response& response, const http::Response& httpResponse) final
{
response.send(httpResponse);
}

void onRouteParametersError(const Request&, Response& response, const whaleroute::RouteParameterError& error)
override
{
Expand Down

0 comments on commit 8c084dc

Please sign in to comment.