xref: /openbmc/bmcweb/src/webserver_main.cpp (revision ed76121b)
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 <exception>
29 #include <memory>
30 #include <string>
31 
32 constexpr int defaultPort = 18080;
33 
34 inline void setupSocket(crow::App& app)
35 {
36     int listenFd = sd_listen_fds(0);
37     if (1 == listenFd)
38     {
39         BMCWEB_LOG_INFO << "attempting systemd socket activation";
40         if (sd_is_socket_inet(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_STREAM, 1,
41                               0) != 0)
42         {
43             BMCWEB_LOG_INFO << "Starting webserver on socket handle "
44                             << SD_LISTEN_FDS_START;
45             app.socket(SD_LISTEN_FDS_START);
46         }
47         else
48         {
49             BMCWEB_LOG_INFO
50                 << "bad incoming socket, starting webserver on port "
51                 << defaultPort;
52             app.port(defaultPort);
53         }
54     }
55     else
56     {
57         BMCWEB_LOG_INFO << "Starting webserver on port " << defaultPort;
58         app.port(defaultPort);
59     }
60 }
61 
62 static int run()
63 {
64     crow::Logger::setLogLevel(crow::LogLevel::Debug);
65 
66     auto io = std::make_shared<boost::asio::io_context>();
67     App app(io);
68 
69     sdbusplus::asio::connection systemBus(*io);
70     crow::connections::systemBus = &systemBus;
71 
72     // Static assets need to be initialized before Authorization, because auth
73     // needs to build the whitelist from the static routes
74 
75 #ifdef BMCWEB_ENABLE_STATIC_HOSTING
76     crow::webassets::requestRoutes(app);
77 #endif
78 
79 #ifdef BMCWEB_ENABLE_KVM
80     crow::obmc_kvm::requestRoutes(app);
81 #endif
82 
83 #ifdef BMCWEB_ENABLE_REDFISH
84     redfish::RedfishService redfish(app);
85 
86     // Create HttpClient instance and initialize Config
87     crow::HttpClient::getInstance();
88 
89     // Create EventServiceManager instance and initialize Config
90     redfish::EventServiceManager::getInstance();
91 
92 #ifdef BMCWEB_ENABLE_REDFISH_AGGREGATION
93     // Create RedfishAggregator instance and initialize Config
94     redfish::RedfishAggregator::getInstance();
95 #endif
96 #endif
97 
98 #ifdef BMCWEB_ENABLE_DBUS_REST
99     crow::dbus_monitor::requestRoutes(app);
100     crow::image_upload::requestRoutes(app);
101     crow::openbmc_mapper::requestRoutes(app);
102 #endif
103 
104 #ifdef BMCWEB_ENABLE_HOST_SERIAL_WEBSOCKET
105     crow::obmc_console::requestRoutes(app);
106 #endif
107 
108 #ifdef BMCWEB_ENABLE_VM_WEBSOCKET
109     crow::obmc_vm::requestRoutes(app);
110 #endif
111 
112 #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
113     crow::ibm_mc::requestRoutes(app);
114     crow::ibm_mc_lock::Lock::getInstance();
115 #endif
116 
117 #ifdef BMCWEB_ENABLE_GOOGLE_API
118     crow::google_api::requestRoutes(app);
119 #endif
120 
121     if (bmcwebInsecureDisableXssPrevention != 0)
122     {
123         cors_preflight::requestRoutes(app);
124     }
125 
126     crow::login_routes::requestRoutes(app);
127 
128     setupSocket(app);
129 
130 #ifdef BMCWEB_ENABLE_VM_NBDPROXY
131     crow::nbd_proxy::requestRoutes(app);
132 #endif
133 
134 #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
135     int rc = redfish::EventServiceManager::startEventLogMonitor(*io);
136     if (rc != 0)
137     {
138         BMCWEB_LOG_ERROR << "Redfish event handler setup failed...";
139         return rc;
140     }
141 #endif
142 
143 #ifdef BMCWEB_ENABLE_SSL
144     BMCWEB_LOG_INFO << "Start Hostname Monitor Service...";
145     crow::hostname_monitor::registerHostnameSignal();
146 #endif
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