xref: /openbmc/bmcweb/src/webserver_main.cpp (revision a778c026)
1 #include <app.h>
2 #include <systemd/sd-daemon.h>
3 
4 #include <boost/asio/io_context.hpp>
5 #include <dbus_monitor.hpp>
6 #include <dbus_singleton.hpp>
7 #include <image_upload.hpp>
8 #include <kvm_websocket.hpp>
9 #include <memory>
10 #include <obmc_console.hpp>
11 #include <openbmc_dbus_rest.hpp>
12 #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
13 #include <ibm/management_console_rest.hpp>
14 #endif
15 #include <persistent_data_middleware.hpp>
16 #include <redfish.hpp>
17 #include <redfish_v1.hpp>
18 #include <sdbusplus/asio/connection.hpp>
19 #include <sdbusplus/bus.hpp>
20 #include <sdbusplus/server.hpp>
21 #include <security_headers_middleware.hpp>
22 #include <ssl_key_handler.hpp>
23 #include <string>
24 #include <token_authorization_middleware.hpp>
25 #include <vm_websocket.hpp>
26 #include <webassets.hpp>
27 #include <webserver_common.hpp>
28 
29 #ifdef BMCWEB_ENABLE_VM_NBDPROXY
30 #include <nbd_proxy.hpp>
31 #endif
32 
33 constexpr int defaultPort = 18080;
34 
35 template <typename... Middlewares>
36 void setupSocket(crow::Crow<Middlewares...>& 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     CrowApp app(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     crow::redfish::requestRoutes(app);
84 #endif
85 
86 #ifdef BMCWEB_ENABLE_DBUS_REST
87     crow::dbus_monitor::requestRoutes(app);
88     crow::image_upload::requestRoutes(app);
89     crow::openbmc_mapper::requestRoutes(app);
90 #endif
91 
92 #ifdef BMCWEB_ENABLE_HOST_SERIAL_WEBSOCKET
93     crow::obmc_console::requestRoutes(app);
94 #endif
95 
96 #ifdef BMCWEB_ENABLE_VM_WEBSOCKET
97     crow::obmc_vm::requestRoutes(app);
98 #endif
99 
100 #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
101     crow::ibm_mc::requestRoutes(app);
102     crow::ibm_mc_lock::Lock::getInstance();
103 #endif
104 
105     crow::token_authorization::requestRoutes(app);
106 
107     BMCWEB_LOG_INFO << "bmcweb (" << __DATE__ << ": " << __TIME__ << ')';
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     app.run();
129     io->run();
130 
131     crow::connections::systemBus.reset();
132 }
133