* @brief Save the configuration settings. * @param response The HTTP response object. * @param request The HTTP request object. * The body for the post request should be JSON serialized in the following format: * @code{.json} * { * "key": "value" * } * @endcode * * @attention{It is recommended to ONLY save the config settings that differ from the default behavior.}
| 1047 | * @api_examples{/api/config| POST| {"key":"value"}} |
| 1048 | */ |
| 1049 | void saveConfig(resp_https_t response, req_https_t request) { |
| 1050 | if (!validateContentType(response, request, "application/json") || !authenticate(response, request)) { |
| 1051 | return; |
| 1052 | } |
| 1053 | |
| 1054 | print_req(request); |
| 1055 | |
| 1056 | std::stringstream ss; |
| 1057 | ss << request->content.rdbuf(); |
| 1058 | try { |
| 1059 | // TODO: Input Validation |
| 1060 | std::stringstream config_stream; |
| 1061 | nlohmann::json output_tree; |
| 1062 | nlohmann::json input_tree = nlohmann::json::parse(ss); |
| 1063 | for (const auto &[k, v] : input_tree.items()) { |
| 1064 | if (v.is_null() || (v.is_string() && v.get<std::string>().empty())) { |
| 1065 | continue; |
| 1066 | } |
| 1067 | |
| 1068 | // v.dump() will dump valid json, which we do not want for strings in the config right now |
| 1069 | // we should migrate the config file to straight json and get rid of all this nonsense |
| 1070 | config_stream << k << " = " << (v.is_string() ? v.get<std::string>() : v.dump()) << std::endl; |
| 1071 | } |
| 1072 | file_handler::write_file(config::sunshine.config_file.c_str(), config_stream.str()); |
| 1073 | output_tree["status"] = true; |
| 1074 | send_response(response, output_tree); |
| 1075 | } catch (std::exception &e) { |
| 1076 | BOOST_LOG(warning) << "SaveConfig: "sv << e.what(); |
| 1077 | bad_request(response, request, e.what()); |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | /** |
| 1082 | * @brief Upload a cover image. |
nothing calls this directly
no test coverage detected