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, 2026
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 "ConfigWWW.h"
43#include "express/legacy/in6/WebApp.h"
44#include "express/middleware/StaticMiddleware.h"
45#include "express/middleware/VHost.h"
46#include "express/tls/in6/WebApp.h"
47
48#ifndef DOXYGEN_SHOULD_SKIP_THIS
49
50#include "log/Logger.h"
51#include "utils/Config.h"
52
53#endif /* DOXYGEN_SHOULD_SKIP_THIS */
54
55using namespace express;
56
57Router getRouter(const std::string& webRoot) {
58 const Router router;
59
60 router.use(middleware::StaticMiddleware(webRoot));
61
62 return router;
63}
64
65int main(int argc, char* argv[]) {
67
68 WebApp::init(argc, argv);
69
70 {
71 const legacy::in6::WebApp legacyApp("legacy");
72
73 const Router& vh1 = middleware::VHost("localhost:8080");
75 legacyApp.use(vh1);
76
77 const Router& vh2 = middleware::VHost("jupiter.home.vchrist.at");
78 vh2.get("/",
79 [] MIDDLEWARE(req, res, next) {
80 if (req->query("how").empty()) {
81 next();
82 } else if (req->query("how") == "route") {
83 next("route");
84 } else if (req->query("how") == "router") {
85 next("router");
86 }
87 })
88 .get([] APPLICATION(req, res) {
89 res->send("Hello! I am VHOST jupiter.home.vchrist.at.");
90 });
91 legacyApp.use(vh2);
92
93 const Router& vh3 = middleware::VHost("atlas.home.vchrist.at:8080");
94 vh3.get("/",
95 [] MIDDLEWARE(req, res, next) {
96 if (req->query("how").empty()) {
97 next();
98 } else if (req->query("how") == "route") {
99 next("route");
100 } else if (req->query("how") == "router") {
101 next("router");
102 }
103 })
104 .get([] APPLICATION(req, res) {
105 res->send("Hello! I am VHOST atlas.home.vchrist.at.");
106 });
107 vh3.get("/", [] APPLICATION(req, res) {
108 res->send("Hello, next route on atlas.home.vchrist.at");
109 });
110 vh3.get("/test", [] APPLICATION(req, res) {
111 res->send("Test Route on VHOST atlas.home.vchrist.at.");
112 });
113 legacyApp.use(vh3);
114
115 legacyApp.use([] APPLICATION(req, res) {
116 res->status(404).send("The requested resource is not found.");
117 });
118
119 legacyApp.listen(8080,
120 [instanceName = legacyApp.getConfig()->getInstanceName()](const legacy::in6::WebApp::SocketAddress& socketAddress,
121 const core::socket::State& state) {
122 switch (state) {
123 case core::socket::State::OK:
124 VLOG(1) << instanceName << " listening on '" << socketAddress.toString() << "'";
125 break;
126 case core::socket::State::DISABLED:
127 VLOG(1) << instanceName << " disabled";
128 break;
129 case core::socket::State::ERROR:
130 LOG(ERROR) << instanceName << " " << socketAddress.toString() << ": " << state.what();
131 break;
132 case core::socket::State::FATAL:
133 LOG(FATAL) << instanceName << " " << socketAddress.toString() << ": " << state.what();
134 break;
135 }
136 });
137 }
138
139 {
140 const express::tls::in6::WebApp tlsApp("tls");
141
142 const Router& vh1 = middleware::VHost("localhost:8088");
144 tlsApp.use(vh1);
145
146 const Router& vh2 = middleware::VHost("atlas.home.vchrist.at:8088");
147 vh2.get("/",
148 [] MIDDLEWARE(req, res, next) {
149 if (req->query("how").empty()) {
150 next();
151 } else if (req->query("how") == "route") {
152 next("route");
153 } else if (req->query("how") == "router") {
154 next("router");
155 }
156 })
157 .get([] APPLICATION(req, res) {
158 res->send("Hello! I am VHOST atlas.home.vchrist.at.");
159 });
160 tlsApp.use(vh2);
161
162 const Router& vh3 = middleware::VHost("ceres.home.vchrist.at:8088");
163 vh3.get("/",
164 [] MIDDLEWARE(req, res, next) {
165 if (req->query("how").empty()) {
166 next();
167 } else if (req->query("how") == "route") {
168 next("route");
169 } else if (req->query("how") == "router") {
170 next("router");
171 }
172 })
173 .get([] APPLICATION(req, res) {
174 res->send("Hello! I am VHOST ceres.home.vchrist.at.");
175 });
176 vh3.get("/", [] APPLICATION(req, res) {
177 res->send("Hello, next route on ceres.home.vchrist.at");
178 });
179 vh3.get("/test", [] APPLICATION(req, res) {
180 res->send("Test Route on VHOST ceres.home.vchrist.at.");
181 });
182 tlsApp.use(vh3);
183
184 tlsApp.use([] APPLICATION(req, res) {
185 res->status(404).send("The requested resource is not found.");
186 });
187
188 tlsApp.listen(8088,
189 [instanceName = tlsApp.getConfig()->getInstanceName()](const legacy::in6::WebApp::SocketAddress& socketAddress,
190 const core::socket::State& state) {
191 switch (state) {
192 case core::socket::State::OK:
193 VLOG(1) << instanceName << " listening on '" << socketAddress.toString() << "'";
194 break;
195 case core::socket::State::DISABLED:
196 VLOG(1) << instanceName << " disabled";
197 break;
198 case core::socket::State::ERROR:
199 LOG(ERROR) << instanceName << " " << socketAddress.toString() << ": " << state.what();
200 break;
201 case core::socket::State::FATAL:
202 LOG(FATAL) << instanceName << " " << socketAddress.toString() << ": " << state.what();
203 break;
204 }
205 });
206
207 tlsApp.getConfig()->setCert("/home/voc/projects/snodec/snode.c/certs/wildcard.home.vchrist.at_-_snode.c_-_server.pem");
209 "/home/voc/projects/snodec/snode.c/certs/Volker_Christian_-_Web_-_snode.c_-_server.key.encrypted.pem");
211 }
212
214}
#define LOG(level)
Definition Logger.h:148
#define VLOG(level)
Definition Logger.h:164
#define APPLICATION(req, res)
Definition Router.h:68
#define MIDDLEWARE(req, res, next)
Definition Router.h:63
Config * getConfig() const
Definition Socket.hpp:65
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:109
void send(const std::string &chunk)
Definition Response.cpp:169
Response & status(int status)
Definition Response.cpp:117
WebAppT(const std::string &name)
Definition WebAppT.h:74
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
LogMessage(Level level, int verboseLevel=-1, bool withErrno=false)
Definition Logger.cpp:280
const std::string & getInstanceName() const
ConfigTls * setCertKey(const std::string &certKey)
Definition ConfigTls.cpp:63
ConfigTls * setCert(const std::string &cert)
Definition ConfigTls.cpp:53
ConfigTls * setCertKeyPassword(const std::string &certKeyPassword)
Definition ConfigTls.cpp:73
std::string toString(bool expanded=true) const override
const Super & listen(uint16_t port, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
std::string getHtmlRoot()
Definition ConfigWWW.cpp:65
static ConfigRoot configRoot
Definition Config.h:116
NewSubCommand * newSubCommand(Args &&... args)
Definition SubCommand.h:280
RequestedSubCommand * getSubCommand()
Definition SubCommand.h:301
int main(int argc, char *argv[])
WebAppT< web::http::legacy::in6::Server > WebApp
Definition WebApp.h:54
WebAppT< web::http::tls::in6::Server > WebApp
Definition WebApp.h:54
Router getRouter(const std::string &webRoot)