adjust endpoints to preferred style

This commit is contained in:
Jan Boon 2024-03-30 02:19:50 +08:00
parent 8b5ae299ec
commit a71ec3db7b

View file

@ -3314,11 +3314,8 @@ int main(int argc, char ** argv) {
res.status = 200; // HTTP OK res.status = 200; // HTTP OK
}; };
const auto handle_slot_save = [&ctx_server, &res_error, &sparams](const httplib::Request & req, httplib::Response & res) { const auto handle_slots_save = [&ctx_server, &res_error, &sparams](const httplib::Request & req, httplib::Response & res, int id_slot) {
res.set_header("Access-Control-Allow-Origin", req.get_header_value("Origin"));
json request_data = json::parse(req.body); json request_data = json::parse(req.body);
int id_slot = request_data["id_slot"];
std::string filename = request_data["filename"]; std::string filename = request_data["filename"];
if (filename.find('/') != std::string::npos || filename.find('\\') != std::string::npos || filename.find("..") != std::string::npos) { if (filename.find('/') != std::string::npos || filename.find('\\') != std::string::npos || filename.find("..") != std::string::npos) {
res_error(res, "Invalid filename"); res_error(res, "Invalid filename");
@ -3347,11 +3344,8 @@ int main(int argc, char ** argv) {
} }
}; };
const auto handle_slot_restore = [&ctx_server, &res_error, &sparams](const httplib::Request & req, httplib::Response & res) { const auto handle_slots_restore = [&ctx_server, &res_error, &sparams](const httplib::Request & req, httplib::Response & res, int id_slot) {
res.set_header("Access-Control-Allow-Origin", req.get_header_value("Origin"));
json request_data = json::parse(req.body); json request_data = json::parse(req.body);
int id_slot = request_data["id_slot"];
std::string filename = request_data["filename"]; std::string filename = request_data["filename"];
if (filename.find('/') != std::string::npos || filename.find('\\') != std::string::npos || filename.find("..") != std::string::npos) { if (filename.find('/') != std::string::npos || filename.find('\\') != std::string::npos || filename.find("..") != std::string::npos) {
res_error(res, "Invalid filename"); res_error(res, "Invalid filename");
@ -3380,12 +3374,7 @@ int main(int argc, char ** argv) {
} }
}; };
const auto handle_slot_erase = [&ctx_server, &res_error](const httplib::Request & req, httplib::Response & res) { const auto handle_slots_erase = [&ctx_server, &res_error](const httplib::Request & req, httplib::Response & res, int id_slot) {
res.set_header("Access-Control-Allow-Origin", req.get_header_value("Origin"));
json request_data = json::parse(req.body);
int id_slot = request_data["id_slot"];
server_task task; server_task task;
task.type = SERVER_TASK_TYPE_SLOT_ERASE; task.type = SERVER_TASK_TYPE_SLOT_ERASE;
task.data = { task.data = {
@ -3405,6 +3394,32 @@ int main(int argc, char ** argv) {
} }
}; };
const auto handle_slots_action = [&ctx_server, &res_error, &sparams, &handle_slots_save, &handle_slots_restore, &handle_slots_erase](const httplib::Request & req, httplib::Response & res) {
res.set_header("Access-Control-Allow-Origin", req.get_header_value("Origin"));
std::string id_slot_str = req.path_params.at("id_slot");
int id_slot;
try {
id_slot = std::stoi(id_slot_str);
} catch (const std::exception &) {
res_error(res, "Invalid slot ID");
return;
}
std::string action = req.get_param_value("action");
if (action == "save") {
handle_slots_save(req, res, id_slot);
} else if (action == "restore") {
handle_slots_restore(req, res, id_slot);
} else if (action == "erase") {
handle_slots_erase(req, res, id_slot);
} else {
res_error(res, "Invalid action");
}
};
const auto handle_props = [&ctx_server](const httplib::Request & req, httplib::Response & res) { const auto handle_props = [&ctx_server](const httplib::Request & req, httplib::Response & res) {
res.set_header("Access-Control-Allow-Origin", req.get_header_value("Origin")); res.set_header("Access-Control-Allow-Origin", req.get_header_value("Origin"));
json data = { json data = {
@ -3769,9 +3784,7 @@ int main(int argc, char ** argv) {
svr->Post("/detokenize", handle_detokenize); svr->Post("/detokenize", handle_detokenize);
if (!sparams.slot_save_path.empty()) { if (!sparams.slot_save_path.empty()) {
// only enable slot endpoints if slot_save_path is set // only enable slot endpoints if slot_save_path is set
svr->Post("/slot/save", handle_slot_save); svr->Post("/slots/:id_slot", handle_slots_action);
svr->Post("/slot/restore", handle_slot_restore);
svr->Post("/slot/erase", handle_slot_erase);
} }
// //