59 {
61
63 using Request = LegacyWebApp::Request;
64 using Response = LegacyWebApp::Response;
66
67 const LegacyWebApp legacyApp("legacy");
68
70
71 legacyApp.get("/ws", [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
72 const std::string connectionName = res->getSocketContext()->getSocketConnection()->getConnectionName();
73
74 res->upgrade(req, [req, res, connectionName](const std::string& name) {
75 if (!name.empty()) {
76 VLOG(1) << connectionName << ": Successful upgrade:";
77 VLOG(1) << connectionName << ": Requested: " << req->get("upgrade");
78 VLOG(1) << connectionName << ": Selected: " << name;
79
80 res->end();
81 } else {
82 VLOG(1) << connectionName << ": Can not upgrade to any of '" << req->get("upgrade") << "'";
83
84 res->sendStatus(404);
85 }
86 });
87 });
88
90 VLOG(1) << "HTTP GET on "
91 << "/";
92 if (req->url == "/" || req->url == "/index.html") {
93 req->url = "/wstest.html";
94 }
95
96 VLOG(1) << CMAKE_CURRENT_SOURCE_DIR "/html" + req->url;
97 res->sendFile(CMAKE_CURRENT_SOURCE_DIR "/html" + req->url, [req](int errnum) {
98 if (errnum == 0) {
99 VLOG(1) << req->url;
100 } else {
101 VLOG(1) << "HTTP response send file failed: " << std::strerror(errnum);
102 }
103 });
104 });
105
106 legacyApp.listen(
108 switch (state) {
110 VLOG(1) << instanceName << " listening on '" << socketAddress.toString() << "'";
111 break;
113 VLOG(1) << instanceName << " disabled";
114 break;
116 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
117 break;
119 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
120 break;
121 }
122 });
123
124 VLOG(1) << "Legacy Routes:";
125 for (std::string& route : legacyApp.getRoutes()) {
126 route.erase(std::remove(route.begin(), route.end(), '$'), route.end());
127
128 VLOG(1) << " " << route;
129 }
130
131 {
133 using Request = TlsWebApp::Request;
134 using Response = TlsWebApp::Response;
136
137 const TlsWebApp tlsApp("tls");
138
140
141 tlsApp.get("/ws", [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
142 const std::string connectionName = res->getSocketContext()->getSocketConnection()->getConnectionName();
143
144 res->upgrade(req, [req, res, connectionName](const std::string& name) {
145 if (!name.empty()) {
146 VLOG(1) << connectionName << ": Upgrade success:";
147 VLOG(1) << connectionName << ": Requested: " << req->get("upgrade");
148 VLOG(1) << connectionName << ": Selected: " << name;
149
150 res->end();
151 } else {
152 VLOG(1) << connectionName << ": Can not upgrade to any of '" << req->get("upgrade") << "'";
153
154 res->sendStatus(404);
155 }
156 });
157 });
158
160 if (req->url == "/" || req->url == "/index.html") {
161 req->url = "/wstest.html";
162 }
163
164 VLOG(1) << CMAKE_CURRENT_SOURCE_DIR "/html" + req->url;
165 res->sendFile(CMAKE_CURRENT_SOURCE_DIR "/html" + req->url, [req](int ret) {
166 if (ret != 0) {
167 PLOG(ERROR) << req->url;
168 }
169 });
170 });
171
172 tlsApp.listen(
174 switch (state) {
176 VLOG(1) << instanceName << " listening on '" << socketAddress.toString() << "'";
177 break;
179 VLOG(1) << instanceName << " disabled";
180 break;
182 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
183 break;
185 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
186 break;
187 }
188 });
189
190 VLOG(1) << "Tls Routes:";
191 for (std::string& route : legacyApp.getRoutes()) {
192 route.erase(std::remove(route.begin(), route.end(), '$'), route.end());
193
194 VLOG(1) << " " << route;
195 }
196 }
197
199}
#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