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
107 .listen([instanceName = legacyApp.getConfig()->getInstanceName()](
const SocketAddress& socketAddress,
109 switch (state) {
111 VLOG(1) << instanceName <<
" listening on '" << socketAddress.toString() <<
"'";
112 break;
114 VLOG(1) << instanceName <<
" disabled";
115 break;
117 VLOG(1) << instanceName <<
" " << socketAddress.toString() <<
": " << state.what();
118 break;
120 VLOG(1) << instanceName <<
" " << socketAddress.toString() <<
": " << state.what();
121 break;
122 }
123 })
124 .getFlowController();
125
126 VLOG(1) <<
"Legacy Routes:";
127 for (std::string& route : legacyApp.getRoutes()) {
128 route.erase(std::remove(route.begin(), route.end(), '$'), route.end());
129
130 VLOG(1) <<
" " << route;
131 }
132
133 {
135 using Request = TlsWebApp::Request;
136 using Response = TlsWebApp::Response;
138
139 const TlsWebApp tlsApp("tls");
140
142
143 tlsApp.get("/ws", [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
144 const std::string connectionName = res->getSocketContext()->getSocketConnection()->getConnectionName();
145
146 res->upgrade(req, [req, res, connectionName](const std::string& name) {
147 if (!name.empty()) {
148 VLOG(1) << connectionName <<
": Upgrade success:";
149 VLOG(1) << connectionName <<
": Requested: " << req->get(
"upgrade");
150 VLOG(1) << connectionName <<
": Selected: " << name;
151
152 res->end();
153 } else {
154 VLOG(1) << connectionName <<
": Can not upgrade to any of '" << req->get(
"upgrade") <<
"'";
155
156 res->sendStatus(404);
157 }
158 });
159 });
160
162 if (req->url == "/" || req->url == "/index.html") {
163 req->url = "/wstest.html";
164 }
165
166 VLOG(1) << CMAKE_CURRENT_SOURCE_DIR
"/html" + req->url;
167 res->sendFile(CMAKE_CURRENT_SOURCE_DIR "/html" + req->url, [req, res](int errnum) {
168 if (errnum == 0) {
169 VLOG(1) << req->url;
170 } else {
171 VLOG(1) << "HTTP response send file failed: " << std::strerror(errnum);
172 res->sendStatus(404);
173 }
174 });
175 });
176
177 tlsApp
178 .listen([instanceName = tlsApp.getConfig()->getInstanceName()](
const SocketAddress& socketAddress,
180 switch (state) {
182 VLOG(1) << instanceName <<
" listening on '" << socketAddress.toString() <<
"'";
183 break;
185 VLOG(1) << instanceName <<
" disabled";
186 break;
188 VLOG(1) << instanceName <<
" " << socketAddress.toString() <<
": " << state.what();
189 break;
191 VLOG(1) << instanceName <<
" " << socketAddress.toString() <<
": " << state.what();
192 break;
193 }
194 })
195 .getFlowController();
196
197 VLOG(1) <<
"Tls Routes:";
198 for (std::string& route : legacyApp.getRoutes()) {
199 route.erase(std::remove(route.begin(), route.end(), '$'), route.end());
200
201 VLOG(1) <<
" " << route;
202 }
203 }
204
206}
#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})
WebAppT< web::http::legacy::in::Server > WebApp
WebAppT< web::http::tls::in::Server > WebApp
SocketClient::SocketAddress SocketAddress