xref: /openbmc/bmcweb/src/webserver_main.cpp (revision 9d424669)
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_v1.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))
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 int main(int /*argc*/, char** /*argv*/)
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::requestRoutes(app);
87     redfish::RedfishService redfish(app);
88 
89     // Create EventServiceManager instance and initialize Config
90     redfish::EventServiceManager::getInstance();
91 #endif
92 
93 #ifdef BMCWEB_ENABLE_DBUS_REST
94     crow::dbus_monitor::requestRoutes(app);
95     crow::image_upload::requestRoutes(app);
96     crow::openbmc_mapper::requestRoutes(app);
97 #endif
98 
99 #ifdef BMCWEB_ENABLE_HOST_SERIAL_WEBSOCKET
100     crow::obmc_console::requestRoutes(app);
101 #endif
102 
103 #ifdef BMCWEB_ENABLE_VM_WEBSOCKET
104     crow::obmc_vm::requestRoutes(app);
105 #endif
106 
107 #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
108     crow::ibm_mc::requestRoutes(app);
109     crow::ibm_mc_lock::Lock::getInstance();
110 #endif
111 
112 #ifdef BMCWEB_ENABLE_GOOGLE_API
113     crow::google_api::requestRoutes(app);
114 #endif
115 
116     if (bmcwebInsecureDisableXssPrevention)
117     {
118         cors_preflight::requestRoutes(app);
119     }
120 
121     crow::login_routes::requestRoutes(app);
122 
123     setupSocket(app);
124 
125 #ifdef BMCWEB_ENABLE_VM_NBDPROXY
126     crow::nbd_proxy::requestRoutes(app);
127 #endif
128 
129 #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
130     int rc = redfish::EventServiceManager::startEventLogMonitor(*io);
131     if (rc)
132     {
133         BMCWEB_LOG_ERROR << "Redfish event handler setup failed...";
134         return rc;
135     }
136 #endif
137 
138 #ifdef BMCWEB_ENABLE_SSL
139     BMCWEB_LOG_INFO << "Start Hostname Monitor Service...";
140     crow::hostname_monitor::registerHostnameSignal();
141 #endif
142 
143     app.run();
144     io->run();
145 
146     crow::connections::systemBus.reset();
147     return 0;
148 }
149