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 60 of file echoserver.cpp.

60 {
61 express::WebApp::init(argc, argv);
62
63 using LegacyWebApp = express::legacy::in::WebApp;
64 using Request = LegacyWebApp::Request;
65 using Response = LegacyWebApp::Response;
66 using SocketAddress = LegacyWebApp::SocketAddress;
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
90 legacyApp.get("/", [] APPLICATION(req, res) {
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
109 .setOnInitState([]([[maybe_unused]] core::eventreceiver::AcceptEventReceiver* acceptEventReceiver) {
110 VLOG(0) << "------------------- Legacy Server Init: " << acceptEventReceiver;
111 if (acceptEventReceiver->isEnabled()) {
112 acceptEventReceiver->stopListen();
113 }
114 })
115 .listen(
116 [instanceName = legacyApp.getConfig().getInstanceName()](const SocketAddress& socketAddress, const core::socket::State& state) {
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 {
141 using TlsWebApp = express::tls::in::WebApp;
142 using Request = TlsWebApp::Request;
143 using Response = TlsWebApp::Response;
144 using SocketAddress = TlsWebApp::SocketAddress;
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
168 tlsApp.get("/", [] APPLICATION(req, res) {
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
185 .setOnInitState([]([[maybe_unused]] core::eventreceiver::AcceptEventReceiver* acceptEventReceiver) {
186 VLOG(0) << "------------------- TLS Server Init: " << acceptEventReceiver;
187 })
188 .listen([instanceName = tlsApp.getConfig().getInstanceName()](const SocketAddress& socketAddress,
189 const core::socket::State& state) {
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
214 return express::WebApp::start();
215}
#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
int listen(int sockfd, int backlog)
Definition socket.cpp:62
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::DescriptorEventReceiver::isEnabled(), core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::listen(), core::socket::State::OK, express::Response::sendFile(), express::Response::sendStatus(), core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::setOnInitState(), express::WebApp::start(), core::eventreceiver::AcceptEventReceiver::stopListen(), 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: