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