xref: /openbmc/bmcweb/src/webserver_main.cpp (revision 81d523a7)
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 <sdbusplus/asio/connection.hpp>
20 #include <sdbusplus/bus.hpp>
21 #include <sdbusplus/server.hpp>
22 #include <security_headers.hpp>
23 #include <ssl_key_handler.hpp>
24 #include <vm_websocket.hpp>
25 #include <webassets.hpp>
26 
27 #include <memory>
28 #include <string>
29 
30 #ifdef BMCWEB_ENABLE_VM_NBDPROXY
31 #include <nbd_proxy.hpp>
32 #endif
33 
34 constexpr int defaultPort = 18080;
35 
36 inline void setupSocket(crow::App& app)
37 {
38     int listenFd = sd_listen_fds(0);
39     if (1 == listenFd)
40     {
41         BMCWEB_LOG_INFO << "attempting systemd socket activation";
42         if (sd_is_socket_inet(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_STREAM, 1,
43                               0) != 0)
44         {
45             BMCWEB_LOG_INFO << "Starting webserver on socket handle "
46                             << SD_LISTEN_FDS_START;
47             app.socket(SD_LISTEN_FDS_START);
48         }
49         else
50         {
51             BMCWEB_LOG_INFO
52                 << "bad incoming socket, starting webserver on port "
53                 << defaultPort;
54             app.port(defaultPort);
55         }
56     }
57     else
58     {
59         BMCWEB_LOG_INFO << "Starting webserver on port " << defaultPort;
60         app.port(defaultPort);
61     }
62 }
63 
64 static int run()
65 {
66     crow::Logger::setLogLevel(crow::LogLevel::Debug);
67 
68     auto io = std::make_shared<boost::asio::io_context>();
69     App app(io);
70 
71     crow::connections::systemBus =
72         std::make_shared<sdbusplus::asio::connection>(*io);
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 HttpClient instance and initialize Config
89     crow::HttpClient::getInstance();
90 
91     // Create EventServiceManager instance and initialize Config
92     redfish::EventServiceManager::getInstance();
93 
94 #ifdef BMCWEB_ENABLE_REDFISH_AGGREGATION
95     // Create RedfishAggregator instance and initialize Config
96     redfish::RedfishAggregator::getInstance();
97 #endif
98 #endif
99 
100 #ifdef BMCWEB_ENABLE_DBUS_REST
101     crow::dbus_monitor::requestRoutes(app);
102     crow::image_upload::requestRoutes(app);
103     crow::openbmc_mapper::requestRoutes(app);
104 #endif
105 
106 #ifdef BMCWEB_ENABLE_HOST_SERIAL_WEBSOCKET
107     crow::obmc_console::requestRoutes(app);
108 #endif
109 
110 #ifdef BMCWEB_ENABLE_VM_WEBSOCKET
111     crow::obmc_vm::requestRoutes(app);
112 #endif
113 
114 #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
115     crow::ibm_mc::requestRoutes(app);
116     crow::ibm_mc_lock::Lock::getInstance();
117 #endif
118 
119 #ifdef BMCWEB_ENABLE_GOOGLE_API
120     crow::google_api::requestRoutes(app);
121 #endif
122 
123     if (bmcwebInsecureDisableXssPrevention != 0)
124     {
125         cors_preflight::requestRoutes(app);
126     }
127 
128     crow::login_routes::requestRoutes(app);
129 
130     setupSocket(app);
131 
132 #ifdef BMCWEB_ENABLE_VM_NBDPROXY
133     crow::nbd_proxy::requestRoutes(app);
134 #endif
135 
136 #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
137     int rc = redfish::EventServiceManager::startEventLogMonitor(*io);
138     if (rc != 0)
139     {
140         BMCWEB_LOG_ERROR << "Redfish event handler setup failed...";
141         return rc;
142     }
143 #endif
144 
145 #ifdef BMCWEB_ENABLE_SSL
146     BMCWEB_LOG_INFO << "Start Hostname Monitor Service...";
147     crow::hostname_monitor::registerHostnameSignal();
148 #endif
149 
150     app.run();
151     io->run();
152 
153     crow::connections::systemBus.reset();
154     return 0;
155 }
156 
157 int main(int /*argc*/, char** /*argv*/)
158 {
159     try
160     {
161         return run();
162     }
163     catch (...)
164     {
165         return -1;
166         BMCWEB_LOG_CRITICAL << "Threw exception to main";
167     }
168 }
169