An ebook/comic library service and web client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.4 KiB

  1. """Atheneum Flask Application."""
  2. import os
  3. from logging.config import dictConfig
  4. from flask import Flask
  5. from flask_migrate import Migrate
  6. from atheneum.db import db
  7. from atheneum.utility import json_utility, session_utility
  8. dictConfig({
  9. 'version': 1,
  10. 'formatters': {'default': {
  11. 'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
  12. }},
  13. 'handlers': {'wsgi': {
  14. 'class': 'logging.StreamHandler',
  15. 'stream': 'ext://flask.logging.wsgi_errors_stream',
  16. 'formatter': 'default'
  17. }},
  18. 'root': {
  19. 'level': 'INFO',
  20. 'handlers': ['wsgi']
  21. }
  22. })
  23. def create_app(test_config: dict = None) -> Flask:
  24. """
  25. Create an instance of Atheneum.
  26. :param test_config:
  27. :return:
  28. """
  29. app = Flask(__name__, instance_relative_config=True)
  30. app.logger.debug('Creating Atheneum Server')
  31. data_directory = os.getenv('ATHENEUM_DATA_DIRECTORY', '/tmp')
  32. app.logger.debug('Atheneum Data Directory: %s', data_directory)
  33. default_database_uri = 'sqlite:///{}/atheneum.db'.format(data_directory)
  34. app.config.from_mapping(
  35. SECRET_KEY='dev',
  36. SQLALCHEMY_DATABASE_URI=default_database_uri,
  37. SQLALCHEMY_TRACK_MODIFICATIONS=False
  38. )
  39. if test_config is None:
  40. app.logger.debug('Loading configurations')
  41. app.config.from_object('atheneum.default_settings')
  42. app.config.from_pyfile('config.py', silent=True)
  43. if os.getenv('ATHENEUM_SETTINGS', None):
  44. app.config.from_envvar('ATHENEUM_SETTINGS')
  45. else:
  46. app.logger.debug('Loading test configuration')
  47. app.config.from_object(test_config)
  48. try:
  49. os.makedirs(app.instance_path)
  50. except OSError:
  51. pass
  52. app.json_encoder = json_utility.CustomJSONEncoder
  53. app.session_interface = session_utility.DisableSessionInterface()
  54. app.logger.debug('Initializing Application')
  55. db.init_app(app)
  56. app.logger.debug('Registering Database Models')
  57. Migrate(app, db)
  58. return app
  59. def register_blueprints(app: Flask) -> None:
  60. """
  61. Register blueprints for the application.
  62. :param app:
  63. :return:
  64. """
  65. from atheneum.api import AUTH_BLUEPRINT, USER_BLUEPRINT
  66. app.register_blueprint(AUTH_BLUEPRINT)
  67. app.register_blueprint(USER_BLUEPRINT)
  68. atheneum = create_app() # pylint: disable=C0103
  69. register_blueprints(atheneum)
  70. logger = atheneum.logger # pylint: disable=C0103
  71. if __name__ == "__main__":
  72. atheneum.run()