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