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