xref: /openbmc/bmcweb/src/webserver_main.cpp (revision 002d39b4)
1 #include <bmcweb_config.h>
2 #include <systemd/sd-daemon.h>
3 
4 #include <app.hpp>
5 #include <boost/asio/io_context.hpp>
6 #include <cors_preflight.hpp>
7 #include <dbus_monitor.hpp>
8 #include <dbus_singleton.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 <login_routes.hpp>
15 #include <obmc_console.hpp>
16 #include <openbmc_dbus_rest.hpp>
17 #include <redfish.hpp>
18 #include <redfish_aggregator.hpp>
19 #include <redfish_v1.hpp>
20 #include <sdbusplus/asio/connection.hpp>
21 #include <sdbusplus/bus.hpp>
22 #include <sdbusplus/server.hpp>
23 #include <security_headers.hpp>
24 #include <ssl_key_handler.hpp>
25 #include <vm_websocket.hpp>
26 #include <webassets.hpp>
27 
28 #include <memory>
29 #include <string>
30 
31 #ifdef BMCWEB_ENABLE_VM_NBDPROXY
32 #include <nbd_proxy.hpp>
33 #endif
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     crow::connections::systemBus =
73         std::make_shared<sdbusplus::asio::connection>(*io);
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::requestRoutes(app);
88     redfish::RedfishService redfish(app);
89 
90     // Create HttpClient instance and initialize Config
91     crow::HttpClient::getInstance();
92 
93     // Create EventServiceManager instance and initialize Config
94     redfish::EventServiceManager::getInstance();
95 
96 #ifdef BMCWEB_ENABLE_REDFISH_AGGREGATION
97     // Create RedfishAggregator instance and initialize Config
98     redfish::RedfishAggregator::getInstance();
99 #endif
100 #endif
101 
102 #ifdef BMCWEB_ENABLE_DBUS_REST
103     crow::dbus_monitor::requestRoutes(app);
104     crow::image_upload::requestRoutes(app);
105     crow::openbmc_mapper::requestRoutes(app);
106 #endif
107 
108 #ifdef BMCWEB_ENABLE_HOST_SERIAL_WEBSOCKET
109     crow::obmc_console::requestRoutes(app);
110 #endif
111 
112 #ifdef BMCWEB_ENABLE_VM_WEBSOCKET
113     crow::obmc_vm::requestRoutes(app);
114 #endif
115 
116 #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
117     crow::ibm_mc::requestRoutes(app);
118     crow::ibm_mc_lock::Lock::getInstance();
119 #endif
120 
121 #ifdef BMCWEB_ENABLE_GOOGLE_API
122     crow::google_api::requestRoutes(app);
123 #endif
124 
125     if (bmcwebInsecureDisableXssPrevention != 0)
126     {
127         cors_preflight::requestRoutes(app);
128     }
129 
130     crow::login_routes::requestRoutes(app);
131 
132     setupSocket(app);
133 
134 #ifdef BMCWEB_ENABLE_VM_NBDPROXY
135     crow::nbd_proxy::requestRoutes(app);
136 #endif
137 
138 #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
139     int rc = redfish::EventServiceManager::startEventLogMonitor(*io);
140     if (rc != 0)
141     {
142         BMCWEB_LOG_ERROR << "Redfish event handler setup failed...";
143         return rc;
144     }
145 #endif
146 
147 #ifdef BMCWEB_ENABLE_SSL
148     BMCWEB_LOG_INFO << "Start Hostname Monitor Service...";
149     crow::hostname_monitor::registerHostnameSignal();
150 #endif
151 
152     app.run();
153     io->run();
154 
155     crow::connections::systemBus.reset();
156     return 0;
157 }
158 
159 int main(int /*argc*/, char** /*argv*/)
160 {
161     try
162     {
163         return run();
164     }
165     catch (...)
166     {
167         return -1;
168         BMCWEB_LOG_CRITICAL << "Threw exception to main";
169     }
170 }
171