#include #include #include void Application::web_server_started(std::size_t port) { std::cout << "Web server started and listening on " << port << ".\n"; }; void Application::secure_web_server_started(std::size_t port) { std::cout << "Secure web server started and listening on " << port << ".\n"; }; Application::Application(const nlohmann::json &cfg) { convert::from_json(cfg.value("http", json::object()), http_server.config); if (cfg.contains("https")) { const auto https = cfg["https"]; if (https.contains("cert") && https.contains("key")) { https_server = std::make_shared(https["cert"], https["key"]); } } if (https_server) { convert::from_json(cfg.value("https", json::object()), https_server->config); } } int Application::run() { std::vector servers; for (const auto &m : {"GET", "POST", "PUT", "PATCH", "OPTIONS", "HEAD"}) { if (https_server) { https_server->default_resource[m] = [](std::shared_ptr response, ...) { return response::not_found(response); }; } http_server.default_resource[m] = [](std::shared_ptr response, ...) { return response::not_found(response); }; } if (https_server) { servers.emplace_back( [&]() { https_server->start(secure_web_server_started); }); } http_server.resource["^/orders/?$"]["GET"] = [&](std::shared_ptr response, ...) { response->write(Status::success_ok, dat.dump(), {header_access_control, header_application_data}); }; servers.emplace_back([&]() { http_server.start(web_server_started); }); for (auto &server : servers) { server.join(); } return 0; } Application &Application::get(const json &config) { static Application app(config); return app; }