69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
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_keyword import keyword_blueprint
|
|
from api_blueprint_shares import shares_blueprint
|
|
from api_blueprint_user import users_blueprint
|
|
from api_blueprint_transactions import transaction_blueprint
|
|
from api_blueprint_portfolio import portfolio_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_USER") is not None and os.getenv("BOT_PASSWORD") is not None:
|
|
if db.session.query(User).filter_by(username=os.getenv("BOT_USER")).first() is None: # Check if user already exist
|
|
bot = User(
|
|
username=os.getenv("BOT_USER"),
|
|
password=hash_password(os.getenv("BOT_PASSWORD")),
|
|
admin=False
|
|
)
|
|
db.session.add(bot)
|
|
db.session.commit()
|
|
|
|
if os.getenv("ADMIN_USER") is not None and os.getenv("ADMIN_PASSWORD") is not None:
|
|
if db.session.query(User).filter_by(username=os.getenv("ADMIN_USER")).first() is None: # Check if user already exist
|
|
admin = User(
|
|
username=os.getenv("ADMIN_USER"),
|
|
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()
|