A multipurpose python flask API server and administration SPA
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.

105 lines
2.7 KiB

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