58 {
60
62 using Request = LegacyWebApp::Request;
63 using Response = LegacyWebApp::Response;
65
66 const LegacyWebApp legacyApp("legacy");
67
69
70 legacyApp.get("/ws", [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
71 const std::string connectionName = res->getSocketContext()->getSocketConnection()->getConnectionName();
72
73 res->upgrade(req, [req, res, connectionName](const std::string& name) {
74 if (!name.empty()) {
75 VLOG(1) << connectionName <<
": Successful upgrade:";
76 VLOG(1) << connectionName <<
": Requested: " << req->get(
"upgrade");
77 VLOG(1) << connectionName <<
": Selected: " << name;
78
79 res->end();
80 } else {
81 VLOG(1) << connectionName <<
": Can not upgrade to any of '" << req->get(
"upgrade") <<
"'";
82
83 res->sendStatus(404);
84 }
85 });
86 });
87
89 VLOG(1) <<
"HTTP GET on "
90 << "/";
91 if (req->url == "/" || req->url == "/index.html") {
92 req->url = "/wstest.html";
93 }
94
95 VLOG(1) << CMAKE_CURRENT_SOURCE_DIR
"/html" + req->url;
96 res->sendFile(CMAKE_CURRENT_SOURCE_DIR "/html" + req->url, [req, res](int errnum) {
97 if (errnum == 0) {
98 VLOG(1) << req->url;
99 } else {
100 VLOG(1) << "HTTP response send file failed: " << std::strerror(errnum);
101 res->sendStatus(404);
102 }
103 });
104 });
105
106 legacyApp
108 VLOG(0) <<
"------------------- Legacy Server Init: " << acceptEventReceiver
109 << " - enabled: " << acceptEventReceiver->isEnabled();
110 if (acceptEventReceiver->isEnabled()) {
111 acceptEventReceiver->stopListen();
112 }
113 })
114 .
listen([instanceName = legacyApp.getConfig()->getInstanceName()](
const SocketAddress& socketAddress,
116 switch (state) {
118 VLOG(1) << instanceName <<
" listening on '" << socketAddress.toString() <<
"'";
119 break;
121 VLOG(1) << instanceName <<
" disabled";
122 break;
124 VLOG(1) << instanceName <<
" " << socketAddress.toString() <<
": " << state.what();
125 break;
127 VLOG(1) << instanceName <<
" " << socketAddress.toString() <<
": " << state.what();
128 break;
129 }
130 });
131
132 VLOG(1) <<
"Legacy Routes:";
133 for (std::string& route : legacyApp.getRoutes()) {
134 route.erase(std::remove(route.begin(), route.end(), '$'), route.end());
135
136 VLOG(1) <<
" " << route;
137 }
138
139 {
141 using Request = TlsWebApp::Request;
142 using Response = TlsWebApp::Response;
144
145 const TlsWebApp tlsApp("tls");
146
148
149 tlsApp.get("/ws", [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
150 const std::string connectionName = res->getSocketContext()->getSocketConnection()->getConnectionName();
151
152 res->upgrade(req, [req, res, connectionName](const std::string& name) {
153 if (!name.empty()) {
154 VLOG(1) << connectionName <<
": Upgrade success:";
155 VLOG(1) << connectionName <<
": Requested: " << req->get(
"upgrade");
156 VLOG(1) << connectionName <<
": Selected: " << name;
157
158 res->end();
159 } else {
160 VLOG(1) << connectionName <<
": Can not upgrade to any of '" << req->get(
"upgrade") <<
"'";
161
162 res->sendStatus(404);
163 }
164 });
165 });
166
168 if (req->url == "/" || req->url == "/index.html") {
169 req->url = "/wstest.html";
170 }
171
172 VLOG(1) << CMAKE_CURRENT_SOURCE_DIR
"/html" + req->url;
173 res->sendFile(CMAKE_CURRENT_SOURCE_DIR "/html" + req->url, [req, res](int errnum) {
174 if (errnum == 0) {
175 VLOG(1) << req->url;
176 } else {
177 VLOG(1) << "HTTP response send file failed: " << std::strerror(errnum);
178 res->sendStatus(404);
179 }
180 });
181 });
182
183 tlsApp
185 VLOG(0) <<
"------------------- TLS Server Init: " << acceptEventReceiver;
186 })
187 .
listen([instanceName = tlsApp.getConfig()->getInstanceName()](
const SocketAddress& socketAddress,
189 switch (state) {
191 VLOG(1) << instanceName <<
" listening on '" << socketAddress.toString() <<
"'";
192 break;
194 VLOG(1) << instanceName <<
" disabled";
195 break;
197 VLOG(1) << instanceName <<
" " << socketAddress.toString() <<
": " << state.what();
198 break;
200 VLOG(1) << instanceName <<
" " << socketAddress.toString() <<
": " << state.what();
201 break;
202 }
203 });
204
205 VLOG(1) <<
"Tls Routes:";
206 for (std::string& route : legacyApp.getRoutes()) {
207 route.erase(std::remove(route.begin(), route.end(), '$'), route.end());
208
209 VLOG(1) <<
" " << route;
210 }
211 }
212
214}
#define APPLICATION(req, res)
static constexpr int DISABLED
static constexpr int ERROR
static constexpr int FATAL
static void init(int argc, char *argv[])
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
int listen(int sockfd, int backlog)
WebAppT< web::http::legacy::in::Server > WebApp
WebAppT< web::http::tls::in::Server > WebApp
SocketClient::SocketAddress SocketAddress