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/*
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 "apps/http/model/servers.h"
43#include "express/middleware/StaticMiddleware.h"
44#include "net/config/ConfigSection.hpp"
45
46#ifndef DOXYGEN_SHOULD_SKIP_THIS
47
48#if (STREAM_TYPE == TLS) // tls
49
50#include <utility>
51#include <variant>
52
53#endif // (STREAM_TYPE == TLS)
54
55#include "log/Logger.h"
56
57#include <algorithm>
58#include <list>
59
60#endif /* DOXYGEN_SHOULD_SKIP_THIS */
61
62using namespace express;
63
64int main(int argc, char* argv[]) {
65 using WebApp = apps::http::STREAM::WebApp;
66
67 const WebApp webApp(apps::http::STREAM::getWebApp("httpserver"));
68
69 net::config::ConfigSection configWeb = net::config::ConfigSection(&webApp.getConfig(), "www", "Web behavior of httpserver");
70 CLI::Option* htmlRoot = configWeb.addOption("--html-root", "HTML root directory", "path", "");
71 configWeb.required(htmlRoot);
72
73 WebApp::init(argc, argv);
74
75 webApp.get(express::middleware::StaticMiddleware(htmlRoot->as<std::string>()));
76
77 {
78#if (STREAM_TYPE == TLS)
79 std::string cert = "/home/voc/projects/snodec/snode.c/certs/snode.c_-_server.pem";
80 std::string key = "/home/voc/projects/snodec/snode.c/certs/Volker_Christian_-_Web_-_snode.c_-_server.key.encrypted.pem";
81 std::string pass = "snode.c";
82
83 std::map<std::string, std::map<std::string, std::variant<std::string, bool, ssl_option_t>>> sniCerts = {
84 {"snodec.home.vchrist.at", {{"Cert", cert}, {"CertKey", key}, {"CertKeyPassword", pass}}},
85 {"www.vchrist.at", {{"Cert", cert}, {"CertKey", key}, {"CertKeyPassword", pass}}}};
86
87 webApp.getConfig().addSniCerts(sniCerts);
88#endif
89
90 VLOG(1) << "Routes:";
91 for (std::string& route : webApp.getRoutes()) {
92 route.erase(std::remove(route.begin(), route.end(), '$'), route.end());
93
94 VLOG(1) << " " << route;
95 }
96
97 webApp.listen([instanceName = webApp.getConfig().getInstanceName()](const core::socket::SocketAddress& socketAddress,
98 const core::socket::State& state) {
99 switch (state) {
100 case core::socket::State::OK:
101 VLOG(1) << instanceName << ": listening on '" << socketAddress.toString() << "'";
102 break;
103 case core::socket::State::DISABLED:
104 VLOG(1) << instanceName << ": disabled";
105 break;
106 case core::socket::State::ERROR:
107 VLOG(1) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
108 break;
109 case core::socket::State::FATAL:
110 VLOG(1) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
111 break;
112 }
113 });
114 }
115 return WebApp::start();
116}
117
118/*
119#if (NET_TYPE == IN) // in
120#if (STREAM_TYPE == LEGACY)
121 webApp.listen(8080, 5, [](const WebApp::SocketAddress& socketAddress, const core::socket::State& state) {
122#elif (STREAM_TYPE == TLS)
123 webApp.listen(8088, 5, [](const WebApp::SocketAddress& socketAddress, const core::socket::State& state) {
124#endif
125#elif (NET_TYPE == IN6) // in6
126#if (STREAM_TYPE == LEGACY)
127 webApp.listen(8080, 5, [](const WebApp::SocketAddress& socketAddress, const core::socket::State& state) {
128#elif (STREAM_TYPE == TLS)
129 webApp.listen(8088, 5, [](const WebApp::SocketAddress& socketAddress, const core::socket::State& state) {
130#endif
131#elif (NET_TYPE == L2) //
132 // ATLAS: 10:3D:1C:AC:BA:9C
133 // TITAN: A4:B1:C1:2C:82:37
134 // USB: 44:01:BB:A3:63:32
135
136 // webApp.listen("A4:B1:C1:2C:82:37", 0x1023, 5, [](const WebApp::SocketAddress& socketAddress, const core::socket::State& state) ->
137void { // titan webApp.listen("10:3D:1C:AC:BA:9C", 0x1023, 5, [](const WebApp::SocketAddress& socketAddress, const core::socket::State&
138state) { // titan #elif (NET_TYPE == RC) // rf
139 // webApp.listen("A4:B1:C1:2C:82:37", 1, 5, [](const WebApp::SocketAddress& socketAddress, const core::socket::State& state) {
140// titan webApp.listen("10:3D:1C:AC:BA:9C", 1, 5, [](const WebApp::SocketAddress& socketAddress, const core::socket::State& state) {
141// titan #elif (NET_TYPE == UN) // un webApp.listen("/tmp/testme", 5, [](const WebApp::SocketAddress& socketAddress, const
142core::socket::State& state) { // titan #endif if (errnum != 0) { PLOG(FATAL) << "listen"; } else { VLOG(1) << "snode.c listening on
143" << socketAddress.toString();
144 }
145
146#ifdef NET_TYPE
147 });
148#endif
149*/
int main(int argc, char *argv[])