diff --git a/src/main.cpp b/src/main.cpp index be6b59b..066ff9e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -81,19 +81,21 @@ class ServerResources { std::unique_ptr api; http::auth::AuthProvider auth{}; - void doTerminaFwUpdate(const http::server::Request& req) { + static void onUploadFirmware(const http::server::Request& req) { std::ofstream f("/tmp/firmware.zip", std::ios::binary); if (f.is_open()) { f.write(req.payload.data(), static_cast(req.payload.size())); f.close(); - - system("do-terminal-update.sh"); } else { throw std::runtime_error("File is not open"); } } + static void doTerminalUpgrade() { + system("do-terminal-update.sh"); + } + public: static constexpr const char* INDEX_HTML = "static/main.html"; static constexpr const char* LOGIN_HTML = "static/login.html"; @@ -409,7 +411,7 @@ public: if (req.method != "PUT") { http::server::stockReply(http::server::bad_request, rep); } - this->doTerminaFwUpdate(req); + onUploadFirmware(req); rep.status = http::server::ok; rep.headers.clear(); @@ -421,6 +423,18 @@ public: result += "\"}"; rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); })); + + s.resources.emplace_back(std::make_unique("/api/doFirmwareUpgrade", this->auth, http::auth::User::UPDATE_FIRMWARE, [this](const auto& req, auto& rep) { + if (req.method != "POST") { + http::server::stockReply(http::server::bad_request, rep); + } + doTerminalUpgrade(); + rep.status = http::server::ok; + rep.headers.clear(); + rep.headers.push_back({.name = "Content-Type", .value = toString(mime_types::json)}); + const auto result = api->loadFirmwareVersion(); + rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); + })); } ~ServerResources() = default; diff --git a/static/main.html b/static/main.html index 9b50db1..865e54f 100644 --- a/static/main.html +++ b/static/main.html @@ -629,7 +629,8 @@ SHA256: {{ uploadFw.sha256 }} - + +