xref: /openbmc/bmcweb/src/webserver_main.cpp (revision 6c068982)
1 #include "bmcweb_config.h"
2 
3 #include "app.hpp"
4 #include "cors_preflight.hpp"
5 #include "dbus_monitor.hpp"
6 #include "dbus_singleton.hpp"
7 #include "hostname_monitor.hpp"
8 #include "ibm/management_console_rest.hpp"
9 #include "image_upload.hpp"
10 #include "kvm_websocket.hpp"
11 #include "login_routes.hpp"
12 #include "nbd_proxy.hpp"
13 #include "obmc_console.hpp"
14 #include "openbmc_dbus_rest.hpp"
15 #include "redfish.hpp"
16 #include "redfish_aggregator.hpp"
17 #include "security_headers.hpp"
18 #include "ssl_key_handler.hpp"
19 #include "user_monitor.hpp"
20 #include "vm_websocket.hpp"
21 #include "webassets.hpp"
22 
23 #include <systemd/sd-daemon.h>
24 
25 #include <boost/asio/io_context.hpp>
26 #include <google/google_service_root.hpp>
27 #include <sdbusplus/asio/connection.hpp>
28 #include <sdbusplus/bus.hpp>
29 #include <sdbusplus/server.hpp>
30 
31 #include <exception>
32 #include <memory>
33 #include <string>
34 
35 constexpr int defaultPort = 18080;
36 
37 inline void setupSocket(crow::App& app)
38 {
39     int listenFd = sd_listen_fds(0);
40     if (1 == listenFd)
41     {
42         BMCWEB_LOG_INFO << "attempting systemd socket activation";
43         if (sd_is_socket_inet(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_STREAM, 1,
44                               0) != 0)
45         {
46             BMCWEB_LOG_INFO << "Starting webserver on socket handle "
47                             << SD_LISTEN_FDS_START;
48             app.socket(SD_LISTEN_FDS_START);
49         }
50         else
51         {
52             BMCWEB_LOG_INFO
53                 << "bad incoming socket, starting webserver on port "
54                 << defaultPort;
55             app.port(defaultPort);
56         }
57     }
58     else
59     {
60         BMCWEB_LOG_INFO << "Starting webserver on port " << defaultPort;
61         app.port(defaultPort);
62     }
63 }
64 
65 static int run()
66 {
67     crow::Logger::setLogLevel(crow::LogLevel::Debug);
68 
69     auto io = std::make_shared<boost::asio::io_context>();
70     App app(io);
71 
72     sdbusplus::asio::connection systemBus(*io);
73     crow::connections::systemBus = &systemBus;
74 
75     // Static assets need to be initialized before Authorization, because auth
76     // needs to build the whitelist from the static routes
77 
78 #ifdef BMCWEB_ENABLE_STATIC_HOSTING
79     crow::webassets::requestRoutes(app);
80 #endif
81 
82 #ifdef BMCWEB_ENABLE_KVM
83     crow::obmc_kvm::requestRoutes(app);
84 #endif
85 
86 #ifdef BMCWEB_ENABLE_REDFISH
87     redfish::RedfishService redfish(app);
88 
89     // Create HttpClient instance and initialize Config
90     crow::HttpClient::getInstance();
91 
92     // Create EventServiceManager instance and initialize Config
93     redfish::EventServiceManager::getInstance();
94 
95 #ifdef BMCWEB_ENABLE_REDFISH_AGGREGATION
96     // Create RedfishAggregator instance and initialize Config
97     redfish::RedfishAggregator::getInstance();
98 #endif
99 #endif
100 
101 #ifdef BMCWEB_ENABLE_DBUS_REST
102     crow::dbus_monitor::requestRoutes(app);
103     crow::image_upload::requestRoutes(app);
104     crow::openbmc_mapper::requestRoutes(app);
105 #endif
106 
107 #ifdef BMCWEB_ENABLE_HOST_SERIAL_WEBSOCKET
108     crow::obmc_console::requestRoutes(app);
109 #endif
110 
111 #ifdef BMCWEB_ENABLE_VM_WEBSOCKET
112     crow::obmc_vm::requestRoutes(app);
113 #endif
114 
115 #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
116     crow::ibm_mc::requestRoutes(app);
117     crow::ibm_mc_lock::Lock::getInstance();
118 #endif
119 
120 #ifdef BMCWEB_ENABLE_GOOGLE_API
121     crow::google_api::requestRoutes(app);
122 #endif
123 
124     if (bmcwebInsecureDisableXssPrevention != 0)
125     {
126         cors_preflight::requestRoutes(app);
127     }
128 
129     crow::login_routes::requestRoutes(app);
130 
131     setupSocket(app);
132 
133 #ifdef BMCWEB_ENABLE_VM_NBDPROXY
134     crow::nbd_proxy::requestRoutes(app);
135 #endif
136 
137 #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
138     int rc = redfish::EventServiceManager::startEventLogMonitor(*io);
139     if (rc != 0)
140     {
141         BMCWEB_LOG_ERROR << "Redfish event handler setup failed...";
142         return rc;
143     }
144 #endif
145 
146 #ifdef BMCWEB_ENABLE_SSL
147     BMCWEB_LOG_INFO << "Start Hostname Monitor Service...";
148     crow::hostname_monitor::registerHostnameSignal();
149 #endif
150 
151     bmcweb::registerUserRemovedSignal();
152 
153     app.run();
154     io->run();
155 
156     crow::connections::systemBus = nullptr;
157 
158     return 0;
159 }
160 
161 int main(int /*argc*/, char** /*argv*/)
162 {
163     try
164     {
165         return run();
166     }
167     catch (const std::exception& e)
168     {
169         BMCWEB_LOG_CRITICAL << "Threw exception to main: " << e.what();
170         return -1;
171     }
172     catch (...)
173     {
174         BMCWEB_LOG_CRITICAL << "Threw exception to main";
175         return -1;
176     }
177 }
178