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.

83 lines
2.0 KiB

  1. import os
  2. from logging.config import dictConfig
  3. from flask import Flask
  4. from flask_migrate import Migrate, upgrade
  5. from flask_sqlalchemy import SQLAlchemy
  6. from atheneum import utility
  7. db: SQLAlchemy = SQLAlchemy()
  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=None):
  24. app = Flask(__name__, instance_relative_config=True)
  25. app.logger.debug('Creating Atheneum Server')
  26. data_directory = os.getenv('ATHENEUM_DATA_DIRECTORY', '/tmp')
  27. app.logger.debug('Atheneum Data Directory: %s', data_directory)
  28. app.config.from_mapping(
  29. SECRET_KEY='dev',
  30. SQLALCHEMY_DATABASE_URI='sqlite:///{}/atheneum.db'
  31. .format(data_directory)
  32. )
  33. if test_config is None:
  34. app.logger.debug('Loading configurations')
  35. app.config.from_object('atheneum.default_settings')
  36. app.config.from_pyfile('config.py', silent=True)
  37. if os.getenv('ATHENEUM_SETTINGS', None):
  38. app.config.from_envvar('ATHENEUM_SETTINGS')
  39. else:
  40. app.logger.debug('Loading test configuration')
  41. app.config.from_object(test_config)
  42. try:
  43. os.makedirs(app.instance_path)
  44. except OSError:
  45. pass
  46. app.json_encoder = utility.CustomJSONEncoder
  47. app.logger.debug('Initializing Application')
  48. db.init_app(app)
  49. app.logger.debug('Registering Database Models')
  50. Migrate(app, db)
  51. return app
  52. def register_blueprints(app):
  53. from atheneum.api import auth_blueprint
  54. app.register_blueprint(auth_blueprint)
  55. app = create_app()
  56. register_blueprints(app)
  57. def init_db():
  58. """Clear existing data and create new tables."""
  59. upgrade('migrations')
  60. if __name__ == "__main__":
  61. app.run()