Coverage for yaptide/application.py: 85%

33 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-04 00:31 +0000

1import os 

2 

3from flask import Flask 

4from flask_restful import Api 

5from flask_migrate import Migrate 

6from yaptide.persistence.models import create_all 

7from yaptide.persistence.database import db 

8from yaptide.routes.main_routes import initialize_routes 

9 

10 

11def create_app(): 

12 """Function starting Flask Server""" 

13 flask_name = __name__.split('.')[0] 

14 app = Flask(flask_name) 

15 app.logger.info("Creating Flask app %s", flask_name) 

16 

17 # Print env variables 

18 for item in os.environ.items(): 

19 app.logger.debug("Environment variable: %s", item) 

20 

21 # Load configuration from environment variables 

22 # Load any environment variables that start with FLASK_, dropping the prefix from the env key for the config key. 

23 # Values are passed through a loading function to attempt to convert them to more specific types than strings. 

24 app.config.from_prefixed_env() 

25 for item in app.config.items(): 

26 app.logger.debug("Flask config variable: %s", item) 

27 

28 if app.config.get('USE_CORS'): 

29 app.logger.info("enabling cors") 

30 from flask_cors import CORS 

31 cors_config = { 

32 "origins": ["http://127.0.0.1:3000", "http://localhost:3000"], 

33 "supports_credentials": True, 

34 "resources": { 

35 r"/*": { 

36 "origins": ["http://127.0.0.1:3000", "http://localhost:3000"] 

37 } 

38 }, 

39 "allow_headers": ["Content-Type", "Authorization"], 

40 "expose_headers": ["Access-Control-Allow-Origin"], 

41 "send_wildcard": False, 

42 "always_send": True, 

43 } 

44 

45 CORS(app, **cors_config) 

46 

47 app.logger.info(f"Initializing Flask to use SQLAlchemy ORM @ {app.config['SQLALCHEMY_DATABASE_URI']}") 

48 db.init_app(app) 

49 

50 # Find a better solution (maybe with Flask-Migrate) to handle migration of data from past versions 

51 with app.app_context(): 

52 app.logger.debug("Creating models") 

53 create_all() 

54 app.logger.debug(f"Created {len(db.metadata.tables)} tables") 

55 

56 Migrate(app, db) 

57 api = Api(app) 

58 initialize_routes(api) 

59 

60 return app 

61 

62 

63if __name__ == "__main__": 

64 create_app()