mirror of
https://github.com/oatpp/oatpp.git
synced 2025-03-13 18:06:47 +08:00
HttpRouter: better API.
This commit is contained in:
parent
ddb25bed54
commit
f43f3fe661
@ -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
|
||||
|
@ -65,7 +65,7 @@ public:
|
||||
* @param controller
|
||||
*/
|
||||
void addController(const std::shared_ptr<ApiController>& controller) {
|
||||
controller->addEndpointsToRouter(m_router);
|
||||
m_router->route(controller->getEndpoints());
|
||||
m_controllers.push_back(controller);
|
||||
}
|
||||
|
||||
|
@ -275,15 +275,15 @@ OATPP_MACRO_API_CONTROLLER_PARAM_INFO X
|
||||
|
||||
#define ENDPOINT_INFO(NAME) \
|
||||
\
|
||||
std::shared_ptr<Endpoint::Info> Z__ENDPOINT_CREATE_ADDITIONAL_INFO_##NAME() { \
|
||||
std::shared_ptr<oatpp::web::server::api::Endpoint::Info> 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<Endpoint::Info> Z__ENDPOINT_ADDITIONAL_INFO_##NAME = Z__ENDPOINT_CREATE_ADDITIONAL_INFO_##NAME(); \
|
||||
const std::shared_ptr<oatpp::web::server::api::Endpoint::Info> Z__ENDPOINT_ADDITIONAL_INFO_##NAME = Z__ENDPOINT_CREATE_ADDITIONAL_INFO_##NAME(); \
|
||||
\
|
||||
void Z__ENDPOINT_ADD_INFO_##NAME(const std::shared_ptr<Endpoint::Info>& info)
|
||||
void Z__ENDPOINT_ADD_INFO_##NAME(const std::shared_ptr<oatpp::web::server::api::Endpoint::Info>& info)
|
||||
|
||||
// ENDPOINT MACRO // ------------------------------------------------------
|
||||
|
||||
@ -299,10 +299,10 @@ static typename std::shared_ptr<Handler<T>> Z__ENDPOINT_HANDLER_GET_INSTANCE_##N
|
||||
return handler; \
|
||||
} \
|
||||
\
|
||||
std::shared_ptr<Endpoint::Info> Z__EDNPOINT_INFO_GET_INSTANCE_##NAME() { \
|
||||
std::shared_ptr<Endpoint::Info> info = getEndpointInfo(#NAME); \
|
||||
std::shared_ptr<oatpp::web::server::api::Endpoint::Info> Z__EDNPOINT_INFO_GET_INSTANCE_##NAME() { \
|
||||
std::shared_ptr<oatpp::web::server::api::Endpoint::Info> 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<Endpoint> Z__ENDPOINT_##NAME = createEndpoint(m_endpoints, \
|
||||
const std::shared_ptr<oatpp::web::server::api::Endpoint> 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<Endpoint> Z__ENDPOINT_##NAME = createEndpoint(m_endpoints, \
|
||||
const std::shared_ptr<oatpp::web::server::api::Endpoint> 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<Handler<T>> Z__ENDPOINT_HANDLER_GET_INSTANCE_##N
|
||||
return handler; \
|
||||
} \
|
||||
\
|
||||
std::shared_ptr<Endpoint::Info> Z__EDNPOINT_INFO_GET_INSTANCE_##NAME() { \
|
||||
std::shared_ptr<Endpoint::Info> info = getEndpointInfo(#NAME); \
|
||||
std::shared_ptr<oatpp::web::server::api::Endpoint::Info> Z__EDNPOINT_INFO_GET_INSTANCE_##NAME() { \
|
||||
std::shared_ptr<oatpp::web::server::api::Endpoint::Info> 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<Endpoint> Z__ENDPOINT_##NAME = createEndpoint(m_endpoints, \
|
||||
const std::shared_ptr<oatpp::web::server::api::Endpoint> Z__ENDPOINT_##NAME = createEndpoint(m_endpoints, \
|
||||
Z__ENDPOINT_HANDLER_GET_INSTANCE_##NAME(this), \
|
||||
Z__CREATE_ENDPOINT_INFO_##NAME);
|
||||
|
||||
|
@ -46,9 +46,10 @@ EndpointInfoBuilder Z__CREATE_ENDPOINT_INFO_Z__CORS_OPTIONS_DECL_##ENDPOINTNAME
|
||||
return info; \
|
||||
}; \
|
||||
\
|
||||
const std::shared_ptr<Endpoint> 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<oatpp::web::server::api::Endpoint> \
|
||||
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<oatpp::web::protocol::http::outgoing::Response> \
|
||||
Z__PROXY_METHOD_Z__CORS_OPTIONS_DECL_##ENDPOINTNAME(const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& __request) \
|
||||
|
49
src/oatpp/web/server/HttpRouter.cpp
Normal file
49
src/oatpp/web/server/HttpRouter.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* Project _____ __ ____ _ _
|
||||
* ( _ ) /__\ (_ _)_| |_ _| |_
|
||||
* )(_)( /(__)\ )( (_ _)(_ _)
|
||||
* (_____)(__)(__)(__) |_| |_|
|
||||
*
|
||||
*
|
||||
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
|
||||
*
|
||||
* 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> HttpRouter::createShared() {
|
||||
return std::make_shared<HttpRouter>();
|
||||
}
|
||||
|
||||
void HttpRouter::route(const std::shared_ptr<server::api::Endpoint>& 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<server::api::ApiController> HttpRouter::addController(const std::shared_ptr<server::api::ApiController>& controller) {
|
||||
m_controllers.push_back(controller);
|
||||
route(controller->getEndpoints());
|
||||
return controller;
|
||||
}
|
||||
|
||||
}}}
|
@ -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<std::shared_ptr<HttpRequestHandler>> HttpRouter;
|
||||
class HttpRouter : public HttpRouterTemplate<std::shared_ptr<HttpRequestHandler>> {
|
||||
private:
|
||||
std::list<std::shared_ptr<server::api::ApiController>> m_controllers;
|
||||
public:
|
||||
|
||||
/**
|
||||
* Create shared HttpRouter.
|
||||
* @return
|
||||
*/
|
||||
static std::shared_ptr<HttpRouter> createShared();
|
||||
|
||||
using HttpRouterTemplate::route;
|
||||
void route(const std::shared_ptr<server::api::Endpoint>& 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<server::api::ApiController> addController(const std::shared_ptr<server::api::ApiController>& controller);
|
||||
|
||||
};
|
||||
|
||||
}}}
|
||||
|
||||
|
@ -22,18 +22,12 @@
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
#include "./ApiController.hpp"
|
||||
#include <oatpp/web/server/handler/ErrorHandler.hpp>
|
||||
#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>& router){
|
||||
for (const auto& endpoint : *m_endpoints) {
|
||||
router->route(endpoint->info()->method, endpoint->info()->path, endpoint->handler);
|
||||
};
|
||||
}
|
||||
|
||||
std::shared_ptr<ApiController::Endpoints> 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::Endpoint::Info> ApiController::getEndpointInfo(const std::string& endpointName) {
|
||||
std::shared_ptr<Endpoint::Info> ApiController::getEndpointInfo(const std::string& endpointName) {
|
||||
return m_endpointInfo[endpointName];
|
||||
}
|
||||
|
||||
|
@ -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<std::shared_ptr<Endpoint>> Endpoints;
|
||||
|
||||
/**
|
||||
* Convenience typedef for &id:oatpp::web::server::HttpRequestHandler;.
|
||||
*/
|
||||
@ -369,7 +353,7 @@ protected:
|
||||
std::shared_ptr<RequestHandler> getEndpointHandler(const std::string& endpointName);
|
||||
|
||||
protected:
|
||||
std::shared_ptr<Endpoints> m_endpoints{std::make_shared<Endpoints>()};
|
||||
Endpoints m_endpoints;
|
||||
std::shared_ptr<handler::ErrorHandler> m_errorHandler;
|
||||
std::shared_ptr<handler::AuthorizationHandler> m_defaultAuthorizationHandler;
|
||||
std::shared_ptr<oatpp::data::mapping::ObjectMapper> m_defaultObjectMapper;
|
||||
@ -384,24 +368,19 @@ public:
|
||||
public:
|
||||
|
||||
template<class T>
|
||||
static std::shared_ptr<Endpoint> createEndpoint(const std::shared_ptr<Endpoints>& endpoints,
|
||||
static std::shared_ptr<Endpoint> createEndpoint(Endpoints& endpoints,
|
||||
const std::shared_ptr<Handler<T>>& 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>& router);
|
||||
|
||||
/**
|
||||
* Get list of Endpoints created via ENDPOINT macro
|
||||
*/
|
||||
std::shared_ptr<Endpoints> getEndpoints();
|
||||
const Endpoints& getEndpoints();
|
||||
|
||||
/**
|
||||
* [under discussion]
|
||||
@ -594,4 +573,4 @@ struct ApiController::TypeInterpretation <data::mapping::type::EnumObjectWrapper
|
||||
|
||||
}}}}
|
||||
|
||||
#endif /* oatpp_web_server_rest_Controller_hpp */
|
||||
#endif /* oatpp_web_server_api_Controller_hpp */
|
||||
|
@ -116,4 +116,18 @@ std::shared_ptr<Endpoint::Info> Endpoint::info() {
|
||||
return m_info;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void Endpoints::append(const std::list<std::shared_ptr<Endpoint>>& 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>& endpoint) {
|
||||
list.push_back(endpoint);
|
||||
}
|
||||
|
||||
}}}}
|
||||
|
@ -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<std::shared_ptr<Endpoint::Info>()> m_infoBuilder;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Collection of endpoints.
|
||||
*/
|
||||
struct Endpoints {
|
||||
|
||||
std::list<std::shared_ptr<Endpoint>> list;
|
||||
|
||||
void append(const std::list<std::shared_ptr<Endpoint>>& endpoints);
|
||||
void append(const Endpoints& endpoints);
|
||||
void append(const std::shared_ptr<Endpoint>& endpoint);
|
||||
|
||||
};
|
||||
|
||||
}}}}
|
||||
|
||||
#endif /* oatpp_web_server_rest_Endpoint_hpp */
|
||||
#endif /* oatpp_web_server_api_Endpoint_hpp */
|
||||
|
@ -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"
|
||||
|
@ -109,6 +109,7 @@ std::shared_ptr<oatpp::network::Server>
|
||||
runServer(const std::shared_ptr<oatpp::network::ServerConnectionProvider>& connectionProvider) {
|
||||
|
||||
auto router = oatpp::web::server::HttpRouter::createShared();
|
||||
|
||||
router->route("GET", "/stream", std::make_shared<StreamingHandler>());
|
||||
|
||||
auto connectionHandler = oatpp::web::server::HttpConnectionHandler::createShared(router);
|
||||
|
Loading…
x
Reference in New Issue
Block a user