-
Notifications
You must be signed in to change notification settings - Fork 68
[develop] add create_snapshot() interface #387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| /* | ||
| Copyright The Overlaybd Authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| #include <photon/net/http/url.h> | ||
| #include <string_view> | ||
| #include "image_service.h" | ||
| #include "image_file.h" | ||
| #include "api_server.h" | ||
|
|
||
| int ApiHandler::handle_request(photon::net::http::Request& req, | ||
| photon::net::http::Response& resp, | ||
| std::string_view) { | ||
| auto target = req.target(); // string view, format: /snapshot?dev_id=${devID}&config=${config} | ||
| std::string_view query(""); | ||
| auto pos = target.find('?'); | ||
| if (pos != std::string_view::npos) { | ||
| query = target.substr(pos + 1); | ||
| } | ||
| LOG_DEBUG("Snapshot query: `", query); // string view, format: dev_id=${devID}&config=${config} | ||
| parse_params(query); | ||
| auto dev_id = params["dev_id"]; | ||
| auto config_path = params["config"]; | ||
| LOG_DEBUG("dev_id: `, config: `", dev_id, config_path); | ||
|
|
||
| int code; | ||
| std::string msg; | ||
| ImageFile* img_file = nullptr; | ||
|
|
||
| if (dev_id.empty() || config_path.empty()) { | ||
| code = 400; | ||
| msg = std::string(R"delimiter({ | ||
| "success": false, | ||
| "message": "Missing dev_id or config in snapshot request" | ||
| })delimiter"); | ||
| goto EXIT; | ||
| } | ||
|
|
||
| img_file = imgservice->find_image_file(dev_id); | ||
| if (!img_file) { | ||
| code = 404; | ||
| msg = std::string(R"delimiter({ | ||
| "success": false, | ||
| "message": "Image file not found" | ||
| })delimiter"); | ||
| goto EXIT; | ||
| } | ||
|
|
||
| if (img_file->create_snapshot(config_path.c_str()) < 0) { | ||
| code = 500; | ||
| msg = std::string(R"delimiter({ | ||
| "success": false, | ||
| "message": "Failed to create snapshot`" | ||
| })delimiter"); | ||
| goto EXIT; | ||
| } | ||
|
|
||
| code = 200; | ||
| msg = std::string(R"delimiter({ | ||
| "success": true, | ||
| "message": "Snapshot created successfully" | ||
| })delimiter"); | ||
|
|
||
| EXIT: | ||
| resp.set_result(code); | ||
| resp.headers.content_length(msg.size()); | ||
| resp.keep_alive(true); | ||
| auto ret_w = resp.write((void*)msg.c_str(), msg.size()); | ||
| if (ret_w != (ssize_t)msg.size()) { | ||
| LOG_ERRNO_RETURN(0, -1, "send body failed, target: `, `", req.target(), VALUE(ret_w)); | ||
| } | ||
| LOG_DEBUG("send body done"); | ||
| return 0; | ||
| } | ||
|
|
||
| void ApiHandler::parse_params(std::string_view query) { // format: dev_id=${devID}&config=${config}... | ||
| if (query.empty()) | ||
| return; | ||
|
|
||
| size_t start = 0; | ||
| while (start < query.length()) { | ||
| auto end = query.find('&', start); | ||
| if (end == std::string_view::npos) { // last one | ||
| end = query.length(); | ||
| } | ||
|
|
||
| auto param = query.substr(start, end - start); | ||
| auto eq_pos = param.find('='); | ||
| if (eq_pos != std::string_view::npos) { | ||
| auto key = param.substr(0, eq_pos); | ||
| auto value = param.substr(eq_pos + 1); | ||
|
|
||
| // url decode | ||
| auto decoded_key = photon::net::http::url_unescape(key); | ||
| auto decoded_value = photon::net::http::url_unescape(value); | ||
| params[decoded_key] = decoded_value; | ||
| } else { | ||
| // key without value | ||
| auto key = photon::net::http::url_unescape(param); | ||
| params[key] = ""; | ||
| } | ||
| start = end + 1; | ||
| } | ||
| } | ||
|
|
||
| ApiServer::ApiServer(const std::string &addr, ApiHandler* handler) { | ||
| photon::net::http::URL url(addr); | ||
| std::string host = url.host().data(); // the string pointed by data() doesn't end up with '\0' | ||
| auto pos = host.find(":"); | ||
| if (pos != host.npos) { | ||
| host.resize(pos); | ||
| } | ||
| tcpserver = photon::net::new_tcp_socket_server(); | ||
| tcpserver->setsockopt(SOL_SOCKET, SO_REUSEPORT, 1); | ||
| if(tcpserver->bind(url.port(), photon::net::IPAddr(host.c_str())) < 0) | ||
| LOG_ERRNO_RETURN(0, , "Failed to bind api server port `", url.port()); | ||
| if(tcpserver->listen() < 0) | ||
| LOG_ERRNO_RETURN(0, , "Failed to listen api server port `", url.port()); | ||
| httpserver = photon::net::http::new_http_server(); | ||
| httpserver->add_handler(handler, false, "/snapshot"); | ||
| tcpserver->set_handler(httpserver->get_connection_handler()); | ||
| tcpserver->start_loop(); | ||
| ready = true; | ||
| LOG_DEBUG("Api server listening on `:`, path: `", host, url.port(), "/snapshot"); | ||
| } | ||
|
|
||
| ApiServer::~ApiServer() { | ||
| delete tcpserver; | ||
| delete httpserver; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| Copyright The Overlaybd Authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <map> | ||
| #include <string> | ||
| #include <photon/net/http/server.h> | ||
| #include <photon/net/socket.h> | ||
|
|
||
| class ImageService; | ||
|
|
||
| class ApiHandler : public photon::net::http::HTTPHandler { | ||
| public: | ||
| ImageService *imgservice; | ||
| std::map<std::string, std::string> params; | ||
|
|
||
| ApiHandler(ImageService *imgservice) : imgservice(imgservice) {} | ||
| int handle_request(photon::net::http::Request& req, | ||
| photon::net::http::Response& resp, | ||
| std::string_view) override; | ||
| void parse_params(std::string_view query); | ||
| }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's not necessary to expose There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you suggesting that I should define ApiHandler in the cpp file instead of the header, and then instantiate it inside the ApiServer constructor?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Exactly. |
||
|
|
||
| struct ApiServer { | ||
| photon::net::ISocketServer* tcpserver = nullptr; | ||
| photon::net::http::HTTPServer* httpserver = nullptr; | ||
| bool ready = false; | ||
|
|
||
| ApiServer(const std::string &addr, ApiHandler* handler); | ||
| ~ApiServer(); | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
req.query()instead.