From f43f3fe6613988591c57ed932a8675ebf60f2a39 Mon Sep 17 00:00:00 2001 From: lganzzzo Date: Mon, 25 Oct 2021 02:19:13 +0300 Subject: [PATCH] HttpRouter: better API. --- src/CMakeLists.txt | 1 + src/oatpp-test/web/ClientServerTestRunner.hpp | 2 +- .../codegen/api_controller/base_define.hpp | 24 ++++----- .../codegen/api_controller/cors_define.hpp | 7 +-- src/oatpp/web/server/HttpRouter.cpp | 49 +++++++++++++++++++ src/oatpp/web/server/HttpRouter.hpp | 27 +++++++++- src/oatpp/web/server/api/ApiController.cpp | 14 ++---- src/oatpp/web/server/api/ApiController.hpp | 35 +++---------- src/oatpp/web/server/api/Endpoint.cpp | 14 ++++++ src/oatpp/web/server/api/Endpoint.hpp | 19 +++++-- .../network/monitor/ConnectionMonitorTest.cpp | 1 + test/oatpp/web/server/ServerStopTest.cpp | 1 + 12 files changed, 136 insertions(+), 58 deletions(-) create mode 100644 src/oatpp/web/server/HttpRouter.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a011984d..7bbb34cd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -269,6 +269,7 @@ add_library(oatpp oatpp/web/server/HttpProcessor.cpp oatpp/web/server/HttpProcessor.hpp oatpp/web/server/HttpRequestHandler.hpp + oatpp/web/server/HttpRouter.cpp oatpp/web/server/HttpRouter.hpp oatpp/web/server/api/ApiController.cpp oatpp/web/server/api/ApiController.hpp diff --git a/src/oatpp-test/web/ClientServerTestRunner.hpp b/src/oatpp-test/web/ClientServerTestRunner.hpp index eaf08d6e..a21713b6 100644 --- a/src/oatpp-test/web/ClientServerTestRunner.hpp +++ b/src/oatpp-test/web/ClientServerTestRunner.hpp @@ -65,7 +65,7 @@ public: * @param controller */ void addController(const std::shared_ptr& controller) { - controller->addEndpointsToRouter(m_router); + m_router->route(controller->getEndpoints()); m_controllers.push_back(controller); } diff --git a/src/oatpp/codegen/api_controller/base_define.hpp b/src/oatpp/codegen/api_controller/base_define.hpp index ef30a65c..21e90505 100644 --- a/src/oatpp/codegen/api_controller/base_define.hpp +++ b/src/oatpp/codegen/api_controller/base_define.hpp @@ -275,15 +275,15 @@ OATPP_MACRO_API_CONTROLLER_PARAM_INFO X #define ENDPOINT_INFO(NAME) \ \ -std::shared_ptr Z__ENDPOINT_CREATE_ADDITIONAL_INFO_##NAME() { \ +std::shared_ptr Z__ENDPOINT_CREATE_ADDITIONAL_INFO_##NAME() { \ auto info = Z__EDNPOINT_INFO_GET_INSTANCE_##NAME(); \ Z__ENDPOINT_ADD_INFO_##NAME(info); \ return info; \ } \ \ -const std::shared_ptr Z__ENDPOINT_ADDITIONAL_INFO_##NAME = Z__ENDPOINT_CREATE_ADDITIONAL_INFO_##NAME(); \ +const std::shared_ptr Z__ENDPOINT_ADDITIONAL_INFO_##NAME = Z__ENDPOINT_CREATE_ADDITIONAL_INFO_##NAME(); \ \ -void Z__ENDPOINT_ADD_INFO_##NAME(const std::shared_ptr& info) +void Z__ENDPOINT_ADD_INFO_##NAME(const std::shared_ptr& info) // ENDPOINT MACRO // ------------------------------------------------------ @@ -299,10 +299,10 @@ static typename std::shared_ptr> Z__ENDPOINT_HANDLER_GET_INSTANCE_##N return handler; \ } \ \ -std::shared_ptr Z__EDNPOINT_INFO_GET_INSTANCE_##NAME() { \ - std::shared_ptr info = getEndpointInfo(#NAME); \ +std::shared_ptr Z__EDNPOINT_INFO_GET_INSTANCE_##NAME() { \ + std::shared_ptr info = getEndpointInfo(#NAME); \ if(!info){ \ - info = Endpoint::Info::createShared(); \ + info = oatpp::web::server::api::Endpoint::Info::createShared(); \ setEndpointInfo(#NAME, info); \ } \ return info; \ @@ -321,7 +321,7 @@ EndpointInfoBuilder Z__CREATE_ENDPOINT_INFO_##NAME = [this](){ \ return info; \ }; \ \ -const std::shared_ptr Z__ENDPOINT_##NAME = createEndpoint(m_endpoints, \ +const std::shared_ptr Z__ENDPOINT_##NAME = createEndpoint(m_endpoints, \ Z__ENDPOINT_HANDLER_GET_INSTANCE_##NAME(this), \ Z__CREATE_ENDPOINT_INFO_##NAME); @@ -351,7 +351,7 @@ auto info = Z__EDNPOINT_INFO_GET_INSTANCE_##NAME(); \ return info; \ }; \ \ -const std::shared_ptr Z__ENDPOINT_##NAME = createEndpoint(m_endpoints, \ +const std::shared_ptr Z__ENDPOINT_##NAME = createEndpoint(m_endpoints, \ Z__ENDPOINT_HANDLER_GET_INSTANCE_##NAME(this), \ Z__CREATE_ENDPOINT_INFO_##NAME); @@ -453,10 +453,10 @@ static typename std::shared_ptr> Z__ENDPOINT_HANDLER_GET_INSTANCE_##N return handler; \ } \ \ -std::shared_ptr Z__EDNPOINT_INFO_GET_INSTANCE_##NAME() { \ - std::shared_ptr info = getEndpointInfo(#NAME); \ +std::shared_ptr Z__EDNPOINT_INFO_GET_INSTANCE_##NAME() { \ + std::shared_ptr info = getEndpointInfo(#NAME); \ if(!info){ \ - info = Endpoint::Info::createShared(); \ + info = oatpp::web::server::api::Endpoint::Info::createShared(); \ setEndpointInfo(#NAME, info); \ } \ return info; \ @@ -476,7 +476,7 @@ EndpointInfoBuilder Z__CREATE_ENDPOINT_INFO_##NAME = [this](){ \ return info; \ }; \ \ -const std::shared_ptr Z__ENDPOINT_##NAME = createEndpoint(m_endpoints, \ +const std::shared_ptr Z__ENDPOINT_##NAME = createEndpoint(m_endpoints, \ Z__ENDPOINT_HANDLER_GET_INSTANCE_##NAME(this), \ Z__CREATE_ENDPOINT_INFO_##NAME); diff --git a/src/oatpp/codegen/api_controller/cors_define.hpp b/src/oatpp/codegen/api_controller/cors_define.hpp index 58a11db6..5c6b5626 100644 --- a/src/oatpp/codegen/api_controller/cors_define.hpp +++ b/src/oatpp/codegen/api_controller/cors_define.hpp @@ -46,9 +46,10 @@ EndpointInfoBuilder Z__CREATE_ENDPOINT_INFO_Z__CORS_OPTIONS_DECL_##ENDPOINTNAME return info; \ }; \ \ -const std::shared_ptr Z__ENDPOINT_Z__CORS_OPTIONS_DECL_##ENDPOINTNAME = createEndpoint(m_endpoints, \ - Z__ENDPOINT_HANDLER_GET_INSTANCE_Z__CORS_OPTIONS_DECL_##ENDPOINTNAME(this), \ - Z__CREATE_ENDPOINT_INFO_Z__CORS_OPTIONS_DECL_##ENDPOINTNAME);\ +const std::shared_ptr \ + Z__ENDPOINT_Z__CORS_OPTIONS_DECL_##ENDPOINTNAME = createEndpoint(m_endpoints, \ + Z__ENDPOINT_HANDLER_GET_INSTANCE_Z__CORS_OPTIONS_DECL_##ENDPOINTNAME(this), \ + Z__CREATE_ENDPOINT_INFO_Z__CORS_OPTIONS_DECL_##ENDPOINTNAME);\ \ std::shared_ptr \ Z__PROXY_METHOD_Z__CORS_OPTIONS_DECL_##ENDPOINTNAME(const std::shared_ptr& __request) \ diff --git a/src/oatpp/web/server/HttpRouter.cpp b/src/oatpp/web/server/HttpRouter.cpp new file mode 100644 index 00000000..745ffbdc --- /dev/null +++ b/src/oatpp/web/server/HttpRouter.cpp @@ -0,0 +1,49 @@ +/*************************************************************************** + * + * Project _____ __ ____ _ _ + * ( _ ) /__\ (_ _)_| |_ _| |_ + * )(_)( /(__)\ )( (_ _)(_ _) + * (_____)(__)(__)(__) |_| |_| + * + * + * Copyright 2018-present, Leonid Stryzhevskyi + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ***************************************************************************/ + +#include "HttpRouter.hpp" + +namespace oatpp { namespace web { namespace server { + +std::shared_ptr HttpRouter::createShared() { + return std::make_shared(); +} + +void HttpRouter::route(const std::shared_ptr& endpoint) { + route(endpoint->info()->method, endpoint->info()->path, endpoint->handler); +} + +void HttpRouter::route(const server::api::Endpoints& endpoints) { + for(auto& e : endpoints.list) { + route(e); + } +} + +std::shared_ptr HttpRouter::addController(const std::shared_ptr& controller) { + m_controllers.push_back(controller); + route(controller->getEndpoints()); + return controller; +} + +}}} diff --git a/src/oatpp/web/server/HttpRouter.hpp b/src/oatpp/web/server/HttpRouter.hpp index 28f1a28d..4ba29f8f 100644 --- a/src/oatpp/web/server/HttpRouter.hpp +++ b/src/oatpp/web/server/HttpRouter.hpp @@ -26,6 +26,9 @@ #define oatpp_web_server_HttpRouter_hpp #include "./HttpRequestHandler.hpp" + +#include "oatpp/web/server/api/ApiController.hpp" +#include "oatpp/web/server/api/Endpoint.hpp" #include "oatpp/web/url/mapping/Router.hpp" namespace oatpp { namespace web { namespace server { @@ -117,7 +120,29 @@ public: /** * Default HttpRouter. */ -typedef HttpRouterTemplate> HttpRouter; +class HttpRouter : public HttpRouterTemplate> { +private: + std::list> m_controllers; +public: + + /** + * Create shared HttpRouter. + * @return + */ + static std::shared_ptr createShared(); + + using HttpRouterTemplate::route; + void route(const std::shared_ptr& endpoint); + void route(const server::api::Endpoints& endpoints); + + /** + * Add controller and route its' endpoints. + * @param controller + * @return - `std::shared_ptr` to the controller added. + */ + std::shared_ptr addController(const std::shared_ptr& controller); + +}; }}} diff --git a/src/oatpp/web/server/api/ApiController.cpp b/src/oatpp/web/server/api/ApiController.cpp index d5a09f49..df665c35 100644 --- a/src/oatpp/web/server/api/ApiController.cpp +++ b/src/oatpp/web/server/api/ApiController.cpp @@ -22,18 +22,12 @@ * ***************************************************************************/ -#include "./ApiController.hpp" -#include +#include "ApiController.hpp" +#include "oatpp/web/server/handler/ErrorHandler.hpp" namespace oatpp { namespace web { namespace server { namespace api { - -void ApiController::addEndpointsToRouter(const std::shared_ptr& router){ - for (const auto& endpoint : *m_endpoints) { - router->route(endpoint->info()->method, endpoint->info()->path, endpoint->handler); - }; -} -std::shared_ptr ApiController::getEndpoints() { +const Endpoints& ApiController::getEndpoints() { return m_endpoints; } @@ -41,7 +35,7 @@ void ApiController::setEndpointInfo(const std::string& endpointName, const std:: m_endpointInfo[endpointName] = info; } -std::shared_ptr ApiController::getEndpointInfo(const std::string& endpointName) { +std::shared_ptr ApiController::getEndpointInfo(const std::string& endpointName) { return m_endpointInfo[endpointName]; } diff --git a/src/oatpp/web/server/api/ApiController.hpp b/src/oatpp/web/server/api/ApiController.hpp index 8b74184e..42d8c73f 100644 --- a/src/oatpp/web/server/api/ApiController.hpp +++ b/src/oatpp/web/server/api/ApiController.hpp @@ -22,16 +22,14 @@ * ***************************************************************************/ -#ifndef oatpp_web_server_rest_Controller_hpp -#define oatpp_web_server_rest_Controller_hpp +#ifndef oatpp_web_server_api_Controller_hpp +#define oatpp_web_server_api_Controller_hpp #include "./Endpoint.hpp" #include "oatpp/web/server/handler/AuthorizationHandler.hpp" #include "oatpp/web/server/handler/ErrorHandler.hpp" #include "oatpp/web/server/handler/AuthorizationHandler.hpp" -#include "oatpp/web/server/HttpConnectionHandler.hpp" -#include "oatpp/web/url/mapping/Router.hpp" #include "oatpp/web/protocol/http/incoming/Response.hpp" #include "oatpp/web/protocol/http/outgoing/Request.hpp" #include "oatpp/web/protocol/http/outgoing/ResponseFactory.hpp" @@ -51,10 +49,6 @@ class ApiController : public oatpp::base::Countable { protected: typedef ApiController __ControllerType; public: - /** - * Convenience typedef for &id:oatpp::web::server::HttpRouter;. - */ - typedef oatpp::web::server::HttpRouter Router; /** * Convenience typedef for &id:oatpp::web::protocol::http::outgoing::ResponseFactory;. @@ -96,16 +90,6 @@ public: */ typedef oatpp::web::protocol::http::QueryParams QueryParams; - /** - * Convenience typedef for &id:oatpp::web::server::api::Endpoint;. - */ - typedef oatpp::web::server::api::Endpoint Endpoint; - - /** - * Convenience typedef for list of &id:oatpp::web::server::api::Endpoint;. - */ - typedef std::list> Endpoints; - /** * Convenience typedef for &id:oatpp::web::server::HttpRequestHandler;. */ @@ -369,7 +353,7 @@ protected: std::shared_ptr getEndpointHandler(const std::string& endpointName); protected: - std::shared_ptr m_endpoints{std::make_shared()}; + Endpoints m_endpoints; std::shared_ptr m_errorHandler; std::shared_ptr m_defaultAuthorizationHandler; std::shared_ptr m_defaultObjectMapper; @@ -384,24 +368,19 @@ public: public: template - static std::shared_ptr createEndpoint(const std::shared_ptr& endpoints, + static std::shared_ptr createEndpoint(Endpoints& endpoints, const std::shared_ptr>& handler, const EndpointInfoBuilder& infoBuilder) { auto endpoint = Endpoint::createShared(handler, infoBuilder); - endpoints->push_back(endpoint); + endpoints.append(endpoint); return endpoint; } - - /** - * Subscribes all created endpoint-handlers to corresponding URLs in Router - */ - void addEndpointsToRouter(const std::shared_ptr& router); /** * Get list of Endpoints created via ENDPOINT macro */ - std::shared_ptr getEndpoints(); + const Endpoints& getEndpoints(); /** * [under discussion] @@ -594,4 +573,4 @@ struct ApiController::TypeInterpretation Endpoint::info() { return m_info; } +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +void Endpoints::append(const std::list>& endpoints) { + list.insert(list.end(), endpoints.begin(), endpoints.end()); +} + +void Endpoints::append(const Endpoints& endpoints) { + append(endpoints.list); +} + +void Endpoints::append(const std::shared_ptr& endpoint) { + list.push_back(endpoint); +} + }}}} diff --git a/src/oatpp/web/server/api/Endpoint.hpp b/src/oatpp/web/server/api/Endpoint.hpp index e4d98b62..71fff544 100644 --- a/src/oatpp/web/server/api/Endpoint.hpp +++ b/src/oatpp/web/server/api/Endpoint.hpp @@ -22,8 +22,8 @@ * ***************************************************************************/ -#ifndef oatpp_web_server_rest_Endpoint_hpp -#define oatpp_web_server_rest_Endpoint_hpp +#ifndef oatpp_web_server_api_Endpoint_hpp +#define oatpp_web_server_api_Endpoint_hpp #include "oatpp/web/server/HttpRequestHandler.hpp" @@ -286,7 +286,20 @@ private: std::function()> m_infoBuilder; }; + +/** + * Collection of endpoints. + */ +struct Endpoints { + + std::list> list; + + void append(const std::list>& endpoints); + void append(const Endpoints& endpoints); + void append(const std::shared_ptr& endpoint); + +}; }}}} -#endif /* oatpp_web_server_rest_Endpoint_hpp */ +#endif /* oatpp_web_server_api_Endpoint_hpp */ diff --git a/test/oatpp/network/monitor/ConnectionMonitorTest.cpp b/test/oatpp/network/monitor/ConnectionMonitorTest.cpp index eb59d498..a7d7be54 100644 --- a/test/oatpp/network/monitor/ConnectionMonitorTest.cpp +++ b/test/oatpp/network/monitor/ConnectionMonitorTest.cpp @@ -27,6 +27,7 @@ #include "oatpp/web/client/HttpRequestExecutor.hpp" #include "oatpp/web/server/AsyncHttpConnectionHandler.hpp" #include "oatpp/web/server/HttpConnectionHandler.hpp" +#include "oatpp/web/server/HttpRouter.hpp" #include "oatpp/web/protocol/http/outgoing/StreamingBody.hpp" #include "oatpp/network/monitor/ConnectionMonitor.hpp" diff --git a/test/oatpp/web/server/ServerStopTest.cpp b/test/oatpp/web/server/ServerStopTest.cpp index 1b61b7a7..7eda4e60 100644 --- a/test/oatpp/web/server/ServerStopTest.cpp +++ b/test/oatpp/web/server/ServerStopTest.cpp @@ -109,6 +109,7 @@ std::shared_ptr runServer(const std::shared_ptr& connectionProvider) { auto router = oatpp::web::server::HttpRouter::createShared(); + router->route("GET", "/stream", std::make_shared()); auto connectionHandler = oatpp::web::server::HttpConnectionHandler::createShared(router);