xref: /openbmc/bmcweb/src/webserver_main.cpp (revision 844b4152)
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 <obmc_console.hpp>
10 #include <openbmc_dbus_rest.hpp>
11 
12 #include <memory>
13 #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
14 #include <ibm/management_console_rest.hpp>
15 #endif
16 #include <persistent_data_middleware.hpp>
17 #include <redfish.hpp>
18 #include <redfish_v1.hpp>
19 #include <sdbusplus/asio/connection.hpp>
20 #include <sdbusplus/bus.hpp>
21 #include <sdbusplus/server.hpp>
22 #include <security_headers_middleware.hpp>
23 #include <ssl_key_handler.hpp>
24 #include <token_authorization_middleware.hpp>
25 #include <vm_websocket.hpp>
26 #include <webassets.hpp>
27 #include <webserver_common.hpp>
28 
29 #include <string>
30 
31 #ifdef BMCWEB_ENABLE_VM_NBDPROXY
32 #include <nbd_proxy.hpp>
33 #endif
34 
35 constexpr int defaultPort = 18080;
36 
37 template <typename... Middlewares>
38 void setupSocket(crow::Crow<Middlewares...>& app)
39 {
40     int listenFd = sd_listen_fds(0);
41     if (1 == listenFd)
42     {
43         BMCWEB_LOG_INFO << "attempting systemd socket activation";
44         if (sd_is_socket_inet(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_STREAM, 1,
45                               0))
46         {
47             BMCWEB_LOG_INFO << "Starting webserver on socket handle "
48                             << SD_LISTEN_FDS_START;
49             app.socket(SD_LISTEN_FDS_START);
50         }
51         else
52         {
53             BMCWEB_LOG_INFO
54                 << "bad incoming socket, starting webserver on port "
55                 << defaultPort;
56             app.port(defaultPort);
57         }
58     }
59     else
60     {
61         BMCWEB_LOG_INFO << "Starting webserver on port " << defaultPort;
62         app.port(defaultPort);
63     }
64 }
65 
66 int main(int argc, char** argv)
67 {
68     crow::logger::setLogLevel(crow::LogLevel::Debug);
69 
70     auto io = std::make_shared<boost::asio::io_context>();
71     CrowApp app(io);
72 
73     // Static assets need to be initialized before Authorization, because auth
74     // needs to build the whitelist from the static routes
75 
76 #ifdef BMCWEB_ENABLE_STATIC_HOSTING
77     crow::webassets::requestRoutes(app);
78 #endif
79 
80 #ifdef BMCWEB_ENABLE_KVM
81     crow::obmc_kvm::requestRoutes(app);
82 #endif
83 
84 #ifdef BMCWEB_ENABLE_REDFISH
85     crow::redfish::requestRoutes(app);
86 #endif
87 
88 #ifdef BMCWEB_ENABLE_DBUS_REST
89     crow::dbus_monitor::requestRoutes(app);
90     crow::image_upload::requestRoutes(app);
91     crow::openbmc_mapper::requestRoutes(app);
92 #endif
93 
94 #ifdef BMCWEB_ENABLE_HOST_SERIAL_WEBSOCKET
95     crow::obmc_console::requestRoutes(app);
96 #endif
97 
98 #ifdef BMCWEB_ENABLE_VM_WEBSOCKET
99     crow::obmc_vm::requestRoutes(app);
100 #endif
101 
102 #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
103     crow::ibm_mc::requestRoutes(app);
104     crow::ibm_mc_lock::Lock::getInstance();
105 #endif
106 
107     crow::token_authorization::requestRoutes(app);
108 
109     BMCWEB_LOG_INFO << "bmcweb (" << __DATE__ << ": " << __TIME__ << ')';
110     setupSocket(app);
111 
112     crow::connections::systemBus =
113         std::make_shared<sdbusplus::asio::connection>(*io);
114 
115 #ifdef BMCWEB_ENABLE_VM_NBDPROXY
116     crow::nbd_proxy::requestRoutes(app);
117 #endif
118 
119     redfish::RedfishService redfish(app);
120 
121     app.run();
122     io->run();
123 
124     crow::connections::systemBus.reset();
125 }
126