xref: /openbmc/bmcweb/src/webserver_run.cpp (revision 55385c705c0efc58b88ff885b78ee65acb455b85)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #include "webserver_run.hpp"
4 
5 #include "bmcweb_config.h"
6 
7 #include "app.hpp"
8 #include "dbus_monitor.hpp"
9 #include "dbus_singleton.hpp"
10 #include "event_service_manager.hpp"
11 #include "google_service_root.hpp"
12 #include "hostname_monitor.hpp"
13 #include "ibm_management_console_rest.hpp"
14 #include "image_upload.hpp"
15 #include "io_context_singleton.hpp"
16 #include "kvm_websocket.hpp"
17 #include "logging.hpp"
18 #include "login_routes.hpp"
19 #include "obmc_console.hpp"
20 #include "openbmc_dbus_rest.hpp"
21 #include "persistent_data.hpp"
22 #include "redfish.hpp"
23 #include "redfish_aggregator.hpp"
24 #include "user_monitor.hpp"
25 #include "vm_websocket.hpp"
26 #include "watchdog.hpp"
27 #include "webassets.hpp"
28 
29 #include <boost/asio/io_context.hpp>
30 #include <sdbusplus/asio/connection.hpp>
31 #include <sdbusplus/asio/object_server.hpp>
32 
33 #include <algorithm>
34 #include <memory>
35 #include <string>
36 #include <string_view>
37 
setLogLevel(const std::string & logLevel)38 static void setLogLevel(const std::string& logLevel)
39 {
40     const std::basic_string_view<char>* iter =
41         std::ranges::find(crow::mapLogLevelFromName, logLevel);
42     if (iter == crow::mapLogLevelFromName.end())
43     {
44         BMCWEB_LOG_ERROR("log-level {} not found", logLevel);
45         return;
46     }
47     crow::getBmcwebCurrentLoggingLevel() = crow::getLogLevelFromName(logLevel);
48     BMCWEB_LOG_INFO("Requested log-level change to: {}", logLevel);
49 }
50 
runWebserver()51 int runWebserver()
52 {
53     boost::asio::io_context& io = getIoContext();
54     App app;
55 
56     std::shared_ptr<sdbusplus::asio::connection> systemBus =
57         std::make_shared<sdbusplus::asio::connection>(io);
58     crow::connections::systemBus = systemBus.get();
59 
60     auto server = sdbusplus::asio::object_server(systemBus);
61 
62     std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
63         server.add_interface("/xyz/openbmc_project/bmcweb",
64                              "xyz.openbmc_project.bmcweb");
65 
66     iface->register_method("SetLogLevel", setLogLevel);
67 
68     iface->initialize();
69 
70     // Load the peristent data
71     persistent_data::getConfig();
72 
73     // Static assets need to be initialized before Authorization, because auth
74     // needs to build the whitelist from the static routes
75 
76     if constexpr (BMCWEB_STATIC_HOSTING)
77     {
78         crow::webassets::requestRoutes(app);
79     }
80 
81     if constexpr (BMCWEB_KVM)
82     {
83         crow::obmc_kvm::requestRoutes(app);
84     }
85 
86     if constexpr (BMCWEB_REDFISH)
87     {
88         redfish::RedfishService::getInstance(app);
89 
90         // Create EventServiceManager instance and initialize Config
91         redfish::EventServiceManager::getInstance();
92 
93         if constexpr (BMCWEB_REDFISH_AGGREGATION)
94         {
95             // Create RedfishAggregator instance and initialize Config
96             redfish::RedfishAggregator::getInstance();
97         }
98     }
99 
100     if constexpr (BMCWEB_REST)
101     {
102         crow::dbus_monitor::requestRoutes(app);
103         crow::image_upload::requestRoutes(app);
104         crow::openbmc_mapper::requestRoutes(app);
105     }
106 
107     if constexpr (BMCWEB_HOST_SERIAL_SOCKET)
108     {
109         crow::obmc_console::requestRoutes(app);
110     }
111 
112     crow::obmc_vm::requestRoutes(app);
113 
114     if constexpr (BMCWEB_IBM_MANAGEMENT_CONSOLE)
115     {
116         crow::ibm_mc::requestRoutes(app);
117     }
118 
119     if constexpr (BMCWEB_GOOGLE_API)
120     {
121         crow::google_api::requestRoutes(app);
122     }
123 
124     crow::login_routes::requestRoutes(app);
125 
126     if constexpr (!BMCWEB_INSECURE_DISABLE_SSL)
127     {
128         BMCWEB_LOG_INFO("Start Hostname Monitor Service...");
129         crow::hostname_monitor::registerHostnameSignal();
130     }
131 
132     bmcweb::registerUserRemovedSignal();
133 
134     bmcweb::ServiceWatchdog watchdog;
135 
136     app.run();
137 
138     systemBus->request_name("xyz.openbmc_project.bmcweb");
139 
140     io.run();
141 
142     crow::connections::systemBus = nullptr;
143 
144     return 0;
145 }
146