SNode.C
Loading...
Searching...
No Matches
vhostserver.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#include "express/legacy/in6/WebApp.h"
43#include "express/middleware/StaticMiddleware.h"
44#include "express/middleware/VHost.h"
45#include "express/tls/in6/WebApp.h"
46#include "utils/Config.h"
47
48#ifndef DOXYGEN_SHOULD_SKIP_THIS
49
50#include "log/Logger.h"
51
52#include <string>
53
54#endif /* DOXYGEN_SHOULD_SKIP_THIS */
55
56using namespace express;
57
58Router getRouter(const std::string& webRoot) {
59 const Router router;
60
61 router.use(middleware::StaticMiddleware(webRoot));
62
63 return router;
64}
65
66int main(int argc, char* argv[]) {
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");
75 vh1.use(middleware::StaticMiddleware(utils::Config::getStringOptionValue("--web-root")));
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");
144 vh1.use(middleware::StaticMiddleware(utils::Config::getStringOptionValue("--web-root")));
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");
210 "/home/voc/projects/snodec/snode.c/certs/Volker_Christian_-_Web_-_snode.c_-_server.key.encrypted.pem");
212 }
213
215}
#define APPLICATION(req, res)
Definition Router.h:68
#define MIDDLEWARE(req, res, next)
Definition Router.h:63
Config & getConfig() const
Definition Socket.hpp:60
static constexpr int DISABLED
Definition State.h:56
static constexpr int ERROR
Definition State.h:57
std::string what() const
Definition State.cpp:114
static constexpr int FATAL
Definition State.h:58
static constexpr int OK
Definition State.h:55
void operator()(const std::string &how="") const
Definition Next.cpp:56
const std::string & query(const std::string &key) const
Definition Request.cpp:107
void send(const std::string &chunk)
Definition Response.cpp:165
Response & status(int status)
Definition Response.cpp:113
WebAppT(const std::string &name)
Definition WebAppT.h:76
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
const std::string & getInstanceName() const
ConfigTls & setCert(const std::string &cert)
ConfigTls & setCertKey(const std::string &certKey)
ConfigTls & setCertKeyPassword(const std::string &certKeyPassword)
std::string toString(bool expanded=true) const override
void listen(uint16_t port, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
int main(int argc, char *argv[])
Router getRouter(const std::string &webRoot)