summaryrefslogtreecommitdiff
path: root/include/tgbot/net/TgWebhookServer.h
diff options
context:
space:
mode:
authorOleg Morozenkov <m@oleg.rocks>2018-07-23 01:56:42 +0300
committerOleg Morozenkov <m@oleg.rocks>2018-07-23 01:56:42 +0300
commitd47ee877be5d1175bdc36f2d87881ddaf875a8e9 (patch)
tree7fd20cdc1236fe6b832ae980de12afd7071ebab9 /include/tgbot/net/TgWebhookServer.h
parentcea20d4078f2088dea0dd589f1cc9dd7ee22461b (diff)
Refactor http clients, fix webhook server, add more samples, change tabs to 4 spaces
Diffstat (limited to 'include/tgbot/net/TgWebhookServer.h')
-rw-r--r--include/tgbot/net/TgWebhookServer.h43
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;
};
}