60 {
62
64 using Request = LegacyWebApp::Request;
65 using Response = LegacyWebApp::Response;
67
68 const LegacyWebApp legacyApp("legacy");
69
71
72 legacyApp.get("/ws", [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
73 const std::string connectionName = res->getSocketContext()->getSocketConnection()->getConnectionName();
74
75 res->upgrade(req, [req, res, connectionName](const std::string& name) {
76 if (!name.empty()) {
77 VLOG(1) << connectionName << ": Successful upgrade:";
78 VLOG(1) << connectionName << ": Requested: " << req->get("upgrade");
79 VLOG(1) << connectionName << ": Selected: " << name;
80
81 res->end();
82 } else {
83 VLOG(1) << connectionName << ": Can not upgrade to any of '" << req->get("upgrade") << "'";
84
85 res->sendStatus(404);
86 }
87 });
88 });
89
91 VLOG(1) << "HTTP GET on "
92 << "/";
93 if (req->url == "/" || req->url == "/index.html") {
94 req->url = "/wstest.html";
95 }
96
97 VLOG(1) << CMAKE_CURRENT_SOURCE_DIR "/html" + req->url;
98 res->sendFile(CMAKE_CURRENT_SOURCE_DIR "/html" + req->url, [req, res](int errnum) {
99 if (errnum == 0) {
100 VLOG(1) << req->url;
101 } else {
102 VLOG(1) << "HTTP response send file failed: " << std::strerror(errnum);
103 res->sendStatus(404);
104 }
105 });
106 });
107
108 legacyApp
110 VLOG(0) << "------------------- Legacy Server Init: " << acceptEventReceiver;
111 if (acceptEventReceiver->isEnabled()) {
112 acceptEventReceiver->stopListen();
113 }
114 })
117 switch (state) {
119 VLOG(1) << instanceName << " listening on '" << socketAddress.toString() << "'";
120 break;
122 VLOG(1) << instanceName << " disabled";
123 break;
125 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
126 break;
128 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
129 break;
130 }
131 });
132
133 VLOG(1) << "Legacy Routes:";
134 for (std::string& route : legacyApp.getRoutes()) {
135 route.erase(std::remove(route.begin(), route.end(), '$'), route.end());
136
137 VLOG(1) << " " << route;
138 }
139
140 {
142 using Request = TlsWebApp::Request;
143 using Response = TlsWebApp::Response;
145
146 const TlsWebApp tlsApp("tls");
147
149
150 tlsApp.get("/ws", [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
151 const std::string connectionName = res->getSocketContext()->getSocketConnection()->getConnectionName();
152
153 res->upgrade(req, [req, res, connectionName](const std::string& name) {
154 if (!name.empty()) {
155 VLOG(1) << connectionName << ": Upgrade success:";
156 VLOG(1) << connectionName << ": Requested: " << req->get("upgrade");
157 VLOG(1) << connectionName << ": Selected: " << name;
158
159 res->end();
160 } else {
161 VLOG(1) << connectionName << ": Can not upgrade to any of '" << req->get("upgrade") << "'";
162
163 res->sendStatus(404);
164 }
165 });
166 });
167
169 if (req->url == "/" || req->url == "/index.html") {
170 req->url = "/wstest.html";
171 }
172
173 VLOG(1) << CMAKE_CURRENT_SOURCE_DIR "/html" + req->url;
174 res->sendFile(CMAKE_CURRENT_SOURCE_DIR "/html" + req->url, [req, res](int errnum) {
175 if (errnum == 0) {
176 VLOG(1) << req->url;
177 } else {
178 VLOG(1) << "HTTP response send file failed: " << std::strerror(errnum);
179 res->sendStatus(404);
180 }
181 });
182 });
183
184 tlsApp
186 VLOG(0) << "------------------- TLS Server Init: " << acceptEventReceiver;
187 })
188 .
listen([instanceName = tlsApp.getConfig().getInstanceName()](
const SocketAddress& socketAddress,
190 switch (state) {
192 VLOG(1) << instanceName << " listening on '" << socketAddress.toString() << "'";
193 break;
195 VLOG(1) << instanceName << " disabled";
196 break;
198 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
199 break;
201 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
202 break;
203 }
204 });
205
206 VLOG(1) << "Tls Routes:";
207 for (std::string& route : legacyApp.getRoutes()) {
208 route.erase(std::remove(route.begin(), route.end(), '$'), route.end());
209
210 VLOG(1) << " " << route;
211 }
212 }
213
215}
#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