Skip to content

Commit

Permalink
server : add handle_slot_type lambda
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
danbev committed Jan 31, 2025
1 parent a2df278 commit c5825e7
Showing 1 changed file with 11 additions and 29 deletions.
40 changes: 11 additions & 29 deletions examples/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, &params](const httplib::Request & req, httplib::Response & res, int id_slot) {
const auto handle_slot_type = [&ctx_server, &res_error, &res_ok, &params](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)) {
Expand All @@ -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;
Expand All @@ -3691,37 +3692,18 @@ int main(int argc, char ** argv) {
return;
}

if (type == SERVER_TASK_TYPE_SLOT_SAVE) {
GGML_ASSERT(dynamic_cast<server_task_result_slot_save_load*>(result.get()) != nullptr);
}
res_ok(res, result->to_json());
};

const auto handle_slots_restore = [&ctx_server, &res_error, &res_ok, &params](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;

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;
}
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);
};

GGML_ASSERT(dynamic_cast<server_task_result_slot_save_load*>(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) {
Expand Down

0 comments on commit c5825e7

Please sign in to comment.