62 lines
2.0 KiB
Python
62 lines
2.0 KiB
Python
'''
|
|
Onionr - P2P Anonymous Storage Network
|
|
|
|
This file handles configuration setting and getting from the HTTP API
|
|
'''
|
|
'''
|
|
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 <https://www.gnu.org/licenses/>.
|
|
'''
|
|
import json
|
|
from flask import Blueprint, request, Response, abort
|
|
import config, onionrutils
|
|
config.reload()
|
|
|
|
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()))
|
|
|
|
@config_BP.route('/config/get/<key>')
|
|
def get_by_key(key):
|
|
'''Return a config setting by key'''
|
|
return Response(json.dumps(config.get(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)
|
|
except json.JSONDecodeError:
|
|
abort(400)
|
|
else:
|
|
config.set_config(new_config)
|
|
return Response('success')
|
|
|
|
@config_BP.route('/config/set/<key>', methods=['POST'])
|
|
def set_by_key(key):
|
|
'''Overwrite/set only 1 config key'''
|
|
'''
|
|
{
|
|
'data': data
|
|
}
|
|
'''
|
|
try:
|
|
data = json.loads(onionrutils.OnionrUtils.bytesToStr(request.data))['data']
|
|
except (json.JSONDecodeError, KeyError):
|
|
abort(400)
|
|
config.set(key, data, True)
|
|
return Response('success') |