SNode.C
Loading...
Searching...
No Matches
verysimpleserver.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/in/WebApp.h"
21#include "express/middleware/StaticMiddleware.h"
22#include "express/tls/in/WebApp.h"
23#include "utils/Config.h"
24
25#ifndef DOXYGEN_SHOULD_SKIP_THIS
26
27#include "log/Logger.h"
28
29#include <string>
30
31#endif /* DOXYGEN_SHOULD_SKIP_THIS */
32
33int main(int argc, char* argv[]) {
34 utils::Config::addStringOption("--web-root", "Root directory of the web site", "[path]");
35
36 express::WebApp::init(argc, argv);
37
38 using LegacyWebApp = express::legacy::in::WebApp;
39 using LegacySocketAddress = LegacyWebApp::SocketAddress;
40
41 const LegacyWebApp legacyApp;
42 legacyApp.getConfig().setReuseAddress();
43
44 legacyApp.use(express::middleware::StaticMiddleware(utils::Config::getStringOptionValue("--web-root")));
45
46 legacyApp.listen(8080,
47 [instanceName = legacyApp.getConfig().getInstanceName()](const LegacySocketAddress& socketAddress,
48 const core::socket::State& state) {
49 switch (state) {
50 case core::socket::State::OK:
51 VLOG(1) << instanceName << " listening on '" << socketAddress.toString() << "'";
52 break;
53 case core::socket::State::DISABLED:
54 VLOG(1) << instanceName << " disabled";
55 break;
56 case core::socket::State::ERROR:
57 LOG(ERROR) << instanceName << " " << socketAddress.toString() << ": " << state.what();
58 break;
59 case core::socket::State::FATAL:
60 LOG(FATAL) << instanceName << " " << socketAddress.toString() << ": " << state.what();
61 break;
62 }
63 });
64
65 using TLSWebApp = express::tls::in::WebApp;
66 using TLSSocketAddress = TLSWebApp::SocketAddress;
67
68 const TLSWebApp tlsApp;
69 tlsApp.getConfig().setReuseAddress();
70
71 tlsApp.getConfig().setCert("/home/voc/projects/snodec/snode.c/certs/wildcard.home.vchrist.at_-_snode.c_-_server.pem");
72 tlsApp.getConfig().setCertKey("/home/voc/projects/snodec/snode.c/certs/Volker_Christian_-_Web_-_snode.c_-_server.key.encrypted.pem");
73 tlsApp.getConfig().setCertKeyPassword("snode.c");
74
75 tlsApp.use(express::middleware::StaticMiddleware(utils::Config::getStringOptionValue("--web-root")));
76
77 tlsApp.listen(
78 8088,
79 [instanceName = legacyApp.getConfig().getInstanceName()](const TLSSocketAddress& socketAddress, const core::socket::State& state) {
80 switch (state) {
81 case core::socket::State::OK:
82 VLOG(1) << instanceName << " listening on '" << socketAddress.toString() << "'";
83 break;
84 case core::socket::State::DISABLED:
85 VLOG(1) << instanceName << " disabled";
86 break;
87 case core::socket::State::ERROR:
88 LOG(ERROR) << instanceName << " " << socketAddress.toString() << ": " << state.what();
89 break;
90 case core::socket::State::FATAL:
91 LOG(FATAL) << instanceName << " " << socketAddress.toString() << ": " << state.what();
92 break;
93 }
94 });
95
96 return express::WebApp::start();
97}
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[])