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#include "express/legacy/in6/WebApp.h"
21#include "express/middleware/StaticMiddleware.h"
22#include "express/middleware/VHost.h"
23#include "express/tls/in6/WebApp.h"
24#include "utils/Config.h"
25
26#ifndef DOXYGEN_SHOULD_SKIP_THIS
27
28#include "log/Logger.h"
29
30#include <string>
31
32#endif /* DOXYGEN_SHOULD_SKIP_THIS */
33
34using namespace express;
35
36Router getRouter(const std::string& webRoot) {
37 const Router router;
38
39 router.use(middleware::StaticMiddleware(webRoot));
40
41 return router;
42}
43
44int main(int argc, char* argv[]) {
45 utils::Config::addStringOption("--web-root", "Root directory of the web site", "[path]");
46
47 WebApp::init(argc, argv);
48
49 {
50 const legacy::in6::WebApp legacyApp("legacy");
51
52 const Router& vh1 = middleware::VHost("localhost:8080");
53 vh1.use(middleware::StaticMiddleware(utils::Config::getStringOptionValue("--web-root")));
54 legacyApp.use(vh1);
55
56 const Router& vh2 = middleware::VHost("atlas.home.vchrist.at:8080");
57 vh2.get("/", [] APPLICATION(req, res) {
58 res->send("Hello! I am VHOST atlas.home.vchrist.at.");
59 });
60 legacyApp.use(vh2);
61
62 const Router& vh3 = middleware::VHost("ceres.home.vchrist.at:8080");
63 vh3.get("/", [] APPLICATION(req, res) {
64 res->send("Hello! I am VHOST ceres.home.vchrist.at.");
65 });
66 legacyApp.use(vh3);
67
68 legacyApp.use([] APPLICATION(req, res) {
69 res->status(404).send("The requested resource is not found.");
70 });
71
72 legacyApp.listen(8080,
73 [instanceName = legacyApp.getConfig().getInstanceName()](const legacy::in6::WebApp::SocketAddress& socketAddress,
74 const core::socket::State& state) {
75 switch (state) {
76 case core::socket::State::OK:
77 VLOG(1) << instanceName << " listening on '" << socketAddress.toString() << "'";
78 break;
79 case core::socket::State::DISABLED:
80 VLOG(1) << instanceName << " disabled";
81 break;
82 case core::socket::State::ERROR:
83 LOG(ERROR) << instanceName << " " << socketAddress.toString() << ": " << state.what();
84 break;
85 case core::socket::State::FATAL:
86 LOG(FATAL) << instanceName << " " << socketAddress.toString() << ": " << state.what();
87 break;
88 }
89 });
90 }
91
92 {
93 const express::tls::in6::WebApp tlsApp("tls");
94
95 const Router& vh1 = middleware::VHost("localhost:8088");
96 vh1.use(getRouter(utils::Config::getStringOptionValue("--web-root")));
97 tlsApp.use(vh1);
98
99 const Router& vh2 = middleware::VHost("atlas.home.vchrist.at:8088");
100 vh2.get("/", [] APPLICATION(req, res) {
101 res->send("Hello! I am VHOST atlas.home.vchrist.at.");
102 });
103 tlsApp.use(vh2);
104
105 const Router& vh3 = middleware::VHost("ceres.home.vchrist.at:8080");
106 vh3.get("/", [] APPLICATION(req, res) {
107 res->send("Hello! I am VHOST ceres.home.vchrist.at.");
108 });
109 tlsApp.use(vh3);
110
111 tlsApp.use([] APPLICATION(req, res) {
112 res->status(404).send("The requested resource is not found.");
113 });
114
115 tlsApp.listen(8088,
116 [instanceName = tlsApp.getConfig().getInstanceName()](const legacy::in6::WebApp::SocketAddress& socketAddress,
117 const core::socket::State& state) {
118 switch (state) {
119 case core::socket::State::OK:
120 VLOG(1) << instanceName << " listening on '" << socketAddress.toString() << "'";
121 break;
122 case core::socket::State::DISABLED:
123 VLOG(1) << instanceName << " disabled";
124 break;
125 case core::socket::State::ERROR:
126 LOG(ERROR) << instanceName << " " << socketAddress.toString() << ": " << state.what();
127 break;
128 case core::socket::State::FATAL:
129 LOG(FATAL) << instanceName << " " << socketAddress.toString() << ": " << state.what();
130 break;
131 }
132 });
133
134 tlsApp.getConfig().setCert("/home/voc/projects/snodec/snode.c/certs/wildcard.home.vchrist.at_-_snode.c_-_server.pem");
135 tlsApp.getConfig().setCertKey(
136 "/home/voc/projects/snodec/snode.c/certs/Volker_Christian_-_Web_-_snode.c_-_server.key.encrypted.pem");
137 tlsApp.getConfig().setCertKeyPassword("snode.c");
138 }
139
141}
#define APPLICATION(req, res)
Definition Router.h:45
static void init(int argc, char *argv[])
Definition WebApp.cpp:34
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
Definition WebApp.cpp:38
int main(int argc, char *argv[])
Router getRouter(const std::string &webRoot)