-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_server.cpp
134 lines (104 loc) · 3.5 KB
/
main_server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "CivetServer.h"
#include <string>
#include <thread>
static volatile bool exitNow = false;
//#define MINIMAL_EMPTY_SERVER
#ifndef MINIMAL_EMPTY_SERVER
#include <QCoreApplication>
#include <QSettings>
#include <QFile>
#include <QDebug>
#include <QDir>
#include "routeshandler.h"
#include "telegramhelper.h"
#include "telegramwebhook.h"
bool parseConfig(QString& tgmToken, QString& storageRoot, bool& tgmPolling, std::vector<std::string>& options){
if(!QFile::exists("server.ini")){
qDebug() << "Missing server.ini";
return false;
}
QSettings settings("server.ini", QSettings::IniFormat);
tgmToken = settings.value("telegram/token").toString();
if(tgmToken.isEmpty()) {
qDebug() << "Missing telegram/token in server.ini";
return false;
}
tgmPolling = settings.value("telegram/polling", "N").toString().compare("Y", Qt::CaseInsensitive) == 0;
storageRoot = settings.value("general/sessions", "storage").toString();
QDir sessions(storageRoot);
if(!sessions.exists()) {
qDebug() << "Folder:" << sessions.path() << "does not exists";
return false;
}
auto getOpt = [&](const QString& varName, const QString& defVal = ""){
QString optStr = settings.value(varName, defVal).toString();
return optStr.toStdString();
};
options.push_back( "document_root" );
options.push_back( getOpt("general/document_root", "www") );
options.push_back( "listening_ports" );
options.push_back( getOpt("general/port", "8081") );
return true;
}
int main(int argc, char * argv[])
{
QCoreApplication a(argc, argv);
//initialize the BOT server
QSettings settings("server.ini", QSettings::IniFormat);
QString tgmToken, storageRoot;
bool tgmPolling(false);
std::vector<std::string> options;
if(!parseConfig(tgmToken, storageRoot, tgmPolling, options))
return 1;
//start web server
CivetServer server(options);
//register web handlers
RoutesHandler h_r;
h_r.parseRoutes();
server.addHandler("/routes", h_r);
TelegramWebHook tgmHook(tgmToken, storageRoot, h_r);
server.addHandler("/telegramNewUpdate", tgmHook); //this is for tgm webhook
//helper object for polling mode
TelegramHelper pollingLoop(tgmToken, storageRoot, h_r);
while (!exitNow) {
std::this_thread::sleep_for (std::chrono::seconds(2));
if(tgmPolling) //only if polling enabled
pollingLoop.checkUpdates();
}
return 0;
}
#else
class ExampleHandler : public CivetHandler
{
public:
bool handleGet(CivetServer *server, struct mg_connection *conn)
{
mg_printf(conn,
"HTTP/1.1 200 OK\r\nContent-Type: "
"text/html\r\nConnection: close\r\n\r\n");
mg_printf(conn,
"<html><body>\r\n");
mg_printf(conn,
"<h2>This is an example text from a C++ handler</h2>\r\n");
std::string s = "";
if (server->getParam(conn, "param", s))
mg_printf(conn, "<p>param set to %s</p>", s.c_str());
else
mg_printf(conn, "<p>param not set</p>");
mg_printf(conn, "</body></html>\r\n");
return true;
}
};
int main(int /*argc*/, char * /*argv*/[])
{
CivetServer server({
"document_root", ".",
"listening_ports", "8081"
});
ExampleHandler h_example;
server.addHandler("/example", h_example);
while (!exitNow)
std::this_thread::sleep_for (std::chrono::seconds(2));
return 0;
}
#endif