xref: /openbmc/bmcweb/src/webserver_main.cpp (revision 7d95f5f6db1df37fe6438aa0e87479275c82807a)
1 #include <systemd/sd-daemon.h>
2 #include <dbus/connection.hpp>
3 #include <dbus_monitor.hpp>
4 #include <dbus_singleton.hpp>
5 #include <intel_oem.hpp>
6 #include <openbmc_dbus_rest.hpp>
7 #include <persistent_data_middleware.hpp>
8 #include <redfish_v1.hpp>
9 #include <security_headers_middleware.hpp>
10 #include <ssl_key_handler.hpp>
11 #include <token_authorization_middleware.hpp>
12 #include <web_kvm.hpp>
13 #include <webassets.hpp>
14 #include <memory>
15 #include <string>
16 #include "redfish.hpp"
17 #include "webserver_common.hpp"
18 #include <crow/app.h>
19 #include <boost/asio.hpp>
20 
21 constexpr int defaultPort = 18080;
22 
23 template <typename... Middlewares>
24 void setup_socket(crow::Crow<Middlewares...>& app) {
25   int listen_fd = sd_listen_fds(0);
26   if (1 == listen_fd) {
27     CROW_LOG_INFO << "attempting systemd socket activation";
28     if (sd_is_socket_inet(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_STREAM, 1, 0)) {
29       CROW_LOG_INFO << "Starting webserver on socket handle "
30                     << SD_LISTEN_FDS_START;
31       app.socket(SD_LISTEN_FDS_START);
32     } else {
33       CROW_LOG_INFO << "bad incoming socket, starting webserver on port "
34                     << defaultPort;
35       app.port(defaultPort);
36     }
37   } else {
38     CROW_LOG_INFO << "Starting webserver on port " << defaultPort;
39     app.port(defaultPort);
40   }
41 }
42 
43 int main(int argc, char** argv) {
44   auto io = std::make_shared<boost::asio::io_service>();
45   crow::PersistentData::session_store =
46       std::make_shared<crow::PersistentData::SessionStore>();
47   CrowApp app(io);
48 
49 #ifdef CROW_ENABLE_SSL
50   std::string ssl_pem_file("server.pem");
51   std::cout << "Building SSL context\n";
52 
53   ensuressl::ensure_openssl_key_present_and_valid(ssl_pem_file);
54   std::cout << "SSL Enabled\n";
55   auto ssl_context = ensuressl::get_ssl_context(ssl_pem_file);
56   app.ssl(std::move(ssl_context));
57 #endif
58   // Static assets need to be initialized before Authorization, because auth
59   // needs to build the whitelist from the static routes
60   crow::webassets::request_routes(app);
61   crow::TokenAuthorization::request_routes(app);
62 
63   crow::kvm::request_routes(app);
64   crow::redfish::request_routes(app);
65   crow::dbus_monitor::request_routes(app);
66   crow::intel_oem::request_routes(app);
67   crow::openbmc_mapper::request_routes(app);
68 
69   crow::logger::setLogLevel(crow::LogLevel::INFO);
70 
71   CROW_LOG_INFO << "bmcweb (" << __DATE__ << ": " << __TIME__ << ')';
72   setup_socket(app);
73 
74   // Start dbus connection
75   crow::connections::system_bus =
76       std::make_shared<dbus::connection>(*io, dbus::bus::system);
77 
78   redfish::RedfishService redfish(app);
79 
80   app.run();
81   io->run();
82 }
83