This commit is contained in:
35
src/server/api_settings_routes.cpp
Normal file
35
src/server/api_settings_routes.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include "server/api_server.hpp"
|
||||
|
||||
#include "util/http_util.hpp"
|
||||
|
||||
namespace lm {
|
||||
|
||||
void ApiServer::registerSettingsRoutes(httplib::Server& svr)
|
||||
{
|
||||
svr.Get("/api/settings", [this](const httplib::Request&, httplib::Response& res) {
|
||||
HttpUtil::addCors(res);
|
||||
res.set_header("Content-Type", "application/json; charset=utf-8");
|
||||
res.body = settings_.get().dump();
|
||||
});
|
||||
|
||||
svr.Put("/api/settings", [this](const httplib::Request& req, httplib::Response& res) {
|
||||
HttpUtil::addCors(res);
|
||||
nlohmann::json body;
|
||||
try
|
||||
{
|
||||
body = nlohmann::json::parse(req.body);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return HttpUtil::jsonError(res, 400, "invalid JSON");
|
||||
}
|
||||
std::string err;
|
||||
if (!settings_.put(body, err))
|
||||
return HttpUtil::jsonError(res, 400, err);
|
||||
res.set_header("Content-Type", "application/json; charset=utf-8");
|
||||
res.body = settings_.get().dump();
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace lm
|
||||
|
||||
Reference in New Issue
Block a user