diff options
Diffstat (limited to 'include/tgbot/net/TgWebhookServer.h')
-rw-r--r-- | include/tgbot/net/TgWebhookServer.h | 43 |
1 files changed, 27 insertions, 16 deletions
diff --git a/include/tgbot/net/TgWebhookServer.h b/include/tgbot/net/TgWebhookServer.h index 696d126..953c1a9 100644 --- a/include/tgbot/net/TgWebhookServer.h +++ b/include/tgbot/net/TgWebhookServer.h @@ -23,6 +23,8 @@ #ifndef TGBOT_TGHTTPSERVER_H #define TGBOT_TGHTTPSERVER_H +#include <utility> + #include "tgbot/Bot.h" #include "tgbot/EventHandler.h" #include "tgbot/TgTypeParser.h" @@ -34,22 +36,31 @@ template<typename Protocol> class TgWebhookServer : public HttpServer<Protocol> { public: - TgWebhookServer(std::shared_ptr<boost::asio::basic_socket_acceptor<Protocol>> acceptor, const typename HttpServer<Protocol>::ServerHandler& handler) = delete; - - TgWebhookServer(std::shared_ptr<boost::asio::basic_socket_acceptor<Protocol>> acceptor, const std::string& path, const EventHandler* eventHandler) : - HttpServer<Protocol>(acceptor, [eventHandler, &path](const std::string& data, const std::unordered_map<std::string, std::string>& headers) -> std::string { - if (headers.at("method") == "POST" && headers.at("path") == path) { - eventHandler->handleUpdate(TgTypeParser::getInstance().parseJsonAndGetUpdate(TgTypeParser::getInstance().parseJson(data))); - } - return HttpParser::getInstance().generateResponse(""); - }) - { - } - - TgWebhookServer(std::shared_ptr<boost::asio::basic_socket_acceptor<Protocol>> acceptor, const std::string& path, const Bot& bot) : - TgWebhookServer(acceptor, path, &bot.getEventHandler()) - { - } + TgWebhookServer(const typename boost::asio::basic_socket_acceptor<Protocol>::endpoint_type& endpoint, const typename HttpServer<Protocol>::ServerHandler& handler) = delete; + + TgWebhookServer(const typename boost::asio::basic_socket_acceptor<Protocol>::endpoint_type& endpoint, std::string path, const EventHandler& eventHandler) + : HttpServer<Protocol>(endpoint, + [this](const std::string& _1, const std::unordered_map<std::string, std::string>& _2) { return _handle(_1, _2); }), + _path(std::move(path)), _eventHandler(eventHandler), _tgTypeParser() + { + } + + TgWebhookServer(const typename boost::asio::basic_socket_acceptor<Protocol>::endpoint_type& endpoint, const Bot& bot) + : TgWebhookServer(endpoint, "/" + bot.getToken(), bot.getEventHandler()) + { + } + +private: + std::string _handle(const std::string& data, const std::unordered_map<std::string, std::string>& headers) { + if (headers.at("_method") == "POST" && headers.at("_path") == _path) { + _eventHandler.handleUpdate(_tgTypeParser.parseJsonAndGetUpdate(_tgTypeParser.parseJson(data))); + } + return HttpServer<Protocol>::_httpParser.generateResponse("", "text/plain", 200, "OK", false); + } + + const EventHandler& _eventHandler; + const std::string _path; + const TgTypeParser _tgTypeParser; }; } |