Skip to content

Commit c5825e7

Browse files
committed
server : add handle_slot_type lambda
This commit adds a lambda function by extracting the common code for saving and restoring slots. The motivation for this change is to reduce code duplication between the current two lambda expressions/functions.
1 parent a2df278 commit c5825e7

File tree

1 file changed

+11
-29
lines changed

1 file changed

+11
-29
lines changed

examples/server/server.cpp

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3665,7 +3665,8 @@ int main(int argc, char ** argv) {
36653665
res.status = 200; // HTTP OK
36663666
};
36673667

3668-
const auto handle_slots_save = [&ctx_server, &res_error, &res_ok, &params](const httplib::Request & req, httplib::Response & res, int id_slot) {
3668+
const auto handle_slot_type = [&ctx_server, &res_error, &res_ok, &params](const httplib::Request & req,
3669+
httplib::Response & res, int id_slot, server_task_type type) {
36693670
json request_data = json::parse(req.body);
36703671
std::string filename = request_data.at("filename");
36713672
if (!fs_validate_filename(filename)) {
@@ -3674,7 +3675,7 @@ int main(int argc, char ** argv) {
36743675
}
36753676
std::string filepath = params.slot_save_path + filename;
36763677

3677-
server_task task(SERVER_TASK_TYPE_SLOT_SAVE);
3678+
server_task task(type);
36783679
task.id = ctx_server.queue_tasks.get_new_id();
36793680
task.slot_action.slot_id = id_slot;
36803681
task.slot_action.filename = filename;
@@ -3691,37 +3692,18 @@ int main(int argc, char ** argv) {
36913692
return;
36923693
}
36933694

3695+
if (type == SERVER_TASK_TYPE_SLOT_SAVE) {
3696+
GGML_ASSERT(dynamic_cast<server_task_result_slot_save_load*>(result.get()) != nullptr);
3697+
}
36943698
res_ok(res, result->to_json());
36953699
};
36963700

3697-
const auto handle_slots_restore = [&ctx_server, &res_error, &res_ok, &params](const httplib::Request & req, httplib::Response & res, int id_slot) {
3698-
json request_data = json::parse(req.body);
3699-
std::string filename = request_data.at("filename");
3700-
if (!fs_validate_filename(filename)) {
3701-
res_error(res, format_error_response("Invalid filename", ERROR_TYPE_INVALID_REQUEST));
3702-
return;
3703-
}
3704-
std::string filepath = params.slot_save_path + filename;
3705-
3706-
server_task task(SERVER_TASK_TYPE_SLOT_RESTORE);
3707-
task.id = ctx_server.queue_tasks.get_new_id();
3708-
task.slot_action.slot_id = id_slot;
3709-
task.slot_action.filename = filename;
3710-
task.slot_action.filepath = filepath;
3711-
3712-
ctx_server.queue_results.add_waiting_task_id(task.id);
3713-
ctx_server.queue_tasks.post(task);
3714-
3715-
server_task_result_ptr result = ctx_server.queue_results.recv(task.id);
3716-
ctx_server.queue_results.remove_waiting_task_id(task.id);
3717-
3718-
if (result->is_error()) {
3719-
res_error(res, result->to_json());
3720-
return;
3721-
}
3701+
const auto handle_slots_save = [&handle_slot_type](const httplib::Request & req, httplib::Response & res, int id_slot) {
3702+
handle_slot_type(req, res, id_slot, SERVER_TASK_TYPE_SLOT_SAVE);
3703+
};
37223704

3723-
GGML_ASSERT(dynamic_cast<server_task_result_slot_save_load*>(result.get()) != nullptr);
3724-
res_ok(res, result->to_json());
3705+
const auto handle_slots_restore = [&handle_slot_type](const httplib::Request & req, httplib::Response & res, int id_slot) {
3706+
handle_slot_type(req, res, id_slot, SERVER_TASK_TYPE_SLOT_RESTORE);
37253707
};
37263708

37273709
const auto handle_slots_erase = [&ctx_server, &res_error, &res_ok](const httplib::Request & /* req */, httplib::Response & res, int id_slot) {

0 commit comments

Comments
 (0)