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

Go to the source code of this file.

Functions

Router getRouter (const std::string &webRoot)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ getRouter()

Router getRouter ( const std::string &  webRoot)

Definition at line 58 of file vhostserver.cpp.

58 {
59 const Router router;
60
62
63 return router;
64}
Route & use(const Router &router) const
Definition Router.cpp:88
Router router(database::mariadb::MariaDBClient &db)
Definition testregex.cpp:68

◆ main()

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

Definition at line 66 of file vhostserver.cpp.

66 {
67 utils::Config::addStringOption("--web-root", "Root directory of the web site", "[path]");
68
69 WebApp::init(argc, argv);
70
71 {
72 const legacy::in6::WebApp legacyApp("legacy");
73
74 const Router& vh1 = middleware::VHost("localhost:8080");
76 legacyApp.use(vh1);
77
78 const Router& vh2 = middleware::VHost("atlas.home.vchrist.at:8080");
79 vh2.get("/",
80 [] MIDDLEWARE(req, res, next) {
81 if (req->query("how").empty()) {
82 next();
83 } else if (req->query("how") == "route") {
84 next("route");
85 } else if (req->query("how") == "router") {
86 next("router");
87 }
88 })
89 .get([] APPLICATION(req, res) {
90 res->send("Hello! I am VHOST atlas.home.vchrist.at.");
91 });
92 legacyApp.use(vh2);
93
94 const Router& vh3 = middleware::VHost("ceres.home.vchrist.at:8080");
95 vh3.get("/",
96 [] MIDDLEWARE(req, res, next) {
97 if (req->query("how").empty()) {
98 next();
99 } else if (req->query("how") == "route") {
100 next("route");
101 } else if (req->query("how") == "router") {
102 next("router");
103 }
104 })
105 .get([] APPLICATION(req, res) {
106 res->send("Hello! I am VHOST ceres.home.vchrist.at.");
107 });
108 vh3.get("/", [] APPLICATION(req, res) {
109 res->send("Hello, next route on ceres.home.vchrist.at");
110 });
111 vh3.get("/test", [] APPLICATION(req, res) {
112 res->send("Test Route on VHOST ceres.home.vchrist.at.");
113 });
114 legacyApp.use(vh3);
115
116 legacyApp.use([] APPLICATION(req, res) {
117 res->status(404).send("The requested resource is not found.");
118 });
119
120 legacyApp.listen(8080,
121 [instanceName = legacyApp.getConfig().getInstanceName()](const legacy::in6::WebApp::SocketAddress& socketAddress,
122 const core::socket::State& state) {
123 switch (state) {
124 case core::socket::State::OK:
125 VLOG(1) << instanceName << " listening on '" << socketAddress.toString() << "'";
126 break;
127 case core::socket::State::DISABLED:
128 VLOG(1) << instanceName << " disabled";
129 break;
130 case core::socket::State::ERROR:
131 LOG(ERROR) << instanceName << " " << socketAddress.toString() << ": " << state.what();
132 break;
133 case core::socket::State::FATAL:
134 LOG(FATAL) << instanceName << " " << socketAddress.toString() << ": " << state.what();
135 break;
136 }
137 });
138 }
139
140 {
141 const express::tls::in6::WebApp tlsApp("tls");
142
143 const Router& vh1 = middleware::VHost("localhost:8088");
145 tlsApp.use(vh1);
146
147 const Router& vh2 = middleware::VHost("atlas.home.vchrist.at:8088");
148 vh2.get("/",
149 [] MIDDLEWARE(req, res, next) {
150 if (req->query("how").empty()) {
151 next();
152 } else if (req->query("how") == "route") {
153 next("route");
154 } else if (req->query("how") == "router") {
155 next("router");
156 }
157 })
158 .get([] APPLICATION(req, res) {
159 res->send("Hello! I am VHOST atlas.home.vchrist.at.");
160 });
161 tlsApp.use(vh2);
162
163 const Router& vh3 = middleware::VHost("ceres.home.vchrist.at:8088");
164 vh3.get("/",
165 [] MIDDLEWARE(req, res, next) {
166 if (req->query("how").empty()) {
167 next();
168 } else if (req->query("how") == "route") {
169 next("route");
170 } else if (req->query("how") == "router") {
171 next("router");
172 }
173 })
174 .get([] APPLICATION(req, res) {
175 res->send("Hello! I am VHOST ceres.home.vchrist.at.");
176 });
177 vh3.get("/", [] APPLICATION(req, res) {
178 res->send("Hello, next route on ceres.home.vchrist.at");
179 });
180 vh3.get("/test", [] APPLICATION(req, res) {
181 res->send("Test Route on VHOST ceres.home.vchrist.at.");
182 });
183 tlsApp.use(vh3);
184
185 tlsApp.use([] APPLICATION(req, res) {
186 res->status(404).send("The requested resource is not found.");
187 });
188
189 tlsApp.listen(8088,
190 [instanceName = tlsApp.getConfig().getInstanceName()](const legacy::in6::WebApp::SocketAddress& socketAddress,
191 const core::socket::State& state) {
192 switch (state) {
193 case core::socket::State::OK:
194 VLOG(1) << instanceName << " listening on '" << socketAddress.toString() << "'";
195 break;
196 case core::socket::State::DISABLED:
197 VLOG(1) << instanceName << " disabled";
198 break;
199 case core::socket::State::ERROR:
200 LOG(ERROR) << instanceName << " " << socketAddress.toString() << ": " << state.what();
201 break;
202 case core::socket::State::FATAL:
203 LOG(FATAL) << instanceName << " " << socketAddress.toString() << ": " << state.what();
204 break;
205 }
206 });
207
208 tlsApp.getConfig().setCert("/home/voc/projects/snodec/snode.c/certs/wildcard.home.vchrist.at_-_snode.c_-_server.pem");
209 tlsApp.getConfig().setCertKey(
210 "/home/voc/projects/snodec/snode.c/certs/Volker_Christian_-_Web_-_snode.c_-_server.key.encrypted.pem");
211 tlsApp.getConfig().setCertKeyPassword("snode.c");
212 }
213
215}
#define APPLICATION(req, res)
Definition Router.h:68
#define MIDDLEWARE(req, res, next)
Definition Router.h:63
Route & use(const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &lambda) const
Definition Route.cpp:131
Route & get(const Router &router) const
Definition Router.cpp:90
typename Server::SocketAddress SocketAddress
Definition WebAppT.h:70
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
static CLI::Option * addStringOption(const std::string &name, const std::string &description, const std::string &typeName)
Definition Config.cpp:885
static std::string getStringOptionValue(const std::string &name)
Definition Config.cpp:942
ssize_t send(int sockfd, const void *buf, std::size_t len, int flags)
Definition socket.cpp:96

References core::socket::State::DISABLED, core::socket::State::ERROR, core::socket::State::FATAL, core::socket::Socket< ConfigT >::getConfig(), net::config::ConfigInstance::getInstanceName(), express::WebApp::init(), net::in6::stream::SocketServer< SocketAcceptorT, ConfigSocketServerT, SocketContextFactoryT, Args >::listen(), core::socket::State::OK, express::Next::operator()(), express::Request::query(), express::Response::send(), net::config::ConfigTls::setCert(), net::config::ConfigTls::setCertKey(), net::config::ConfigTls::setCertKeyPassword(), express::WebApp::start(), express::Response::status(), net::in6::SocketAddress::toString(), express::WebAppT< ServerT >::WebAppT(), and core::socket::State::what().

Here is the call graph for this function: