Fix db wipe on json error and default config checking

This commit is contained in:
Izalia Mae 2019-09-30 10:45:46 +00:00 committed by kaniini
parent f3b4fc44b8
commit b4f0835106
3 changed files with 26 additions and 5 deletions

View file

@ -9,7 +9,23 @@ import yaml
def load_config(): def load_config():
with open('relay.yaml') as f: with open('relay.yaml') as f:
return yaml.load(f) yaml_file = yaml.load(f)
whitelist = yaml_file['ap'].get('whitelist', [])
blocked = yaml_file['ap'].get('blocked_instances', [])
config = {
'db': yaml_file.get('db', 'relay.jsonld'),
'listen': yaml_file.get('listen', '0.0.0.0'),
'port': int(yaml_file.get('port', 8080)),
'note': yaml_file.get('note', 'Make a note about your instance here.'),
'ap': {
'blocked_instances': [] if blocked is None else blocked,
'host': yaml_file['ap'].get('host', 'localhost'),
'whitelist': [] if whitelist is None else whitelist,
'whitelist_enabled': yaml_file['ap'].get('whitelist_enabled', False)
}
}
return config
CONFIG = load_config() CONFIG = load_config()

View file

@ -36,7 +36,7 @@ from . import app, CONFIG
from .remote_actor import fetch_actor from .remote_actor import fetch_actor
AP_CONFIG = CONFIG.get('ap', {'host': 'localhost','blocked_instances':[], 'whitelist_enabled': False, 'whitelist': []}) AP_CONFIG = CONFIG['ap']
CACHE_SIZE = CONFIG.get('cache-size', 16384) CACHE_SIZE = CONFIG.get('cache-size', 16384)

View file

@ -2,19 +2,24 @@ import asyncio
import logging import logging
import urllib.parse import urllib.parse
import simplejson as json import simplejson as json
from sys import exit
from . import CONFIG from . import CONFIG
AP_CONFIG = CONFIG.get('ap', {'blocked_instances':[], 'whitelist_enabled': False, 'whitelist': []}) AP_CONFIG = CONFIG['ap']
try: try:
with open(CONFIG['db']) as f: with open(CONFIG['db']) as f:
DATABASE = json.load(f) DATABASE = json.load(f)
except:
except FileNotFoundError:
logging.info('No database was found, making a new one.') logging.info('No database was found, making a new one.')
DATABASE = {} DATABASE = {}
except json.decoder.JSONDecodeError:
logging.info('Invalid JSON in db. Exiting...')
exit(1)
following = DATABASE.get('relay-list', []) following = DATABASE.get('relay-list', [])
for inbox in following: for inbox in following:
if urllib.parse.urlsplit(inbox).hostname in AP_CONFIG['blocked_instances']: if urllib.parse.urlsplit(inbox).hostname in AP_CONFIG['blocked_instances']: