diff --git a/relay/__init__.py b/relay/__init__.py index fed32d8..87ab5ef 100644 --- a/relay/__init__.py +++ b/relay/__init__.py @@ -9,7 +9,23 @@ import yaml def load_config(): 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() diff --git a/relay/actor.py b/relay/actor.py index e35faa6..4c61f75 100644 --- a/relay/actor.py +++ b/relay/actor.py @@ -36,7 +36,7 @@ from . import app, CONFIG 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) diff --git a/relay/database.py b/relay/database.py index 6a99d49..299cbb8 100644 --- a/relay/database.py +++ b/relay/database.py @@ -2,19 +2,24 @@ import asyncio import logging import urllib.parse import simplejson as json +from sys import exit from . import CONFIG -AP_CONFIG = CONFIG.get('ap', {'blocked_instances':[], 'whitelist_enabled': False, 'whitelist': []}) - +AP_CONFIG = CONFIG['ap'] try: with open(CONFIG['db']) as f: DATABASE = json.load(f) -except: + +except FileNotFoundError: logging.info('No database was found, making a new one.') DATABASE = {} +except json.decoder.JSONDecodeError: + logging.info('Invalid JSON in db. Exiting...') + exit(1) + following = DATABASE.get('relay-list', []) for inbox in following: if urllib.parse.urlsplit(inbox).hostname in AP_CONFIG['blocked_instances']: