xref: /openbmc/bmcweb/src/webserver_main.cpp (revision 16a5535f)
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 <nbd_proxy.hpp>
16 #include <obmc_console.hpp>
17 #include <openbmc_dbus_rest.hpp>
18 #include <redfish.hpp>
19 #include <redfish_aggregator.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 constexpr int defaultPort = 18080;
32 
33 inline void setupSocket(crow::App& app)
34 {
35     int listenFd = sd_listen_fds(0);
36     if (1 == listenFd)
37     {
38         BMCWEB_LOG_INFO << "attempting systemd socket activation";
39         if (sd_is_socket_inet(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_STREAM, 1,
40                               0) != 0)
41         {
42             BMCWEB_LOG_INFO << "Starting webserver on socket handle "
43                             << SD_LISTEN_FDS_START;
44             app.socket(SD_LISTEN_FDS_START);
45         }
46         else
47         {
48             BMCWEB_LOG_INFO
49                 << "bad incoming socket, starting webserver on port "
50                 << defaultPort;
51             app.port(defaultPort);
52         }
53     }
54     else
55     {
56         BMCWEB_LOG_INFO << "Starting webserver on port " << defaultPort;
57         app.port(defaultPort);
58     }
59 }
60 
61 static int run()
62 {
63     crow::Logger::setLogLevel(crow::LogLevel::Debug);
64 
65     auto io = std::make_shared<boost::asio::io_context>();
66     App app(io);
67 
68     crow::connections::systemBus =
69         std::make_shared<sdbusplus::asio::connection>(*io);
70 
71     // Static assets need to be initialized before Authorization, because auth
72     // needs to build the whitelist from the static routes
73 
74 #ifdef BMCWEB_ENABLE_STATIC_HOSTING
75     crow::webassets::requestRoutes(app);
76 #endif
77 
78 #ifdef BMCWEB_ENABLE_KVM
79     crow::obmc_kvm::requestRoutes(app);
80 #endif
81 
82 #ifdef BMCWEB_ENABLE_REDFISH
83     redfish::RedfishService redfish(app);
84 
85     // Create HttpClient instance and initialize Config
86     crow::HttpClient::getInstance();
87 
88     // Create EventServiceManager instance and initialize Config
89     redfish::EventServiceManager::getInstance();
90 
91 #ifdef BMCWEB_ENABLE_REDFISH_AGGREGATION
92     // Create RedfishAggregator instance and initialize Config
93     redfish::RedfishAggregator::getInstance();
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     app.run();
148     io->run();
149 
150     crow::connections::systemBus.reset();
151     return 0;
152 }
153 
154 int main(int /*argc*/, char** /*argv*/)
155 {
156     try
157     {
158         return run();
159     }
160     catch (...)
161     {
162         return -1;
163         BMCWEB_LOG_CRITICAL << "Threw exception to main";
164     }
165 }
166