-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathConfigLoader.cc
More file actions
719 lines (699 loc) · 24.6 KB
/
ConfigLoader.cc
File metadata and controls
719 lines (699 loc) · 24.6 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/**
*
* @file ConfigLoader.cc
* @author 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
*
*/
#include "ConfigLoader.h"
#include "HttpAppFrameworkImpl.h"
#include <drogon/config.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <thread>
#include <trantor/utils/Logger.h>
#if !defined(_WIN32)
#include <unistd.h>
#define os_access access
#else
#include <io.h>
#ifndef __MINGW32__
#define os_access _waccess
#define R_OK 04
#define W_OK 02
#else
#define os_access access
#endif
#endif
#include <drogon/utils/Utilities.h>
#include "ConfigAdapterManager.h"
#include <filesystem>
using namespace drogon;
static bool bytesSize(std::string &sizeStr, size_t &size)
{
if (sizeStr.empty())
{
size = -1;
return true;
}
else
{
size = 1;
switch (sizeStr[sizeStr.length() - 1])
{
case 'k':
case 'K':
size = 1024;
sizeStr.resize(sizeStr.length() - 1);
break;
case 'M':
case 'm':
size = (1024 * 1024);
sizeStr.resize(sizeStr.length() - 1);
break;
case 'g':
case 'G':
size = (1024 * 1024 * 1024);
sizeStr.resize(sizeStr.length() - 1);
break;
#if ((ULONG_MAX) != (UINT_MAX))
// 64bit system
case 't':
case 'T':
size = (1024L * 1024L * 1024L * 1024L);
sizeStr.resize(sizeStr.length() - 1);
break;
#endif
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '7':
case '8':
case '9':
break;
default:
return false;
break;
}
std::istringstream iss(sizeStr);
size_t tmpSize;
iss >> tmpSize;
if (iss.fail())
{
return false;
}
if ((size_t(-1) / tmpSize) >= size)
size *= tmpSize;
else
{
size = -1;
}
return true;
}
}
ConfigLoader::ConfigLoader(const std::string &configFile)
{
if (os_access(drogon::utils::toNativePath(configFile).c_str(), 0) != 0)
{
throw std::runtime_error("Config file " + configFile + " not found!");
}
if (os_access(drogon::utils::toNativePath(configFile).c_str(), R_OK) != 0)
{
throw std::runtime_error("No permission to read config file " +
configFile);
}
configFile_ = configFile;
auto pos = configFile.find_last_of('.');
if (pos == std::string::npos)
{
throw std::runtime_error("Invalid config file name!");
}
auto ext = configFile.substr(pos + 1);
std::ifstream infile(drogon::utils::toNativePath(configFile).c_str(),
std::ifstream::in);
// get the content of the infile
std::string content((std::istreambuf_iterator<char>(infile)),
std::istreambuf_iterator<char>());
try
{
configJsonRoot_ =
ConfigAdapterManager::instance().getJson(content, std::move(ext));
}
catch (std::exception &e)
{
throw std::runtime_error("Error reading config file " + configFile +
": " + e.what());
}
}
ConfigLoader::ConfigLoader(const Json::Value &data) : configJsonRoot_(data)
{
}
ConfigLoader::ConfigLoader(Json::Value &&data)
: configJsonRoot_(std::move(data))
{
}
ConfigLoader::~ConfigLoader()
{
}
static void loadLogSetting(const Json::Value &log)
{
if (!log)
return;
auto useSpdlog = log.get("use_spdlog", false).asBool();
auto logPath = log.get("log_path", "").asString();
auto baseName = log.get("logfile_base_name", "").asString();
auto logSize = log.get("log_size_limit", 100000000).asUInt64();
auto maxFiles = log.get("max_files", 0).asUInt();
HttpAppFrameworkImpl::instance().setLogPath(
logPath, baseName, logSize, maxFiles, useSpdlog);
auto logLevel = log.get("log_level", "DEBUG").asString();
if (logLevel == "TRACE")
{
trantor::Logger::setLogLevel(trantor::Logger::kTrace);
}
else if (logLevel == "DEBUG")
{
trantor::Logger::setLogLevel(trantor::Logger::kDebug);
}
else if (logLevel == "INFO")
{
trantor::Logger::setLogLevel(trantor::Logger::kInfo);
}
else if (logLevel == "WARN")
{
trantor::Logger::setLogLevel(trantor::Logger::kWarn);
}
auto localTime = log.get("display_local_time", false).asBool();
trantor::Logger::setDisplayLocalTime(localTime);
}
static void loadControllers(const Json::Value &controllers)
{
if (!controllers)
return;
for (auto const &controller : controllers)
{
auto path = controller.get("path", "").asString();
auto ctrlName = controller.get("controller", "").asString();
if (path == "" || ctrlName == "")
continue;
std::vector<internal::HttpConstraint> constraints;
if (!controller["http_methods"].isNull())
{
for (auto const &method : controller["http_methods"])
{
auto strMethod = method.asString();
std::transform(strMethod.begin(),
strMethod.end(),
strMethod.begin(),
[](unsigned char c) { return tolower(c); });
if (strMethod == "get")
{
constraints.push_back(Get);
}
else if (strMethod == "post")
{
constraints.push_back(Post);
}
else if (strMethod == "head") // The branch never work
{
constraints.push_back(Head);
}
else if (strMethod == "put")
{
constraints.push_back(Put);
}
else if (strMethod == "delete")
{
constraints.push_back(Delete);
}
else if (strMethod == "patch")
{
constraints.push_back(Patch);
}
}
}
if (!controller["filters"].isNull())
{
for (auto const &filter : controller["filters"])
{
constraints.push_back(filter.asString());
}
}
drogon::app().registerHttpSimpleController(path, ctrlName, constraints);
}
}
static void loadApp(const Json::Value &app)
{
if (!app)
return;
// threads number
auto threadsNum = app.get("threads_num", 1).asUInt64();
if (threadsNum == 1)
{
threadsNum = app.get("number_of_threads", 1).asUInt64();
}
if (threadsNum == 0)
{
// set the number to the number of processors.
threadsNum = std::thread::hardware_concurrency();
LOG_TRACE << "The number of processors is " << threadsNum;
}
if (threadsNum < 1)
threadsNum = 1;
drogon::app().setThreadNum(threadsNum);
// session
auto enableSession = app.get("enable_session", false).asBool();
if (enableSession)
{
auto timeout = app.get("session_timeout", 0).asUInt64();
auto sameSite = app.get("session_same_site", "Null").asString();
auto cookieKey = app.get("session_cookie_key", "JSESSIONID").asString();
auto maxAge = app.get("session_max_age", -1).asInt();
drogon::app().enableSession(timeout,
Cookie::convertString2SameSite(sameSite),
cookieKey,
maxAge);
}
else
drogon::app().disableSession();
// document root
auto documentRoot = app.get("document_root", "").asString();
if (documentRoot != "")
{
drogon::app().setDocumentRoot(documentRoot);
}
if (!app["static_file_headers"].empty())
{
if (app["static_file_headers"].isArray())
{
std::vector<std::pair<std::string, std::string>> headers;
for (auto &header : app["static_file_headers"])
{
headers.emplace_back(
std::make_pair(header["name"].asString(),
header["value"].asString()));
}
drogon::app().setStaticFileHeaders(headers);
}
else
{
throw std::runtime_error(
"The static_file_headers option must be an array");
}
}
// upload path
auto uploadPath = app.get("upload_path", "uploads").asString();
drogon::app().setUploadPath(uploadPath);
// file types
auto fileTypes = app["file_types"];
if (fileTypes.isArray() && !fileTypes.empty())
{
std::vector<std::string> types;
for (auto const &fileType : fileTypes)
{
types.push_back(fileType.asString());
LOG_TRACE << "file type:" << types.back();
}
drogon::app().setFileTypes(types);
}
// locations
if (app.isMember("locations"))
{
auto &locations = app["locations"];
if (!locations.isArray())
{
throw std::runtime_error("The locations option must be an array");
}
for (auto &location : locations)
{
auto uri = location.get("uri_prefix", "").asString();
if (uri.empty())
continue;
auto defaultContentType =
location.get("default_content_type", "").asString();
auto alias = location.get("alias", "").asString();
auto isCaseSensitive =
location.get("is_case_sensitive", false).asBool();
auto allAll = location.get("allow_all", true).asBool();
auto isRecursive = location.get("is_recursive", true).asBool();
if (!location["filters"].isNull())
{
if (location["filters"].isArray())
{
std::vector<std::string> filters;
for (auto const &filter : location["filters"])
{
filters.push_back(filter.asString());
}
drogon::app().addALocation(uri,
defaultContentType,
alias,
isCaseSensitive,
allAll,
isRecursive,
filters);
}
else
{
throw std::runtime_error("the filters of location '" + uri +
"' should be an array");
}
}
else
{
drogon::app().addALocation(uri,
defaultContentType,
alias,
isCaseSensitive,
allAll,
isRecursive);
}
}
}
// max connections
auto maxConns = app.get("max_connections", 0).asUInt64();
if (maxConns > 0)
{
drogon::app().setMaxConnectionNum(maxConns);
}
// max connections per IP
auto maxConnsPerIP = app.get("max_connections_per_ip", 0).asUInt64();
if (maxConnsPerIP > 0)
{
drogon::app().setMaxConnectionNumPerIP(maxConnsPerIP);
}
#if !defined(_WIN32) && !TARGET_OS_IOS
// dynamic views
auto enableDynamicViews = app.get("load_dynamic_views", false).asBool();
if (enableDynamicViews)
{
auto viewsPaths = app["dynamic_views_path"];
if (viewsPaths.isArray() && viewsPaths.size() > 0)
{
std::vector<std::string> paths;
for (auto const &viewsPath : viewsPaths)
{
paths.push_back(viewsPath.asString());
LOG_TRACE << "views path:" << paths.back();
}
auto outputPath =
app.get("dynamic_views_output_path", "").asString();
drogon::app().enableDynamicViewsLoading(paths, outputPath);
}
}
#endif
auto stackLimit = app.get("json_parser_stack_limit", 1000).asUInt64();
drogon::app().setJsonParserStackLimit(stackLimit);
auto unicodeEscaping =
app.get("enable_unicode_escaping_in_json", true).asBool();
drogon::app().setUnicodeEscapingInJson(unicodeEscaping);
auto &precision = app["float_precision_in_json"];
if (!precision.isNull())
{
auto precisionLength = precision.get("precision", 0).asUInt64();
auto precisionType =
precision.get("precision_type", "significant").asString();
drogon::app().setFloatPrecisionInJson((unsigned int)precisionLength,
precisionType);
}
// log
loadLogSetting(app["log"]);
// run as daemon
auto runAsDaemon = app.get("run_as_daemon", false).asBool();
if (runAsDaemon)
{
drogon::app().enableRunAsDaemon();
}
// handle SIGTERM
auto handleSigterm = app.get("handle_sig_term", true).asBool();
if (!handleSigterm)
{
drogon::app().disableSigtermHandling();
}
// relaunch
auto relaunch = app.get("relaunch_on_error", false).asBool();
if (relaunch)
{
drogon::app().enableRelaunchOnError();
}
auto useSendfile = app.get("use_sendfile", true).asBool();
drogon::app().enableSendfile(useSendfile);
auto useGzip = app.get("use_gzip", true).asBool();
drogon::app().enableGzip(useGzip);
auto useBr = app.get("use_brotli", false).asBool();
drogon::app().enableBrotli(useBr);
auto staticFilesCacheTime = app.get("static_files_cache_time", 5).asInt();
drogon::app().setStaticFilesCacheTime(staticFilesCacheTime);
loadControllers(app["simple_controllers_map"]);
// Kick off idle connections
auto kickOffTimeout = app.get("idle_connection_timeout", 60).asUInt64();
drogon::app().setIdleConnectionTimeout(kickOffTimeout);
auto server = app.get("server_header_field", "").asString();
if (!server.empty())
drogon::app().setServerHeaderField(server);
auto sendServerHeader = app.get("enable_server_header", true).asBool();
drogon::app().enableServerHeader(sendServerHeader);
auto sendDateHeader = app.get("enable_date_header", true).asBool();
drogon::app().enableDateHeader(sendDateHeader);
auto keepaliveReqs = app.get("keepalive_requests", 0).asUInt64();
drogon::app().setKeepaliveRequestsNumber(keepaliveReqs);
auto pipeliningReqs = app.get("pipelining_requests", 0).asUInt64();
drogon::app().setPipeliningRequestsNumber(pipeliningReqs);
auto useGzipStatic = app.get("gzip_static", true).asBool();
drogon::app().setGzipStatic(useGzipStatic);
auto useBrStatic = app.get("br_static", true).asBool();
drogon::app().setBrStatic(useBrStatic);
auto maxBodySize = app.get("client_max_body_size", "1M").asString();
size_t size;
if (bytesSize(maxBodySize, size))
{
drogon::app().setClientMaxBodySize(size);
}
else
{
throw std::runtime_error("Error format of client_max_body_size");
}
auto maxMemoryBodySize =
app.get("client_max_memory_body_size", "64K").asString();
if (bytesSize(maxMemoryBodySize, size))
{
drogon::app().setClientMaxMemoryBodySize(size);
}
else
{
throw std::runtime_error("Error format of client_max_memory_body_size");
}
auto maxWsMsgSize =
app.get("client_max_websocket_message_size", "128K").asString();
if (bytesSize(maxWsMsgSize, size))
{
drogon::app().setClientMaxWebSocketMessageSize(size);
}
else
{
throw std::runtime_error(
"Error format of client_max_websocket_message_size");
}
drogon::app().enableReusePort(app.get("reuse_port", false).asBool());
drogon::app().setHomePage(app.get("home_page", "index.html").asString());
drogon::app().setImplicitPageEnable(
app.get("use_implicit_page", true).asBool());
drogon::app().setImplicitPage(
app.get("implicit_page", "index.html").asString());
auto mimes = app["mime"];
if (!mimes.isNull())
{
auto names = mimes.getMemberNames();
for (const auto &mime : names)
{
auto ext = mimes[mime];
std::vector<std::string> exts;
if (ext.isString())
exts.push_back(ext.asString());
else if (ext.isArray())
{
for (const auto &extension : ext)
exts.push_back(extension.asString());
}
for (const auto &extension : exts)
drogon::app().registerCustomExtensionMime(extension, mime);
}
}
bool enableCompressedRequests =
app.get("enabled_compressed_request", false).asBool();
drogon::app().enableCompressedRequest(enableCompressedRequests);
drogon::app().enableRequestStream(
app.get("enable_request_stream", false).asBool());
}
static void loadDbClients(const Json::Value &dbClients)
{
if (!dbClients)
return;
for (auto const &client : dbClients)
{
auto type = client.get("rdbms", "postgresql").asString();
std::transform(type.begin(),
type.end(),
type.begin(),
[](unsigned char c) { return tolower(c); });
auto host = client.get("host", "127.0.0.1").asString();
unsigned short port = client.get("port", 5432).asUInt();
auto dbname = client.get("dbname", "").asString();
if (dbname.empty() && type != "sqlite3")
{
throw std::runtime_error(
"Please configure dbname in the configuration file");
}
auto user = client.get("user", "postgres").asString();
auto password = client.get("passwd", "").asString();
if (password.empty())
{
password = client.get("password", "").asString();
}
auto connNum = client.get("connection_number", 1).asUInt();
if (connNum == 1)
{
connNum = client.get("number_of_connections", 1).asUInt();
}
auto name = client.get("name", "default").asString();
auto filename = client.get("filename", "").asString();
auto isFast = client.get("is_fast", false).asBool();
auto characterSet = client.get("characterSet", "").asString();
if (characterSet.empty())
{
characterSet = client.get("client_encoding", "").asString();
}
auto connectOptions = client.get("connect_options", Json::Value());
auto timeout = client.get("timeout", -1.0).asDouble();
auto autoBatch = client.get("auto_batch", false).asBool();
std::unordered_map<std::string, std::string> options;
if (connectOptions.isObject() && !connectOptions.empty())
{
for (const auto &key : connectOptions.getMemberNames())
{
options[key] = connectOptions[key].asString();
}
}
HttpAppFrameworkImpl::instance().addDbClient(type,
host,
port,
dbname,
user,
password,
connNum,
filename,
name,
isFast,
characterSet,
timeout,
autoBatch,
std::move(options));
}
}
static void loadRedisClients(const Json::Value &redisClients)
{
if (!redisClients)
return;
for (auto const &client : redisClients)
{
std::promise<std::string> promise;
auto future = promise.get_future();
auto host = client.get("host", "127.0.0.1").asString();
trantor::Resolver::newResolver()->resolve(
host, [&promise](const trantor::InetAddress &address) {
promise.set_value(address.toIp());
});
auto port = client.get("port", 6379).asUInt();
auto username = client.get("username", "").asString();
auto password = client.get("passwd", "").asString();
if (password.empty())
{
password = client.get("password", "").asString();
}
auto connNum = client.get("connection_number", 1).asUInt();
if (connNum == 1)
{
connNum = client.get("number_of_connections", 1).asUInt();
}
auto name = client.get("name", "default").asString();
auto isFast = client.get("is_fast", false).asBool();
auto timeout = client.get("timeout", -1.0).asDouble();
auto db = client.get("db", 0).asUInt();
auto hostIp = future.get();
drogon::app().createRedisClient(hostIp,
port,
name,
password,
connNum,
isFast,
timeout,
db,
username);
}
}
static void loadListeners(const Json::Value &listeners)
{
if (!listeners)
return;
LOG_TRACE << "Has " << listeners.size() << " listeners";
for (auto const &listener : listeners)
{
#ifndef _WIN32
// Check for Unix domain socket listener
auto unixSocketPath = listener.get("unix_socket", "").asString();
if (!unixSocketPath.empty())
{
LOG_TRACE << "Add Unix domain socket listener: " << unixSocketPath;
drogon::app().addListener(unixSocketPath);
continue;
}
#endif
auto addr = listener.get("address", "0.0.0.0").asString();
auto port = (uint16_t)listener.get("port", 0).asUInt();
auto useSSL = listener.get("https", false).asBool();
auto cert = listener.get("cert", "").asString();
auto key = listener.get("key", "").asString();
auto useOldTLS = listener.get("use_old_tls", false).asBool();
std::vector<std::pair<std::string, std::string>> sslConfCmds;
if (listener.isMember("ssl_conf"))
{
for (const auto &opt : listener["ssl_conf"])
{
if (opt.size() == 0 || opt.size() > 2)
{
LOG_FATAL << "SSL configuration option should be an 1 or "
"2-element array";
abort();
}
sslConfCmds.emplace_back(opt[0].asString(),
opt.get(1, "").asString());
}
}
LOG_TRACE << "Add listener:" << addr << ":" << port;
drogon::app().addListener(
addr, port, useSSL, cert, key, useOldTLS, sslConfCmds);
}
}
static void loadSSL(const Json::Value &sslConf)
{
if (!sslConf)
return;
auto key = sslConf.get("key", "").asString();
auto cert = sslConf.get("cert", "").asString();
drogon::app().setSSLFiles(cert, key);
std::vector<std::pair<std::string, std::string>> sslConfCmds;
if (sslConf.isMember("conf"))
{
for (const auto &opt : sslConf["conf"])
{
if (opt.size() == 0 || opt.size() > 2)
{
LOG_FATAL << "SSL configuration option should be an 1 or "
"2-element array";
abort();
}
sslConfCmds.emplace_back(opt[0].asString(),
opt.get(1, "").asString());
}
}
drogon::app().setSSLConfigCommands(sslConfCmds);
}
void ConfigLoader::load()
{
// std::cout<<configJsonRoot_<<std::endl;
loadApp(configJsonRoot_["app"]);
loadSSL(configJsonRoot_["ssl"]);
loadListeners(configJsonRoot_["listeners"]);
loadDbClients(configJsonRoot_["db_clients"]);
loadRedisClients(configJsonRoot_["redis_clients"]);
}