2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
42#include "express/legacy/in/WebApp.h"
43#include "express/middleware/VerboseRequest.h"
44#include "express/tls/in/WebApp.h"
46#ifndef DOXYGEN_SHOULD_SKIP_THIS
48#include "log/Logger.h"
57using namespace express;
59int main(
int argc,
char* argv[]) {
62 using LegacyWebApp = express::legacy::in::WebApp;
63 using Request = LegacyWebApp::Request;
64 using Response = LegacyWebApp::Response;
65 using SocketAddress = LegacyWebApp::SocketAddress;
67 const LegacyWebApp legacyApp
("legacy");
69 legacyApp.use(express::middleware::VerboseRequest());
71 legacyApp.get(
"/ws", [](
const std::shared_ptr<Request>& req,
const std::shared_ptr<Response>& res) {
74 res->
upgrade(req
, [req, res, connectionName](
const std::string& name) {
76 VLOG(1) << connectionName <<
": Successful upgrade:";
77 VLOG(1) << connectionName <<
": Requested: " << req->
get("upgrade");
78 VLOG(1) << connectionName <<
": Selected: " << name;
82 VLOG(1) << connectionName <<
": Can not upgrade to any of '" << req->
get("upgrade") <<
"'";
90 VLOG(1) <<
"HTTP GET on "
92 if (req->
url ==
"/" || req->
url ==
"/index.html") {
93 req->
url =
"/wstest.html";
96 VLOG(1) << CMAKE_CURRENT_SOURCE_DIR
"/html" + req->url;
97 res->sendFile(CMAKE_CURRENT_SOURCE_DIR
"/html" + req->url, [req, res](
int errnum) {
101 VLOG(1) <<
"HTTP response send file failed: " << std::strerror(errnum);
102 res->sendStatus(404);
111 VLOG(1) << instanceName <<
" listening on '" << socketAddress
.toString() <<
"'";
114 VLOG(1) << instanceName <<
" disabled";
125 VLOG(1) <<
"Legacy Routes:";
127 route.erase(std::remove(route.begin(), route.end(),
'$'), route.end());
129 VLOG(1) <<
" " << route;
133 using TlsWebApp = express::tls::
in::WebApp;
134 using Request = TlsWebApp::Request;
135 using Response = TlsWebApp::Response;
136 using SocketAddress = TlsWebApp::SocketAddress;
138 const TlsWebApp tlsApp
("tls");
140 tlsApp.use(express::middleware::VerboseRequest());
142 tlsApp.get(
"/ws", [](
const std::shared_ptr<Request>& req,
const std::shared_ptr<Response>& res) {
145 res->
upgrade(req
, [req, res, connectionName](
const std::string& name) {
147 VLOG(1) << connectionName <<
": Upgrade success:";
148 VLOG(1) << connectionName <<
": Requested: " << req->
get("upgrade");
149 VLOG(1) << connectionName <<
": Selected: " << name;
153 VLOG(1) << connectionName <<
": Can not upgrade to any of '" << req->
get("upgrade") <<
"'";
161 if (req->
url ==
"/" || req->
url ==
"/index.html") {
162 req->
url =
"/wstest.html";
165 VLOG(1) << CMAKE_CURRENT_SOURCE_DIR
"/html" + req->url;
166 res->sendFile(CMAKE_CURRENT_SOURCE_DIR
"/html" + req->url, [req, res](
int errnum) {
170 VLOG(1) <<
"HTTP response send file failed: " << std::strerror(errnum);
171 res->sendStatus(404);
180 VLOG(1) << instanceName <<
" listening on '" << socketAddress
.toString() <<
"'";
183 VLOG(1) << instanceName <<
" disabled";
194 VLOG(1) <<
"Tls Routes:";
196 route.erase(std::remove(route.begin(), route.end(),
'$'), route.end());
198 VLOG(1) <<
" " << route;
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#define APPLICATION(req, res)
Config & getConfig() const
static constexpr int DISABLED
static constexpr int ERROR
static constexpr int FATAL
const std::string & getConnectionName() const
SocketConnection * getSocketConnection() const
void listen(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
const std::string & get(const std::string &key, int i=0) const
void sendStatus(int state, const std::string &html={})
web::http::server::SocketContext * getSocketContext() const
void upgrade(const std::shared_ptr< Request > &request, const std::function< void(const std::string)> &status)
std::list< std::string > getRoutes() const
WebAppT(const std::string &name)
static void init(int argc, char *argv[])
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
const std::string & getInstanceName() const
std::string toString(bool expanded=true) const override
int main(int argc, char *argv[])