import os from apiflask import APIFlask from dotenv import load_dotenv from flask_cors import CORS from api.helper_functions import hash_password from models import * from api_blueprint_user import users_blueprint def create_app(): load_dotenv() # Create Flask app load app.config application = APIFlask(__name__, openapi_blueprint_url_prefix='/api') application.config.from_object("config.ConfigClass") CORS(application, resources={r"*": {"origins": "*"}}) application.app_context().push() db.init_app(application) # api blueprints application.register_blueprint(keyword_blueprint) application.register_blueprint(shares_blueprint) application.register_blueprint(transaction_blueprint) application.register_blueprint(portfolio_blueprint) application.register_blueprint(users_blueprint) @application.before_first_request def init_database(): db.create_all() if os.getenv("BOT_EMAIL") is not None and os.getenv("BOT_USERNAME") is not None and os.getenv("BOT_PASSWORD") is not None: if db.session.query(User).filter_by(email=os.getenv("BOT_EMAIL")).first() is None: # Check if user already exist bot = User( email=os.getenv("BOT_EMAIL"), username=os.getenv("BOT_USERNAME"), password=hash_password(os.getenv("BOT_PASSWORD")), admin=False ) db.session.add(bot) db.session.commit() if os.getenv("ADMIN_EMAIL") is not None and os.getenv("ADMIN_USERNAME") is not None and os.getenv("ADMIN_PASSWORD") is not None: if db.session.query(User).filter_by(email=os.getenv("ADMIN_EMAIL")).first() is None: # Check if user already exist admin = User( email=os.getenv("ADMIN_EMAIL"), username=os.getenv("ADMIN_USERNAME"), password=hash_password(os.getenv("ADMIN_PASSWORD")), admin=True ) db.session.add(admin) db.session.commit() return application app = create_app() # Start development web server if __name__ == '__main__': app.run()