Skip to content

Commit 2d2d076

Browse files
committed
server : extract handle_slot_erase to handle_slot_impl
This commit moves the code for handling slot erase to the handle_slot_impl function. This is done to avoid code duplication and to make the code more readable.
1 parent 7c34af4 commit 2d2d076

File tree

1 file changed

+17
-29
lines changed

1 file changed

+17
-29
lines changed

examples/server/server.cpp

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3667,19 +3667,21 @@ int main(int argc, char ** argv) {
36673667

36683668
const auto handle_slot_impl = [&ctx_server, &res_error, &res_ok, &params](const httplib::Request & req,
36693669
httplib::Response & res, int id_slot, server_task_type type) {
3670-
json request_data = json::parse(req.body);
3671-
std::string filename = request_data.at("filename");
3672-
if (!fs_validate_filename(filename)) {
3673-
res_error(res, format_error_response("Invalid filename", ERROR_TYPE_INVALID_REQUEST));
3674-
return;
3675-
}
3676-
std::string filepath = params.slot_save_path + filename;
3677-
36783670
server_task task(type);
36793671
task.id = ctx_server.queue_tasks.get_new_id();
36803672
task.slot_action.slot_id = id_slot;
3681-
task.slot_action.filename = filename;
3682-
task.slot_action.filepath = filepath;
3673+
3674+
if (type != SERVER_TASK_TYPE_SLOT_ERASE) {
3675+
json request_data = json::parse(req.body);
3676+
std::string filename = request_data.at("filename");
3677+
if (!fs_validate_filename(filename)) {
3678+
res_error(res, format_error_response("Invalid filename", ERROR_TYPE_INVALID_REQUEST));
3679+
return;
3680+
}
3681+
std::string filepath = params.slot_save_path + filename;
3682+
task.slot_action.filename = filename;
3683+
task.slot_action.filepath = filepath;
3684+
}
36833685

36843686
ctx_server.queue_results.add_waiting_task_id(task.id);
36853687
ctx_server.queue_tasks.post(task);
@@ -3694,6 +3696,8 @@ int main(int argc, char ** argv) {
36943696

36953697
if (type == SERVER_TASK_TYPE_SLOT_SAVE) {
36963698
GGML_ASSERT(dynamic_cast<server_task_result_slot_save_load*>(result.get()) != nullptr);
3699+
} else if (type == SERVER_TASK_TYPE_SLOT_ERASE) {
3700+
GGML_ASSERT(dynamic_cast<server_task_result_slot_erase*>(result.get()) != nullptr);
36973701
}
36983702
res_ok(res, result->to_json());
36993703
};
@@ -3706,27 +3710,11 @@ int main(int argc, char ** argv) {
37063710
handle_slot_impl(req, res, id_slot, SERVER_TASK_TYPE_SLOT_RESTORE);
37073711
};
37083712

3709-
const auto handle_slots_erase = [&ctx_server, &res_error, &res_ok](const httplib::Request & /* req */, httplib::Response & res, int id_slot) {
3710-
server_task task(SERVER_TASK_TYPE_SLOT_ERASE);
3711-
task.id = ctx_server.queue_tasks.get_new_id();
3712-
task.slot_action.slot_id = id_slot;
3713-
3714-
ctx_server.queue_results.add_waiting_task_id(task.id);
3715-
ctx_server.queue_tasks.post(task);
3716-
3717-
server_task_result_ptr result = ctx_server.queue_results.recv(task.id);
3718-
ctx_server.queue_results.remove_waiting_task_id(task.id);
3719-
3720-
if (result->is_error()) {
3721-
res_error(res, result->to_json());
3722-
return;
3723-
}
3724-
3725-
GGML_ASSERT(dynamic_cast<server_task_result_slot_erase*>(result.get()) != nullptr);
3726-
res_ok(res, result->to_json());
3713+
const auto handle_slots_erase = [&handle_slot_impl](const httplib::Request & req, httplib::Response & res, int id_slot) {
3714+
handle_slot_impl(req, res, id_slot, SERVER_TASK_TYPE_SLOT_ERASE);
37273715
};
37283716

3729-
const auto handle_slots_action = [&params, &res_error, &handle_slots_save, &handle_slots_restore, &handle_slots_erase](const httplib::Request & req, httplib::Response & res) {
3717+
auto handle_slots_action = [&params, &res_error, &handle_slots_save, &handle_slots_restore, &handle_slots_erase](const httplib::Request & req, httplib::Response & res) {
37303718
if (params.slot_save_path.empty()) {
37313719
res_error(res, format_error_response("This server does not support slots action. Start it with `--slot-save-path`", ERROR_TYPE_NOT_SUPPORTED));
37323720
return;

0 commit comments

Comments
 (0)