xref: /openbmc/bmcweb/src/webserver_main.cpp (revision a8d8f9d8)
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 "hostname_monitor.hpp"
8 #include "ibm/management_console_rest.hpp"
9 #include "image_upload.hpp"
10 #include "kvm_websocket.hpp"
11 #include "login_routes.hpp"
12 #include "nbd_proxy.hpp"
13 #include "obmc_console.hpp"
14 #include "openbmc_dbus_rest.hpp"
15 #include "redfish.hpp"
16 #include "redfish_aggregator.hpp"
17 #include "security_headers.hpp"
18 #include "ssl_key_handler.hpp"
19 #include "vm_websocket.hpp"
20 #include "webassets.hpp"
21 
22 #include <systemd/sd-daemon.h>
23 
24 #include <boost/asio/io_context.hpp>
25 #include <google/google_service_root.hpp>
26 #include <sdbusplus/asio/connection.hpp>
27 #include <sdbusplus/bus.hpp>
28 #include <sdbusplus/server.hpp>
29 
30 #include <exception>
31 #include <memory>
32 #include <string>
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     sdbusplus::asio::connection systemBus(*io);
72     crow::connections::systemBus = &systemBus;
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 = nullptr;
154 
155     return 0;
156 }
157 
158 int main(int /*argc*/, char** /*argv*/)
159 {
160     try
161     {
162         return run();
163     }
164     catch (const std::exception& e)
165     {
166         BMCWEB_LOG_CRITICAL << "Threw exception to main: " << e.what();
167         return -1;
168     }
169     catch (...)
170     {
171         BMCWEB_LOG_CRITICAL << "Threw exception to main";
172         return -1;
173     }
174 }
175