36 lines
914 B
C++
36 lines
914 B
C++
#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
|
|
|