server/config: move config validation to app.py
This commit is contained in:
parent
cd15cdff7a
commit
cecf620126
2 changed files with 31 additions and 31 deletions
|
@ -1,5 +1,6 @@
|
||||||
''' Exports create_app. '''
|
''' Exports create_app. '''
|
||||||
|
|
||||||
|
import os
|
||||||
import logging
|
import logging
|
||||||
import coloredlogs
|
import coloredlogs
|
||||||
import falcon
|
import falcon
|
||||||
|
@ -36,8 +37,38 @@ def create_method_not_allowed(allowed_methods):
|
||||||
}
|
}
|
||||||
return method_not_allowed
|
return method_not_allowed
|
||||||
|
|
||||||
|
def validate_config():
|
||||||
|
'''
|
||||||
|
Check whether config doesn't contain errors that might prove
|
||||||
|
lethal at runtime.
|
||||||
|
'''
|
||||||
|
from szurubooru.db.user import User
|
||||||
|
for privilege, rank in config.config['privileges'].items():
|
||||||
|
if rank not in User.ALL_RANKS:
|
||||||
|
raise errors.ConfigError(
|
||||||
|
'Rank %r for privilege %r is missing' % (rank, privilege))
|
||||||
|
if config.config['default_rank'] not in User.ALL_RANKS:
|
||||||
|
raise errors.ConfigError(
|
||||||
|
'Default rank %r is not on the list of known ranks' % (
|
||||||
|
config.config['default_rank']))
|
||||||
|
|
||||||
|
for key in ['base_url', 'api_url', 'data_url', 'data_dir']:
|
||||||
|
if not config.config[key]:
|
||||||
|
raise errors.ConfigError(
|
||||||
|
'Service is not configured: %r is missing' % key)
|
||||||
|
|
||||||
|
if not os.path.isabs(config.config['data_dir']):
|
||||||
|
raise errors.ConfigError(
|
||||||
|
'data_dir must be an absolute path')
|
||||||
|
|
||||||
|
for key in ['schema', 'host', 'port', 'user', 'pass', 'name']:
|
||||||
|
if not config.config['database'][key]:
|
||||||
|
raise errors.ConfigError(
|
||||||
|
'Database is not configured: %r is missing' % key)
|
||||||
|
|
||||||
def create_app():
|
def create_app():
|
||||||
''' Create a WSGI compatible App object. '''
|
''' Create a WSGI compatible App object. '''
|
||||||
|
validate_config()
|
||||||
falcon.responders.create_method_not_allowed = create_method_not_allowed
|
falcon.responders.create_method_not_allowed = create_method_not_allowed
|
||||||
|
|
||||||
coloredlogs.install(fmt='[%(asctime)-15s] %(name)s %(message)s')
|
coloredlogs.install(fmt='[%(asctime)-15s] %(name)s %(message)s')
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import os
|
import os
|
||||||
import yaml
|
import yaml
|
||||||
from szurubooru import errors
|
|
||||||
|
|
||||||
def merge(left, right):
|
def merge(left, right):
|
||||||
for key in right:
|
for key in right:
|
||||||
|
@ -21,34 +20,4 @@ def read_config():
|
||||||
ret = merge(ret, yaml.load(handle.read()))
|
ret = merge(ret, yaml.load(handle.read()))
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def validate_config(src):
|
|
||||||
'''
|
|
||||||
Check whether config doesn't contain errors that might prove
|
|
||||||
lethal at runtime.
|
|
||||||
'''
|
|
||||||
from szurubooru.db.user import User
|
|
||||||
for privilege, rank in src['privileges'].items():
|
|
||||||
if rank not in User.ALL_RANKS:
|
|
||||||
raise errors.ConfigError(
|
|
||||||
'Rank %r for privilege %r is missing' % (rank, privilege))
|
|
||||||
if src['default_rank'] not in User.ALL_RANKS:
|
|
||||||
raise errors.ConfigError(
|
|
||||||
'Default rank %r is not on the list of known ranks' % (
|
|
||||||
src['default_rank']))
|
|
||||||
|
|
||||||
for key in ['base_url', 'api_url', 'data_url', 'data_dir']:
|
|
||||||
if not src[key]:
|
|
||||||
raise errors.ConfigError(
|
|
||||||
'Service is not configured: %r is missing' % key)
|
|
||||||
|
|
||||||
if not os.path.isabs(src['data_dir']):
|
|
||||||
raise errors.ConfigError(
|
|
||||||
'data_dir must be an absolute path')
|
|
||||||
|
|
||||||
for key in ['schema', 'host', 'port', 'user', 'pass', 'name']:
|
|
||||||
if not src['database'][key]:
|
|
||||||
raise errors.ConfigError(
|
|
||||||
'Database is not configured: %r is missing' % key)
|
|
||||||
|
|
||||||
config = read_config() # pylint: disable=invalid-name
|
config = read_config() # pylint: disable=invalid-name
|
||||||
validate_config(config)
|
|
||||||
|
|
Loading…
Reference in a new issue