gallery.accords-library.com/server/szurubooru/app.py

125 lines
4.9 KiB
Python
Raw Normal View History

''' Exports create_app. '''
import os
2016-05-08 17:41:35 +00:00
import logging
import coloredlogs
import falcon
2016-05-08 17:41:35 +00:00
from szurubooru import api, config, errors, middleware
def _on_auth_error(ex, _request, _response, _params):
raise falcon.HTTPForbidden(
title='Authentication error', description=str(ex))
def _on_validation_error(ex, _request, _response, _params):
raise falcon.HTTPBadRequest(title='Validation error', description=str(ex))
def _on_search_error(ex, _request, _response, _params):
raise falcon.HTTPBadRequest(title='Search error', description=str(ex))
2016-04-02 12:40:10 +00:00
def _on_integrity_error(ex, _request, _response, _params):
raise falcon.HTTPConflict(
title='Integrity violation', description=ex.args[0])
def _on_not_found_error(ex, _request, _response, _params):
raise falcon.HTTPNotFound(title='Not found', description=str(ex))
2016-04-09 19:41:10 +00:00
def _on_processing_error(ex, _request, _response, _params):
2016-04-10 15:04:25 +00:00
raise falcon.HTTPBadRequest(title='Processing error', description=str(ex))
2016-04-09 19:41:10 +00:00
2016-04-19 10:40:08 +00:00
def create_method_not_allowed(allowed_methods):
allowed = ', '.join(allowed_methods)
def method_not_allowed(request, response, **_kwargs):
2016-04-19 10:40:08 +00:00
response.status = falcon.status_codes.HTTP_405
response.set_header('Allow', allowed)
request.context.output = {
'title': 'Method not allowed',
'description': 'Allowed methods: %r' % allowed_methods,
}
return method_not_allowed
def validate_config():
'''
Check whether config doesn't contain errors that might prove
lethal at runtime.
'''
2016-05-10 09:58:55 +00:00
from szurubooru.func.auth import RANK_MAP
for privilege, rank in config.config['privileges'].items():
2016-05-10 09:58:55 +00:00
if rank not in RANK_MAP.values():
raise errors.ConfigError(
'Rank %r for privilege %r is missing' % (rank, privilege))
2016-05-10 09:58:55 +00:00
if config.config['default_rank'] not in RANK_MAP.values():
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():
2016-04-03 20:03:58 +00:00
''' Create a WSGI compatible App object. '''
validate_config()
2016-04-19 10:40:08 +00:00
falcon.responders.create_method_not_allowed = create_method_not_allowed
2016-05-08 17:41:35 +00:00
coloredlogs.install(fmt='[%(asctime)-15s] %(name)s %(message)s')
if config.config['debug']:
logging.getLogger('szurubooru').setLevel(logging.INFO)
if config.config['show_sql']:
2016-05-08 17:41:35 +00:00
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
app = falcon.API(
request_type=api.Request,
middleware=[
2016-04-03 20:03:58 +00:00
middleware.RequireJson(),
middleware.CachePurger(),
middleware.ContextAdapter(),
2016-04-18 20:41:39 +00:00
middleware.DbSession(),
2016-04-03 20:03:58 +00:00
middleware.Authenticator(),
middleware.RequestLogger(),
])
2016-04-03 20:03:58 +00:00
app.add_error_handler(errors.AuthError, _on_auth_error)
app.add_error_handler(errors.IntegrityError, _on_integrity_error)
app.add_error_handler(errors.ValidationError, _on_validation_error)
app.add_error_handler(errors.SearchError, _on_search_error)
app.add_error_handler(errors.NotFoundError, _on_not_found_error)
2016-04-09 19:41:10 +00:00
app.add_error_handler(errors.ProcessingError, _on_processing_error)
2016-04-24 14:34:06 +00:00
app.add_route('/users/', api.UserListApi())
app.add_route('/user/{user_name}', api.UserDetailApi())
app.add_route('/password-reset/{user_name}', api.PasswordResetApi())
app.add_route('/tag-categories/', api.TagCategoryListApi())
app.add_route('/tag-category/{category_name}', api.TagCategoryDetailApi())
app.add_route('/tag-category/{category_name}/default', api.DefaultTagCategoryApi())
2016-04-24 14:34:06 +00:00
app.add_route('/tags/', api.TagListApi())
app.add_route('/tag/{tag_name}', api.TagDetailApi())
app.add_route('/tag-merge/', api.TagMergeApi())
app.add_route('/tag-siblings/{tag_name}', api.TagSiblingsApi())
2016-04-30 21:17:08 +00:00
app.add_route('/posts/', api.PostListApi())
2016-04-25 08:48:15 +00:00
app.add_route('/post/{post_id}', api.PostDetailApi())
2016-04-24 14:34:06 +00:00
app.add_route('/post/{post_id}/score', api.PostScoreApi())
2016-04-28 17:04:44 +00:00
app.add_route('/post/{post_id}/favorite', api.PostFavoriteApi())
app.add_route('/post/{post_id}/around', api.PostsAroundApi())
2016-04-24 14:34:06 +00:00
app.add_route('/comments/', api.CommentListApi())
app.add_route('/comment/{comment_id}', api.CommentDetailApi())
app.add_route('/comment/{comment_id}/score', api.CommentScoreApi())
app.add_route('/info/', api.InfoApi())
app.add_route('/featured-post/', api.PostFeatureApi())
app.add_route('/snapshots/', api.SnapshotListApi())
return app