Router.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /***************************************************************************
  2. *
  3. * Project _____ __ ____ _ _
  4. * ( _ ) /__\ (_ _)_| |_ _| |_
  5. * )(_)( /(__)\ )( (_ _)(_ _)
  6. * (_____)(__)(__)(__) |_| |_|
  7. *
  8. *
  9. * Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ***************************************************************************/
  24. #ifndef oatpp_web_url_mapping_Router_hpp
  25. #define oatpp_web_url_mapping_Router_hpp
  26. #include "./Pattern.hpp"
  27. #include "oatpp/core/Types.hpp"
  28. #include <utility>
  29. #include <list>
  30. namespace oatpp { namespace web { namespace url { namespace mapping {
  31. /**
  32. * Class responsible to map "Path" to "Route" by "Path-Pattern".
  33. * @tparam Endpoint - endpoint of the route.
  34. */
  35. template<typename Endpoint>
  36. class Router : public base::Countable {
  37. private:
  38. /**
  39. * Pair &id:oatpp::web::url::mapping::Pattern; to Endpoint.
  40. */
  41. typedef std::pair<std::shared_ptr<Pattern>, Endpoint> Pair;
  42. /**
  43. * Convenience typedef &id:oatpp::data::share::StringKeyLabel;.
  44. */
  45. typedef oatpp::data::share::StringKeyLabel StringKeyLabel;
  46. public:
  47. /**
  48. * Resolved "Route" for "path-pattern"
  49. */
  50. class Route {
  51. private:
  52. bool m_valid;
  53. Endpoint m_endpoint;
  54. Pattern::MatchMap m_matchMap;
  55. public:
  56. /**
  57. * Default constructor.
  58. */
  59. Route()
  60. : m_valid(false)
  61. {}
  62. /**
  63. * Constructor.
  64. * @param pEndpoint - route endpoint.
  65. * @param pMatchMap - Match map of resolved path containing resolved path variables.
  66. */
  67. Route(const Endpoint& endpoint, Pattern::MatchMap&& matchMap)
  68. : m_valid(true)
  69. , m_endpoint(endpoint)
  70. , m_matchMap(matchMap)
  71. {}
  72. /**
  73. * Get Endpoint.
  74. */
  75. const Endpoint& getEndpoint() {
  76. return m_endpoint;
  77. }
  78. /**
  79. * Match map of resolved path containing resolved path variables.
  80. */
  81. const Pattern::MatchMap& getMatchMap() {
  82. return m_matchMap;
  83. }
  84. /**
  85. * Check if route is valid.
  86. * @return
  87. */
  88. bool isValid() {
  89. return m_valid;
  90. }
  91. explicit operator bool() const {
  92. return m_valid;
  93. }
  94. };
  95. private:
  96. std::list<Pair> m_endpointsByPattern;
  97. public:
  98. static std::shared_ptr<Router> createShared(){
  99. return std::make_shared<Router>();
  100. }
  101. /**
  102. * Add `path-pattern` to `endpoint` mapping.
  103. * @param pathPattern - path pattern for endpoint.
  104. * @param endpoint - route endpoint.
  105. */
  106. void route(const oatpp::String& pathPattern, const Endpoint& endpoint) {
  107. auto pattern = Pattern::parse(pathPattern);
  108. m_endpointsByPattern.push_back({pattern, endpoint});
  109. }
  110. /**
  111. * Resolve path to corresponding endpoint.
  112. * @param path
  113. * @return - &id:Router::Route;.
  114. */
  115. Route getRoute(const StringKeyLabel& path){
  116. for(auto& pair : m_endpointsByPattern) {
  117. Pattern::MatchMap matchMap;
  118. if(pair.first->match(path, matchMap)) {
  119. return Route(pair.second, std::move(matchMap));
  120. }
  121. }
  122. return Route();
  123. }
  124. void logRouterMappings(const oatpp::data::share::StringKeyLabel &branch) {
  125. for(auto& pair : m_endpointsByPattern) {
  126. auto mapping = pair.first->toString();
  127. OATPP_LOGD("Router", "url '%s %s' -> mapped", (const char*)branch.getData(), mapping->c_str());
  128. }
  129. }
  130. };
  131. }}}}
  132. #endif /* oatpp_web_url_mapping_Router_hpp */