Ed Tanous | 9e6e1b2 | 2018-03-16 13:08:50 -0700 | [diff] [blame] | 1 | #include <systemd/sd-daemon.h> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 2 | #include <dbus_monitor.hpp> |
| 3 | #include <dbus_singleton.hpp> |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame^] | 4 | #include <image_upload.hpp> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 5 | #include <openbmc_dbus_rest.hpp> |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 6 | #include <persistent_data_middleware.hpp> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 7 | #include <redfish_v1.hpp> |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 8 | #include <sdbusplus/asio/connection.hpp> |
| 9 | #include <sdbusplus/bus.hpp> |
| 10 | #include <sdbusplus/server.hpp> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 11 | #include <security_headers_middleware.hpp> |
| 12 | #include <ssl_key_handler.hpp> |
| 13 | #include <token_authorization_middleware.hpp> |
| 14 | #include <web_kvm.hpp> |
| 15 | #include <webassets.hpp> |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 16 | #include <memory> |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 17 | #include <string> |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 18 | #include "redfish.hpp" |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 19 | #include "webserver_common.hpp" |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 20 | #include <crow/app.h> |
| 21 | #include <boost/asio.hpp> |
Ed Tanous | cc5a37f | 2017-05-11 10:27:23 -0700 | [diff] [blame] | 22 | |
Vernon Mauery | 168792a | 2018-01-26 13:42:54 -0800 | [diff] [blame] | 23 | constexpr int defaultPort = 18080; |
| 24 | |
| 25 | template <typename... Middlewares> |
| 26 | void setup_socket(crow::Crow<Middlewares...>& app) { |
| 27 | int listen_fd = sd_listen_fds(0); |
| 28 | if (1 == listen_fd) { |
| 29 | CROW_LOG_INFO << "attempting systemd socket activation"; |
| 30 | if (sd_is_socket_inet(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_STREAM, 1, 0)) { |
| 31 | CROW_LOG_INFO << "Starting webserver on socket handle " |
| 32 | << SD_LISTEN_FDS_START; |
| 33 | app.socket(SD_LISTEN_FDS_START); |
| 34 | } else { |
| 35 | CROW_LOG_INFO << "bad incoming socket, starting webserver on port " |
Ed Tanous | 9e6e1b2 | 2018-03-16 13:08:50 -0700 | [diff] [blame] | 36 | << defaultPort; |
Vernon Mauery | 168792a | 2018-01-26 13:42:54 -0800 | [diff] [blame] | 37 | app.port(defaultPort); |
| 38 | } |
| 39 | } else { |
| 40 | CROW_LOG_INFO << "Starting webserver on port " << defaultPort; |
| 41 | app.port(defaultPort); |
| 42 | } |
| 43 | } |
| 44 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 45 | int main(int argc, char** argv) { |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame^] | 46 | crow::logger::setLogLevel(crow::LogLevel::DEBUG); |
Lewanczyk, Dawid | 08777fb | 2018-03-22 23:33:49 +0100 | [diff] [blame] | 47 | |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 48 | auto io = std::make_shared<boost::asio::io_service>(); |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 49 | crow::PersistentData::session_store = |
| 50 | std::make_shared<crow::PersistentData::SessionStore>(); |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 51 | CrowApp app(io); |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 52 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 53 | #ifdef CROW_ENABLE_SSL |
| 54 | std::string ssl_pem_file("server.pem"); |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 55 | std::cout << "Building SSL context\n"; |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 56 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 57 | ensuressl::ensure_openssl_key_present_and_valid(ssl_pem_file); |
| 58 | std::cout << "SSL Enabled\n"; |
| 59 | auto ssl_context = ensuressl::get_ssl_context(ssl_pem_file); |
| 60 | app.ssl(std::move(ssl_context)); |
| 61 | #endif |
| 62 | // Static assets need to be initialized before Authorization, because auth |
| 63 | // needs to build the whitelist from the static routes |
| 64 | crow::webassets::request_routes(app); |
| 65 | crow::TokenAuthorization::request_routes(app); |
Ed Tanous | f0226cd | 2017-05-16 12:35:38 -0700 | [diff] [blame] | 66 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 67 | crow::kvm::request_routes(app); |
| 68 | crow::redfish::request_routes(app); |
| 69 | crow::dbus_monitor::request_routes(app); |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame^] | 70 | crow::image_upload::requestRoutes(app); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 71 | crow::openbmc_mapper::request_routes(app); |
| 72 | |
Vernon Mauery | 168792a | 2018-01-26 13:42:54 -0800 | [diff] [blame] | 73 | CROW_LOG_INFO << "bmcweb (" << __DATE__ << ": " << __TIME__ << ')'; |
| 74 | setup_socket(app); |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 75 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 76 | crow::connections::system_bus = |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 77 | std::make_shared<sdbusplus::asio::connection>(*io); |
Borawski.Lukasz | b6df6dc | 2018-01-24 10:20:45 +0100 | [diff] [blame] | 78 | redfish::RedfishService redfish(app); |
| 79 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 80 | app.run(); |
Ed Tanous | 9e6e1b2 | 2018-03-16 13:08:50 -0700 | [diff] [blame] | 81 | io->run(); |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 82 | |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame^] | 83 | crow::connections::system_bus.reset(); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 84 | } |