-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyhttp-server.cpp
177 lines (145 loc) · 4.16 KB
/
myhttp-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
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
#include "project.h"
#include "timestamp.h"
static const char kVersionInfo[] =
"MyHttp Server App\n"
"=================\n"
"A simple HTTP server based on the MyHttp library.\n"
"\n"
"Built: " MyCpp_TIMESTAMP "\n"
"Version: 1.0\n"
"Copyright (C) 2024-2025 Yuhao Gu. All Rights Reserved.";
#include <My/err.hpp>
#include <My/log.hpp>
#include <My/util.hpp>
using namespace My::util;
// ========================================================================== //
// 日志配置
// ========================================================================== //
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/console.hpp>
namespace {
int gLogLevel = My::log::Level::noti;
void
init_log()
{
boost::log::add_common_attributes();
boost::log::add_console_log(std::clog,
boost::log::keywords::format = &My::log::format);
boost::log::core::get()->set_filter(
[](const boost::log::attribute_value_set& attrs) {
return attrs["Severity"].extract<My::log::Level>() >= gLogLevel;
});
}
} // namespace
// ========================================================================== //
// 服务器
// ========================================================================== //
#include <MyHttp/ServerBuilder.hpp>
#include <thread>
namespace {
int gThreads = std::thread::hardware_concurrency();
std::string gManifest;
MyHttp::util::ThreadsExecutor* gExecutor;
MyHttp::ServerBuilder::Servers gServers;
void
stop_servers(int sig)
{
MyHttp::ServerBuilder::stop_all(gServers);
// gExecutor->stop();
}
void
build_and_run_servers()
{
MyHttp::util::ThreadsExecutor ex(gThreads);
gExecutor = &ex;
ex.start();
MyHttp::ServerBuilder sb(ex);
sb.register_builtins();
gServers = sb.build_json_file(gManifest);
MyHttp::ServerBuilder::start_all(gServers);
signal(SIGINT, &stop_servers);
signal(SIGTERM, &stop_servers);
ex.wait();
}
} // namespace
// ========================================================================== //
// 配置示例
// ========================================================================== //
namespace {
const char kExampleManifest[] = R"(
{
"hello-world": {
"Type": "HttpHelloWorld",
"Host": "0.0.0.0",
"Port": 8001,
"Backlog": 128,
"Details": {
"BufferLimit": 8192,
"KeepAliveTimeout": 3,
"KeepAliveMax": 1,
},
},
"matpowsum": {
"Type": "HttpMatpowsum",
"Host": "127.0.0.1",
"Port": 8002,
"Backlog": 4096,
"Details": {
"BufferLimit": 8192,
"KeepAliveTimeout": 3,
"KeepAliveMax": null,
},
},
}
)";
} // namespace
// ========================================================================== //
// 主函数
// ========================================================================== //
#include "po.hpp"
int
main(int argc, char* argv[])
try {
po::options_description od("Options");
od.add_options() //
("version,v", "print version info") //
("help,h", "print help info") //
("log,l", povd(gLogLevel), "log level") //
("threads,t", povd(gThreads), "number of threads") //
("manifest-example", "print example of service manifest file") //
("manifest", povd(gManifest), "path to service manifest file") //
;
po::positional_options_description pod;
pod.add("manifest", 1);
po::variables_map vmap;
po::store(
po::command_line_parser(argc, argv).options(od).positional(pod).run(),
vmap);
if (vmap.count("help") || argc == 1) {
std::cout << od << std::endl;
return 0;
}
if (vmap.count("version")) {
std::cout << kVersionInfo << std::endl;
return 0;
}
if (vmap.count("manifest-example")) {
std::cout << kExampleManifest << std::endl;
return 0;
}
po::notify(vmap);
init_log();
build_and_run_servers();
}
catch (My::Err& e) {
std::cout << e.what() << ": " << e.info() << std::endl;
return -3;
}
catch (std::exception& e) {
std::cout << "Exception: " << e.what() << std::endl;
return -2;
}
catch (...) {
std::cout << "UNKNOWN EXCEPTION" << std::endl;
return -1;
}