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