From 7cb0dc1f29bd8070c5b90b2d810bb549ae2e2745 Mon Sep 17 00:00:00 2001 From: Andrej Babic Date: Thu, 9 Apr 2020 13:22:54 +0200 Subject: [PATCH] Fix REST api declaration --- core-writer/src/RestApi.cpp | 103 ++++++++++++++++++++---------------- 1 file changed, 58 insertions(+), 45 deletions(-) diff --git a/core-writer/src/RestApi.cpp b/core-writer/src/RestApi.cpp index d647ae6..202f58c 100644 --- a/core-writer/src/RestApi.cpp +++ b/core-writer/src/RestApi.cpp @@ -17,67 +17,80 @@ void RestApi::start_rest_api(ProcessManager& manager, uint16_t port) crow::SimpleApp app; - CROW_ROUTE (app, "/writing").methods("POST"_method) + CROW_ROUTE (app, "/writing").methods("POST"_method, "DELETE"_method) ([&](const crow::request& req) { - auto request_parameters = crow::json::load(req.body); + if (req.method == "POST"_method) { + auto request_parameters = crow::json::load(req.body); - string output_file = request_parameters["output_file"].s(); - int n_frames = request_parameters["n_frames"].i(); - int user_id = request_parameters["user_id"].i(); + string output_file = + request_parameters["output_file"].s(); + int n_frames = + request_parameters["n_frames"].i(); + int user_id = + request_parameters["user_id"].i(); - manager.start_writing(output_file, n_frames, user_id); + manager.start_writing(output_file, n_frames, user_id); - crow::json::wvalue result; - result["state"] = "ok"; - result["status"] = manager.get_status(); - result["message"] = "Writing started."; + crow::json::wvalue result; + result["state"] = "ok"; + result["status"] = manager.get_status(); + result["message"] = "Writing started."; + + return result; + } + + if (req.method == "DELETE"_method) { + manager.stop_writing(); + + crow::json::wvalue result; + result["state"] = "ok"; + result["status"] = manager.get_status(); + result["message"] = "Writing stopped."; + + return result; + } + + throw runtime_error("You should not see this."); - return result; }); - CROW_ROUTE(app, "/writing").methods("DELETE"_method) - ([&](const crow::request& req){ - manager.stop_writing(); - - crow::json::wvalue result; - result["state"] = "ok"; - result["status"] = manager.get_status(); - result["message"] = "Writing stopped."; - - return result; - }); - - CROW_ROUTE (app, "/receiving").methods("POST"_method) + CROW_ROUTE (app, "/receiving").methods("POST"_method, "DELETE"_method) ([&](const crow::request& req) { - auto request_parameters = crow::json::load(req.body); + if (req.method == "POST"_method) { - string url = request_parameters["connect_address"].s(); - int n_threads = request_parameters["n_receiving_threads"].i(); + auto request_parameters = crow::json::load(req.body); - manager.start_receiving(url, n_threads); + string url = + request_parameters["connect_address"].s(); + int n_threads = + request_parameters["n_receiving_threads"].i(); - crow::json::wvalue result; - result["state"] = "ok"; - result["status"] = manager.get_status(); - result["message"] = "Receiving started."; + manager.start_receiving(url, n_threads); - return result; + crow::json::wvalue result; + result["state"] = "ok"; + result["status"] = manager.get_status(); + result["message"] = "Receiving started."; + + return result; + } + + if (req.method == "DELETE"_method) { + manager.stop_receiving(); + + crow::json::wvalue result; + result["state"] = "ok"; + result["status"] = manager.get_status(); + result["message"] = "Receiving stopped."; + + return result; + } + + throw runtime_error("You should not see this."); }); - CROW_ROUTE(app, "/receiving").methods("DELETE"_method) - ([&](const crow::request& req){ - manager.stop_receiving(); - - crow::json::wvalue result; - result["state"] = "ok"; - result["status"] = manager.get_status(); - result["message"] = "Receiving stopped."; - - return result; - }); - CROW_ROUTE (app, "/status") ([&](){ crow::json::wvalue result;