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