SNode.C
Loading...
Searching...
No Matches
testbasicauthentication.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/BasicAuthentication.h"
22#include "express/middleware/StaticMiddleware.h"
23#include "express/middleware/VHost.h"
24#include "express/tls/in6/WebApp.h"
25#include "utils/Config.h"
26
27#ifndef DOXYGEN_SHOULD_SKIP_THIS
28
29#include "log/Logger.h"
30
31#include <string>
32
33#endif /* DOXYGEN_SHOULD_SKIP_THIS */
34
35using namespace express;
36
37Router getRouter(const std::string& webRoot) {
38 const Router router;
39
40 router.use(middleware::BasicAuthentication("voc", "pentium5", "Authenticate yourself with username and password"));
41 router.use(middleware::StaticMiddleware(webRoot));
42
43 return router;
44}
45
46int main(int argc, char* argv[]) {
47 utils::Config::addStringOption("--web-root", "Root directory of the web site", "[path]");
48
49 WebApp::init(argc, argv);
50
51 {
52 const legacy::in6::WebApp legacyApp("legacy");
53
54 const Router& router1 = middleware::VHost("localhost:8080");
55
56 const Router& ba = middleware::BasicAuthentication("voc", "pentium5", "Authenticate yourself with username and password");
57 ba.use(middleware::StaticMiddleware(utils::Config::getStringOptionValue("--web-root")));
58
59 router1.use(ba);
60 legacyApp.use(router1);
61
62 const Router& router2 = middleware::VHost("ceres.home.vchrist.at:8080");
63 router2.get("/", [] APPLICATION(req, res) {
64 res->send("Hello! I am VHOST ceres.home.vchrist.at.");
65 });
66 legacyApp.use(router2);
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 const express::tls::in6::WebApp tlsApp("tls");
93
94 const Router& vh1 = middleware::VHost("localhost:8088");
95 vh1.use(getRouter(utils::Config::getStringOptionValue("--web-root")));
96 tlsApp.use(vh1);
97
98 const Router& vh2 = middleware::VHost("ceres.home.vchrist.at:8088");
99 vh2.get("/", [] APPLICATION(req, res) {
100 res->send("Hello! I am VHOST ceres.home.vchrist.at.");
101 });
102
103 tlsApp.use(vh2);
104
105 tlsApp.use([] APPLICATION(req, res) {
106 res->status(404).send("The requested resource is not found.");
107 });
108
109 tlsApp.listen(8088, [](const legacy::in6::WebApp::SocketAddress& socketAddress, const core::socket::State& state) {
110 switch (state) {
111 case core::socket::State::OK:
112 VLOG(1) << "tls: listening on '" << socketAddress.toString() << "'"
113 << "'";
114 break;
115 case core::socket::State::DISABLED:
116 VLOG(1) << "tls: disabled";
117 break;
118 case core::socket::State::ERROR:
119 VLOG(1) << "tls: error occurred";
120 break;
121 case core::socket::State::FATAL:
122 VLOG(1) << "tls: fatal error occurred";
123 break;
124 }
125 });
126 }
127 }
128
130}
#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)