-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
74 lines (53 loc) · 1.83 KB
/
main.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
#include <QCoreApplication>
#include <QCommandLineParser>
#include <QSettings>
#include <httpserver.h>
QSettings *getSettings(QString &configPath)
{
QSettings *settings = new QSettings(configPath, QSettings::IniFormat);
settings->beginGroup("server");
if (!settings->contains("port")) {
settings->setValue("port", 80);
}
if (!settings->contains("document_root")) {
settings->setValue("document_root", QCoreApplication::applicationDirPath());
}
if (!settings->contains("keep_alive_allowed")) {
settings->setValue("keep_alive_allowed", true);
}
if (!settings->contains("keep_alive_timeout")) {
settings->setValue("keep_alive_timeout", 5000);
}
settings->endGroup();
settings->beginGroup("cache");
if (!settings->contains("max_cached_file_size")) {
settings->setValue("max_cached_file_size", 100);
}
if (!settings->contains("max_total_cache_size")) {
settings->setValue("max_total_cache_size", 20000);
}
settings->endGroup();
return settings;
}
void parseCommandLineArguments(QCoreApplication &a, QString &configPath)
{
QCommandLineParser parser;
QCommandLineOption configOption(QStringList() << "c" << "config", "Path to config file", "config");
parser.setApplicationDescription("Http web server");
parser.addHelpOption();
parser.addOption(configOption);
parser.process(a);
configPath = (parser.isSet(configOption)) ? parser.value(configOption) : "settings.ini";
}
int main(int argc, char *argv[])
{
QString configPath;
QCoreApplication a(argc, argv);
parseCommandLineArguments(a, configPath);
QSettings *settings = getSettings(configPath);
HttpServer httpServer(settings);
if (!httpServer.start()) {
std::cout << "Server start failed" << std::endl;
}
return a.exec();
}