You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.0 KiB
102 lines
3.0 KiB
#include <http.hpp> |
|
#include <json_converters.hpp> |
|
#include <store.hpp> |
|
|
|
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<HttpsServer>(https["cert"], https["key"]); |
|
} |
|
} |
|
|
|
if (https_server) { |
|
convert::from_json(cfg.value("https", json::object()), |
|
https_server->config); |
|
} |
|
} |
|
|
|
int Application::run() { |
|
auto store = json::object(); |
|
std::vector<std::thread> servers; |
|
|
|
if (https_server) { |
|
https_server->default_resource["GET"] = |
|
[&](std::shared_ptr<HttpsServer::Response> response, |
|
std::shared_ptr<HttpsServer::Request>) { |
|
response->write(Status::success_ok, store.dump(), |
|
{header_access_control, header_application_data}); |
|
}; |
|
|
|
https_server->default_resource["POST"] = |
|
[&](std::shared_ptr<HttpsServer::Response> response, |
|
std::shared_ptr<HttpsServer::Request> request) { |
|
json data; |
|
try { |
|
request->content >> data; |
|
} catch (...) { |
|
return response::bad_json(response); |
|
} |
|
if (data.is_object()) { |
|
store.update(data); |
|
return response->write(Status::success_no_content); |
|
} |
|
return response->write(Status::client_error_bad_request); |
|
}; |
|
|
|
servers.emplace_back( |
|
[&]() { https_server->start(secure_web_server_started); }); |
|
} |
|
|
|
http_server.default_resource["GET"] = |
|
[&](std::shared_ptr<HttpServer::Response> response, ...) { |
|
if (https_server) { |
|
return response::https_required(response); |
|
} |
|
response->write(Status::success_ok, store.dump(), |
|
{header_access_control, header_application_data}); |
|
}; |
|
|
|
http_server.default_resource["POST"] = |
|
[&](std::shared_ptr<HttpServer::Response> response, |
|
std::shared_ptr<HttpServer::Request> request) { |
|
if (https_server) { |
|
return response::https_required(response); |
|
} |
|
|
|
json data; |
|
try { |
|
request->content >> data; |
|
} catch (...) { |
|
return response::bad_json(response); |
|
} |
|
if (data.is_object()) { |
|
store.update(data); |
|
return response->write(Status::success_no_content); |
|
} |
|
return response->write(Status::client_error_bad_request); |
|
}; |
|
|
|
|
|
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; |
|
}
|
|
|