/** * * HttpControllersRouter.h * An Tao * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license * that can be found in the License file. * * Drogon * */ #pragma once #include "impl_forwards.h" #include "ControllerBinderBase.h" #include #include #include #include #include #include namespace drogon { class HttpControllerBinder; class HttpSimpleControllerBinder; class WebsocketControllerBinder; class HttpControllersRouter : public trantor::NonCopyable { public: static HttpControllersRouter &instance() { static HttpControllersRouter inst; return inst; } void init(const std::vector &ioLoops); // clean all resources void reset(); void registerHttpSimpleController( const std::string &pathName, const std::string &ctrlName, const std::vector &constraints); void registerWebSocketController( const std::string &pathName, const std::string &ctrlName, const std::vector &constraints); void registerWebSocketControllerRegex( const std::string ®Exp, const std::string &ctrlName, const std::vector &constraints); void addHttpPath(const std::string &path, const internal::HttpBinderBasePtr &binder, const std::vector &validMethods, const std::vector &middlewareNames, const std::string &handlerName = ""); void addHttpRegex(const std::string ®Exp, const internal::HttpBinderBasePtr &binder, const std::vector &validMethods, const std::vector &middlewareNames, const std::string &handlerName = ""); RouteResult route(const HttpRequestImplPtr &req); RouteResult routeWs(const HttpRequestImplPtr &req); std::vector getHandlersInfo() const; private: void addRegexCtrlBinder( const std::shared_ptr &binderPtr, const std::string &pathPattern, const std::string &pathParameterPattern, const std::vector &methods); struct SimpleControllerRouterItem { std::shared_ptr binders_[Invalid]{nullptr}; }; struct HttpControllerRouterItem { std::string pathParameterPattern_; std::string pathPattern_; std::regex regex_; std::shared_ptr binders_[Invalid]{nullptr}; }; struct WebSocketControllerRouterItem { std::shared_ptr binders_[Invalid]{nullptr}; }; struct RegExWebSocketControllerRouterItem { std::string pathPattern_; std::regex regex_; std::shared_ptr binders_[Invalid]{nullptr}; }; std::unordered_map simpleCtrlMap_; std::unordered_map ctrlMap_; std::vector ctrlVector_; // for regexp path std::unordered_map wsCtrlMap_; std::vector wsCtrlVector_; }; } // namespace drogon