SNode.C
Loading...
Searching...
No Matches
httpserver.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 "apps/http/model/servers.h"
21#include "express/middleware/StaticMiddleware.h"
22#include "net/config/ConfigSection.hpp"
23
24#ifndef DOXYGEN_SHOULD_SKIP_THIS
25
26#if (STREAM_TYPE == TLS) // tls
27
28#include <utility>
29#include <variant>
30
31#endif // (STREAM_TYPE == TLS)
32
33#include "log/Logger.h"
34
35#endif /* DOXYGEN_SHOULD_SKIP_THIS */
36
37using namespace express;
38
39int main(int argc, char* argv[]) {
40 using WebApp = apps::http::STREAM::WebApp;
41
42 const WebApp webApp(apps::http::STREAM::getWebApp("httpserver"));
43
44 net::config::ConfigSection configWeb = net::config::ConfigSection(&webApp.getConfig(), "www", "Web behavior of httpserver");
45 CLI::Option* htmlRoot = configWeb.addOption("--html-root", "HTML root directory", "path", "");
46 configWeb.required(htmlRoot);
47
48 WebApp::init(argc, argv);
49
50 webApp.use(express::middleware::StaticMiddleware(htmlRoot->as<std::string>()));
51
52 {
53#if (STREAM_TYPE == TLS)
54 std::string cert = "/home/voc/projects/snodec/snode.c/certs/snode.c_-_server.pem";
55 std::string key = "/home/voc/projects/snodec/snode.c/certs/Volker_Christian_-_Web_-_snode.c_-_server.key.encrypted.pem";
56 std::string pass = "snode.c";
57
58 std::map<std::string, std::map<std::string, std::variant<std::string, bool, ssl_option_t>>> sniCerts = {
59 {"snodec.home.vchrist.at", {{"Cert", cert}, {"CertKey", key}, {"CertKeyPassword", pass}}},
60 {"www.vchrist.at", {{"Cert", cert}, {"CertKey", key}, {"CertKeyPassword", pass}}}};
61
62 webApp.getConfig().addSniCerts(sniCerts);
63#endif
64
65 webApp.listen([instanceName = webApp.getConfig().getInstanceName()](const core::socket::SocketAddress& socketAddress,
66 const core::socket::State& state) {
67 switch (state) {
68 case core::socket::State::OK:
69 VLOG(1) << instanceName << ": listening on '" << socketAddress.toString() << "'";
70 break;
71 case core::socket::State::DISABLED:
72 VLOG(1) << instanceName << ": disabled";
73 break;
74 case core::socket::State::ERROR:
75 VLOG(1) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
76 break;
77 case core::socket::State::FATAL:
78 VLOG(1) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
79 break;
80 }
81 });
82 }
83 return WebApp::start();
84}
85
86/*
87#if (NET_TYPE == IN) // in
88#if (STREAM_TYPE == LEGACY)
89 webApp.listen(8080, 5, [](const WebApp::SocketAddress& socketAddress, const core::socket::State& state) {
90#elif (STREAM_TYPE == TLS)
91 webApp.listen(8088, 5, [](const WebApp::SocketAddress& socketAddress, const core::socket::State& state) {
92#endif
93#elif (NET_TYPE == IN6) // in6
94#if (STREAM_TYPE == LEGACY)
95 webApp.listen(8080, 5, [](const WebApp::SocketAddress& socketAddress, const core::socket::State& state) {
96#elif (STREAM_TYPE == TLS)
97 webApp.listen(8088, 5, [](const WebApp::SocketAddress& socketAddress, const core::socket::State& state) {
98#endif
99#elif (NET_TYPE == L2) //
100 // ATLAS: 10:3D:1C:AC:BA:9C
101 // TITAN: A4:B1:C1:2C:82:37
102 // USB: 44:01:BB:A3:63:32
103
104 // webApp.listen("A4:B1:C1:2C:82:37", 0x1023, 5, [](const WebApp::SocketAddress& socketAddress, const core::socket::State& state) ->
105void { // titan webApp.listen("10:3D:1C:AC:BA:9C", 0x1023, 5, [](const WebApp::SocketAddress& socketAddress, const core::socket::State&
106state) { // titan #elif (NET_TYPE == RC) // rf
107 // webApp.listen("A4:B1:C1:2C:82:37", 1, 5, [](const WebApp::SocketAddress& socketAddress, const core::socket::State& state) {
108// titan webApp.listen("10:3D:1C:AC:BA:9C", 1, 5, [](const WebApp::SocketAddress& socketAddress, const core::socket::State& state) {
109// titan #elif (NET_TYPE == UN) // un webApp.listen("/tmp/testme", 5, [](const WebApp::SocketAddress& socketAddress, const
110core::socket::State& state) { // titan #endif if (errnum != 0) { PLOG(FATAL) << "listen"; } else { VLOG(1) << "snode.c listening on
111" << socketAddress.toString();
112 }
113
114#ifdef NET_TYPE
115 });
116#endif
117*/
int main(int argc, char *argv[])