Coverage for yaptide/application.py: 87%
38 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-22 07:31 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-22 07:31 +0000
1import os
3from flask import Flask
4from flask_restful import Api
5from flask_migrate import Migrate
6from flask_swagger_ui import get_swaggerui_blueprint
7from yaptide.persistence.models import create_all
8from yaptide.persistence.database import db
9from yaptide.routes.main_routes import initialize_routes
12def create_app():
13 """Function starting Flask Server"""
14 flask_name = __name__.split('.')[0]
15 app = Flask(flask_name)
16 app.logger.info("Creating Flask app %s", flask_name)
18 # Print env variables
19 for item in os.environ.items():
20 app.logger.debug("Environment variable: %s", item)
22 # Load configuration from environment variables
23 # Load any environment variables that start with FLASK_, dropping the prefix from the env key for the config key.
24 # Values are passed through a loading function to attempt to convert them to more specific types than strings.
25 app.config.from_prefixed_env()
26 for item in app.config.items():
27 app.logger.debug("Flask config variable: %s", item)
29 if app.config.get('USE_CORS'):
30 app.logger.info("enabling cors")
31 from flask_cors import CORS
32 cors_config = {
33 "origins": ["http://127.0.0.1:3000", "http://localhost:3000"],
34 "supports_credentials": True,
35 "resources": {
36 r"/*": {
37 "origins": ["http://127.0.0.1:3000", "http://localhost:3000"]
38 }
39 },
40 "allow_headers": ["Content-Type", "Authorization"],
41 "expose_headers": ["Access-Control-Allow-Origin"],
42 "send_wildcard": False,
43 "always_send": True,
44 }
46 CORS(app, **cors_config)
48 SWAGGER_URL = '/api/docs'
49 API_URL = '/static/openapi.yaml'
51 swaggerui_blueprint = get_swaggerui_blueprint(SWAGGER_URL, API_URL, config={'app_name': "yaptide"})
53 app.register_blueprint(swaggerui_blueprint)
55 app.logger.info(f"Initializing Flask to use SQLAlchemy ORM @ {app.config['SQLALCHEMY_DATABASE_URI']}")
56 db.init_app(app)
58 # Find a better solution (maybe with Flask-Migrate) to handle migration of data from past versions
59 with app.app_context():
60 app.logger.debug("Creating models")
61 create_all()
62 app.logger.debug(f"Created {len(db.metadata.tables)} tables")
64 Migrate(app, db)
65 api = Api(app)
66 initialize_routes(api)
68 return app
71if __name__ == "__main__":
72 create_app()