xref: /openbmc/bmcweb/src/webserver_main.cpp (revision 118b1c71)
1 #include <crow/app.h>
2 #include <systemd/sd-daemon.h>
3 
4 #include <bmcweb/settings.hpp>
5 #include <boost/asio.hpp>
6 #include <dbus_monitor.hpp>
7 #include <dbus_singleton.hpp>
8 #include <image_upload.hpp>
9 #include <memory>
10 #include <obmc_console.hpp>
11 #include <openbmc_dbus_rest.hpp>
12 #include <persistent_data_middleware.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_middleware.hpp>
19 #include <ssl_key_handler.hpp>
20 #include <string>
21 #include <token_authorization_middleware.hpp>
22 #include <web_kvm.hpp>
23 #include <webassets.hpp>
24 #include <webserver_common.hpp>
25 
26 constexpr int defaultPort = 18080;
27 
28 template <typename... Middlewares>
29 void setupSocket(crow::Crow<Middlewares...>& app)
30 {
31     int listenFd = sd_listen_fds(0);
32     if (1 == listenFd)
33     {
34         BMCWEB_LOG_INFO << "attempting systemd socket activation";
35         if (sd_is_socket_inet(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_STREAM, 1,
36                               0))
37         {
38             BMCWEB_LOG_INFO << "Starting webserver on socket handle "
39                             << SD_LISTEN_FDS_START;
40             app.socket(SD_LISTEN_FDS_START);
41         }
42         else
43         {
44             BMCWEB_LOG_INFO
45                 << "bad incoming socket, starting webserver on port "
46                 << defaultPort;
47             app.port(defaultPort);
48         }
49     }
50     else
51     {
52         BMCWEB_LOG_INFO << "Starting webserver on port " << defaultPort;
53         app.port(defaultPort);
54     }
55 }
56 
57 int main(int argc, char** argv)
58 {
59     crow::logger::setLogLevel(crow::LogLevel::DEBUG);
60 
61     auto io = std::make_shared<boost::asio::io_service>();
62     CrowApp app(io);
63 
64 #ifdef BMCWEB_ENABLE_SSL
65     std::string sslPemFile("server.pem");
66     std::cout << "Building SSL Context\n";
67 
68     ensuressl::ensureOpensslKeyPresentAndValid(sslPemFile);
69     std::cout << "SSL Enabled\n";
70     auto sslContext = ensuressl::getSslContext(sslPemFile);
71     app.ssl(std::move(sslContext));
72 #endif
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::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     crow::token_authorization::requestRoutes(app);
99 
100     BMCWEB_LOG_INFO << "bmcweb (" << __DATE__ << ": " << __TIME__ << ')';
101     setupSocket(app);
102 
103     crow::connections::systemBus =
104         std::make_shared<sdbusplus::asio::connection>(*io);
105     redfish::RedfishService redfish(app);
106 
107     app.run();
108     io->run();
109 
110     crow::connections::systemBus.reset();
111 }
112