SNode.C
Loading...
Searching...
No Matches
jsonserver.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 * Json Middleware 2020, 2021 Marlene Mayr, Anna Moser, Matteo Prock, Eric Thalhammer
6 * Github <MarleneMayr><moseranna><MatteoMatteoMatteo><peregrin-tuk>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "express/legacy/in/WebApp.h"
23#include "express/middleware/JsonMiddleware.h"
24
25#ifndef DOXYGEN_SHOULD_SKIP_THIS
26
27#include "log/Logger.h"
28
29#include <nlohmann/json.hpp>
30#include <string>
31
32// IWYU pragma: no_include <nlohmann/json_fwd.hpp>
33
34#endif /* DOXYGEN_SHOULD_SKIP_THIS */
35
36int main(int argc, char* argv[]) {
37 using WebApp = express::legacy::in::WebApp;
38
39 WebApp::init(argc, argv);
40
41 // el::Loggers::setVModules("jsonserver*=0");
42
43 using SocketAddress = WebApp::SocketAddress;
44
45 WebApp legacyApp("legacy-jsonserver");
46
47 legacyApp.use(express::middleware::JsonMiddleware());
48
49 legacyApp.listen(
50 8080,
51 [instanceName = legacyApp.getConfig().getInstanceName()](const SocketAddress& socketAddress, const core::socket::State& state) {
52 switch (state) {
53 case core::socket::State::OK:
54 VLOG(1) << instanceName << ": listening on '" << socketAddress.toString() << "'";
55 break;
56 case core::socket::State::DISABLED:
57 VLOG(1) << instanceName << ": disabled";
58 break;
59 case core::socket::State::ERROR:
60 LOG(ERROR) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
61 break;
62 case core::socket::State::FATAL:
63 LOG(FATAL) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
64 break;
65 }
66 });
67
68 legacyApp.post("/index.html", [] APPLICATION(req, res) {
69 std::string jsonString;
70
71 req->getAttribute<nlohmann::json>(
72 [&jsonString](nlohmann::json& json) {
73 jsonString = json.dump(4);
74 VLOG(1) << "Application received body: " << jsonString;
75 },
76 [](const std::string& key) {
77 VLOG(1) << key << " attribute not found";
78 });
79
80 res->send(jsonString);
81 });
82
83 legacyApp.post([] APPLICATION(req, res) {
84 res->send("Wrong Url");
85 });
86
87 return WebApp::start();
88}
#define APPLICATION(req, res)
Definition Router.h:45
int main(int argc, char *argv[])