xref: /openbmc/bmcweb/src/webserver_main.cpp (revision 262d7d4b)
1 #include <systemd/sd-daemon.h>
2 
3 #include <app.hpp>
4 #include <boost/asio/io_context.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 <obmc_console.hpp>
13 #include <openbmc_dbus_rest.hpp>
14 #include <redfish.hpp>
15 #include <redfish_v1.hpp>
16 #include <sdbusplus/asio/connection.hpp>
17 #include <sdbusplus/bus.hpp>
18 #include <sdbusplus/server.hpp>
19 #include <security_headers.hpp>
20 #include <ssl_key_handler.hpp>
21 #include <vm_websocket.hpp>
22 #include <webassets.hpp>
23 
24 #include <memory>
25 #include <string>
26 
27 #ifdef BMCWEB_ENABLE_VM_NBDPROXY
28 #include <nbd_proxy.hpp>
29 #endif
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))
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 int main(int /*argc*/, char** /*argv*/)
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     // Static assets need to be initialized before Authorization, because auth
69     // needs to build the whitelist from the static routes
70 
71 #ifdef BMCWEB_ENABLE_STATIC_HOSTING
72     crow::webassets::requestRoutes(app);
73 #endif
74 
75 #ifdef BMCWEB_ENABLE_KVM
76     crow::obmc_kvm::requestRoutes(app);
77 #endif
78 
79 #ifdef BMCWEB_ENABLE_REDFISH
80     crow::redfish::requestRoutes(app);
81 #endif
82 
83 #ifdef BMCWEB_ENABLE_DBUS_REST
84     crow::dbus_monitor::requestRoutes(app);
85     crow::image_upload::requestRoutes(app);
86     crow::openbmc_mapper::requestRoutes(app);
87 #endif
88 
89 #ifdef BMCWEB_ENABLE_HOST_SERIAL_WEBSOCKET
90     crow::obmc_console::requestRoutes(app);
91 #endif
92 
93 #ifdef BMCWEB_ENABLE_VM_WEBSOCKET
94     crow::obmc_vm::requestRoutes(app);
95 #endif
96 
97 #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
98     crow::ibm_mc::requestRoutes(app);
99     crow::ibm_mc_lock::Lock::getInstance();
100 #endif
101 
102 #ifdef BMCWEB_INSECURE_DISABLE_XSS_PREVENTION
103     cors_preflight::requestRoutes(app);
104 #endif
105 
106     crow::login_routes::requestRoutes(app);
107 
108     setupSocket(app);
109 
110     crow::connections::systemBus =
111         std::make_shared<sdbusplus::asio::connection>(*io);
112 
113 #ifdef BMCWEB_ENABLE_VM_NBDPROXY
114     crow::nbd_proxy::requestRoutes(app);
115 #endif
116 
117     redfish::RedfishService redfish(app);
118 
119 #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
120     int rc = redfish::EventServiceManager::startEventLogMonitor(*io);
121     if (rc)
122     {
123         BMCWEB_LOG_ERROR << "Redfish event handler setup failed...";
124         return rc;
125     }
126 #endif
127 
128 #ifdef BMCWEB_ENABLE_SSL
129     BMCWEB_LOG_INFO << "Start Hostname Monitor Service...";
130     crow::hostname_monitor::registerHostnameSignal();
131 #endif
132 
133     app.run();
134     io->run();
135 
136     crow::connections::systemBus.reset();
137 }
138