SNode.C
Loading...
Searching...
No Matches
echoserver.cpp File Reference
Include dependency graph for echoserver.cpp:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 59 of file echoserver.cpp.

59 {
60 express::WebApp::init(argc, argv);
61
62 using LegacyWebApp = express::legacy::in::WebApp;
63 using Request = LegacyWebApp::Request;
64 using Response = LegacyWebApp::Response;
65 using SocketAddress = LegacyWebApp::SocketAddress;
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
89 legacyApp.get("/", [] APPLICATION(req, res) {
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, res](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 res->sendStatus(404);
103 }
104 });
105 });
106
107 legacyApp.listen(
108 [instanceName = legacyApp.getConfig().getInstanceName()](const SocketAddress& socketAddress, const core::socket::State& state) {
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
125 VLOG(1) << "Legacy Routes:";
126 for (std::string& route : legacyApp.getRoutes()) {
127 route.erase(std::remove(route.begin(), route.end(), '$'), route.end());
128
129 VLOG(1) << " " << route;
130 }
131
132 {
133 using TlsWebApp = express::tls::in::WebApp;
134 using Request = TlsWebApp::Request;
135 using Response = TlsWebApp::Response;
136 using SocketAddress = TlsWebApp::SocketAddress;
137
138 const TlsWebApp tlsApp("tls");
139
141
142 tlsApp.get("/ws", [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
143 const std::string connectionName = res->getSocketContext()->getSocketConnection()->getConnectionName();
144
145 res->upgrade(req, [req, res, connectionName](const std::string& name) {
146 if (!name.empty()) {
147 VLOG(1) << connectionName << ": Upgrade success:";
148 VLOG(1) << connectionName << ": Requested: " << req->get("upgrade");
149 VLOG(1) << connectionName << ": Selected: " << name;
150
151 res->end();
152 } else {
153 VLOG(1) << connectionName << ": Can not upgrade to any of '" << req->get("upgrade") << "'";
154
155 res->sendStatus(404);
156 }
157 });
158 });
159
160 tlsApp.get("/", [] APPLICATION(req, res) {
161 if (req->url == "/" || req->url == "/index.html") {
162 req->url = "/wstest.html";
163 }
164
165 VLOG(1) << CMAKE_CURRENT_SOURCE_DIR "/html" + req->url;
166 res->sendFile(CMAKE_CURRENT_SOURCE_DIR "/html" + req->url, [req, res](int errnum) {
167 if (errnum == 0) {
168 VLOG(1) << req->url;
169 } else {
170 VLOG(1) << "HTTP response send file failed: " << std::strerror(errnum);
171 res->sendStatus(404);
172 }
173 });
174 });
175
176 tlsApp.listen(
177 [instanceName = tlsApp.getConfig().getInstanceName()](const SocketAddress& socketAddress, const core::socket::State& state) {
178 switch (state) {
180 VLOG(1) << instanceName << " listening on '" << socketAddress.toString() << "'";
181 break;
183 VLOG(1) << instanceName << " disabled";
184 break;
186 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
187 break;
189 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
190 break;
191 }
192 });
193
194 VLOG(1) << "Tls Routes:";
195 for (std::string& route : legacyApp.getRoutes()) {
196 route.erase(std::remove(route.begin(), route.end(), '$'), route.end());
197
198 VLOG(1) << " " << route;
199 }
200 }
201
202 return express::WebApp::start();
203}
#define APPLICATION(req, res)
Definition Router.h:68
static constexpr int DISABLED
Definition State.h:56
static constexpr int ERROR
Definition State.h:57
static constexpr int FATAL
Definition State.h:58
static constexpr int OK
Definition State.h:55
static void init(int argc, char *argv[])
Definition WebApp.cpp:56
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
Definition WebApp.cpp:60
WebAppT< web::http::legacy::in::Server > WebApp
Definition WebApp.h:56
WebAppT< web::http::tls::in::Server > WebApp
Definition WebApp.h:54
SocketClient::SocketAddress SocketAddress

References core::socket::State::DISABLED, express::Response::end(), core::socket::State::ERROR, core::socket::State::FATAL, express::Request::get(), core::socket::Socket< ConfigT >::getConfig(), core::socket::stream::SocketConnection::getConnectionName(), net::config::ConfigInstance::getInstanceName(), express::Router::getRoutes(), core::socket::stream::SocketContext::getSocketConnection(), express::Response::getSocketContext(), express::WebApp::init(), core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::listen(), core::socket::State::OK, express::Response::sendStatus(), express::WebApp::start(), net::in::SocketAddress::toString(), express::Response::upgrade(), express::Request::url, express::WebAppT< ServerT >::WebAppT(), and core::socket::State::what().

Here is the call graph for this function: