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.

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