diff --git a/examples/server/server.cpp b/examples/server/server.cpp index 1ebcb5085..b895b9196 100644 --- a/examples/server/server.cpp +++ b/examples/server/server.cpp @@ -3665,7 +3665,8 @@ int main(int argc, char ** argv) { res.status = 200; // HTTP OK }; - const auto handle_slots_save = [&ctx_server, &res_error, &res_ok, ¶ms](const httplib::Request & req, httplib::Response & res, int id_slot) { + const auto handle_slot_type = [&ctx_server, &res_error, &res_ok, ¶ms](const httplib::Request & req, + httplib::Response & res, int id_slot, server_task_type type) { json request_data = json::parse(req.body); std::string filename = request_data.at("filename"); if (!fs_validate_filename(filename)) { @@ -3674,7 +3675,7 @@ int main(int argc, char ** argv) { } std::string filepath = params.slot_save_path + filename; - server_task task(SERVER_TASK_TYPE_SLOT_SAVE); + server_task task(type); task.id = ctx_server.queue_tasks.get_new_id(); task.slot_action.slot_id = id_slot; task.slot_action.filename = filename; @@ -3691,37 +3692,18 @@ int main(int argc, char ** argv) { return; } + if (type == SERVER_TASK_TYPE_SLOT_SAVE) { + GGML_ASSERT(dynamic_cast(result.get()) != nullptr); + } res_ok(res, result->to_json()); }; - const auto handle_slots_restore = [&ctx_server, &res_error, &res_ok, ¶ms](const httplib::Request & req, httplib::Response & res, int id_slot) { - json request_data = json::parse(req.body); - std::string filename = request_data.at("filename"); - if (!fs_validate_filename(filename)) { - res_error(res, format_error_response("Invalid filename", ERROR_TYPE_INVALID_REQUEST)); - return; - } - std::string filepath = params.slot_save_path + filename; + const auto handle_slots_save = [&handle_slot_type](const httplib::Request & req, httplib::Response & res, int id_slot) { + handle_slot_type(req, res, id_slot, SERVER_TASK_TYPE_SLOT_SAVE); + }; - server_task task(SERVER_TASK_TYPE_SLOT_RESTORE); - task.id = ctx_server.queue_tasks.get_new_id(); - task.slot_action.slot_id = id_slot; - task.slot_action.filename = filename; - task.slot_action.filepath = filepath; - - ctx_server.queue_results.add_waiting_task_id(task.id); - ctx_server.queue_tasks.post(task); - - server_task_result_ptr result = ctx_server.queue_results.recv(task.id); - ctx_server.queue_results.remove_waiting_task_id(task.id); - - if (result->is_error()) { - res_error(res, result->to_json()); - return; - } - - GGML_ASSERT(dynamic_cast(result.get()) != nullptr); - res_ok(res, result->to_json()); + const auto handle_slots_restore = [&handle_slot_type](const httplib::Request & req, httplib::Response & res, int id_slot) { + handle_slot_type(req, res, id_slot, SERVER_TASK_TYPE_SLOT_RESTORE); }; const auto handle_slots_erase = [&ctx_server, &res_error, &res_ok](const httplib::Request & /* req */, httplib::Response & res, int id_slot) {