diff --git a/onionr/httpapi/configapi/__init__.py b/onionr/httpapi/configapi/__init__.py index 5f5588af..3f5484f8 100644 --- a/onionr/httpapi/configapi/__init__.py +++ b/onionr/httpapi/configapi/__init__.py @@ -27,7 +27,7 @@ config_BP = Blueprint('config_BP', __name__) @config_BP.route('/config/get') def get_all_config(): '''Simply return all configuration as JSON string''' - return Response(json.dumps(config.get_config())) + return Response(json.dumps(config.get_config(), indent=4, sort_keys=True)) @config_BP.route('/config/get/') def get_by_key(key): @@ -37,13 +37,13 @@ def get_by_key(key): @config_BP.route('/config/setall', methods=['POST']) def set_all_config(): '''Overwrite existing JSON config with new JSON string''' - new_config = request.get_json(force=True) try: - new_config = json.loads(new_config) + new_config = request.get_json(force=True) except json.JSONDecodeError: abort(400) else: config.set_config(new_config) + config.save() return Response('success') @config_BP.route('/config/set/', methods=['POST']) diff --git a/onionr/static-data/www/private/index.html b/onionr/static-data/www/private/index.html index 2b54cc99..32c4c497 100644 --- a/onionr/static-data/www/private/index.html +++ b/onionr/static-data/www/private/index.html @@ -31,6 +31,8 @@
Edit Configuration
+

Warning: Some values can be dangerous to change. Use caution.

+
diff --git a/onionr/static-data/www/shared/configeditor.js b/onionr/static-data/www/shared/configeditor.js index 45a97d46..6c9913b1 100644 --- a/onionr/static-data/www/shared/configeditor.js +++ b/onionr/static-data/www/shared/configeditor.js @@ -1,3 +1,22 @@ +/* + Onionr - P2P Anonymous Storage Network + + This file is for configuration editing in the web interface + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + var saveBtns = document.getElementsByClassName('saveConfig') var saveBtn = document.getElementsByClassName('saveConfig')[0] var configEditor = document.getElementsByClassName('configEditor')[0] @@ -7,12 +26,29 @@ fetch('/config/get', { headers: { "token": webpass }}) -.then((resp) => resp.json()) // Transform the data into json +.then((resp) => resp.text()) // Transform the data into text .then(function(resp) { - config = resp - configEditor.value = JSON.stringify(config) + configEditor.value = resp + config = JSON.parse(resp) //parse here so we can set the text field to pretty json }) saveBtn.onclick = function(){ - + var postData = configEditor.value + try { + JSON.parse(postData) + } catch (e) { + alert('Configuration is not valid JSON') + return false + } + fetch('/config/setall', { + method: 'POST', + body: postData, + headers: { + "content-type": "application/json", + "token": webpass + }}) + .then((resp) => resp.text()) // Transform the data into text + .then(function(data) { + alert('Config saved') + }) } \ No newline at end of file