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