xref: /openbmc/bmcweb/src/webserver_main.cpp (revision e9cc1bc9)
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 "google/google_service_root.hpp"
8 #include "hostname_monitor.hpp"
9 #include "ibm/management_console_rest.hpp"
10 #include "image_upload.hpp"
11 #include "kvm_websocket.hpp"
12 #include "login_routes.hpp"
13 #include "nbd_proxy.hpp"
14 #include "obmc_console.hpp"
15 #include "openbmc_dbus_rest.hpp"
16 #include "redfish.hpp"
17 #include "redfish_aggregator.hpp"
18 #include "security_headers.hpp"
19 #include "ssl_key_handler.hpp"
20 #include "user_monitor.hpp"
21 #include "vm_websocket.hpp"
22 #include "webassets.hpp"
23 
24 #include <systemd/sd-daemon.h>
25 
26 #include <boost/asio/io_context.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     auto io = std::make_shared<boost::asio::io_context>();
68     App app(io);
69 
70     sdbusplus::asio::connection systemBus(*io);
71     crow::connections::systemBus = &systemBus;
72 
73     // Static assets need to be initialized before Authorization, because auth
74     // needs to build the whitelist from the static routes
75 
76 #ifdef BMCWEB_ENABLE_STATIC_HOSTING
77     crow::webassets::requestRoutes(app);
78 #endif
79 
80 #ifdef BMCWEB_ENABLE_KVM
81     crow::obmc_kvm::requestRoutes(app);
82 #endif
83 
84 #ifdef BMCWEB_ENABLE_REDFISH
85     redfish::RedfishService redfish(app);
86 
87     // Create EventServiceManager instance and initialize Config
88     redfish::EventServiceManager::getInstance(&*io);
89 
90 #ifdef BMCWEB_ENABLE_REDFISH_AGGREGATION
91     // Create RedfishAggregator instance and initialize Config
92     redfish::RedfishAggregator::getInstance(&*io);
93 #endif
94 #endif
95 
96 #ifdef BMCWEB_ENABLE_DBUS_REST
97     crow::dbus_monitor::requestRoutes(app);
98     crow::image_upload::requestRoutes(app);
99     crow::openbmc_mapper::requestRoutes(app);
100 #endif
101 
102 #ifdef BMCWEB_ENABLE_HOST_SERIAL_WEBSOCKET
103     crow::obmc_console::requestRoutes(app);
104 #endif
105 
106 #ifdef BMCWEB_ENABLE_VM_WEBSOCKET
107     crow::obmc_vm::requestRoutes(app);
108 #endif
109 
110 #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
111     crow::ibm_mc::requestRoutes(app);
112     crow::ibm_mc_lock::Lock::getInstance();
113 #endif
114 
115 #ifdef BMCWEB_ENABLE_GOOGLE_API
116     crow::google_api::requestRoutes(app);
117 #endif
118 
119     if (bmcwebInsecureDisableXssPrevention != 0)
120     {
121         cors_preflight::requestRoutes(app);
122     }
123 
124     crow::login_routes::requestRoutes(app);
125 
126     setupSocket(app);
127 
128 #ifdef BMCWEB_ENABLE_VM_NBDPROXY
129     crow::nbd_proxy::requestRoutes(app);
130 #endif
131 
132 #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
133     int rc = redfish::EventServiceManager::startEventLogMonitor(*io);
134     if (rc != 0)
135     {
136         BMCWEB_LOG_ERROR("Redfish event handler setup failed...");
137         return rc;
138     }
139 #endif
140 
141 #ifdef BMCWEB_ENABLE_SSL
142     BMCWEB_LOG_INFO("Start Hostname Monitor Service...");
143     crow::hostname_monitor::registerHostnameSignal();
144 #endif
145 
146     bmcweb::registerUserRemovedSignal();
147 
148     app.run();
149     io->run();
150 
151     crow::connections::systemBus = nullptr;
152 
153     return 0;
154 }
155 
156 int main(int /*argc*/, char** /*argv*/)
157 {
158     try
159     {
160         return run();
161     }
162     catch (const std::exception& e)
163     {
164         BMCWEB_LOG_CRITICAL("Threw exception to main: {}", e.what());
165         return -1;
166     }
167     catch (...)
168     {
169         BMCWEB_LOG_CRITICAL("Threw exception to main");
170         return -1;
171     }
172 }
173