2022-03-27 10:42:53 +02:00
|
|
|
import os
|
|
|
|
|
2022-03-17 09:26:25 +01:00
|
|
|
from apiflask import APIFlask
|
|
|
|
|
2022-03-13 20:43:24 +01:00
|
|
|
from dotenv import load_dotenv
|
2022-03-17 13:33:19 +01:00
|
|
|
from flask_cors import CORS
|
2022-03-13 20:43:24 +01:00
|
|
|
|
2022-03-27 18:16:45 +02:00
|
|
|
from api_blueprint_keyword import keyword_blueprint
|
|
|
|
from api_blueprint_portfolio import portfolio_blueprint
|
|
|
|
from api_blueprint_shares import shares_blueprint
|
|
|
|
from api_blueprint_transactions import transaction_blueprint
|
|
|
|
from api_blueprint_telegram import telegram_blueprint
|
|
|
|
from helper_functions import hash_password
|
2022-03-14 17:36:38 +01:00
|
|
|
from models import *
|
|
|
|
from api_blueprint_user import users_blueprint
|
2022-03-13 21:00:14 +01:00
|
|
|
|
2022-03-08 15:11:39 +01:00
|
|
|
|
2022-03-13 20:43:24 +01:00
|
|
|
def create_app():
|
|
|
|
load_dotenv()
|
|
|
|
|
|
|
|
# Create Flask app load app.config
|
2022-03-17 19:09:14 +01:00
|
|
|
application = APIFlask(__name__, openapi_blueprint_url_prefix='/api')
|
2022-03-13 20:43:24 +01:00
|
|
|
application.config.from_object("config.ConfigClass")
|
|
|
|
|
2022-03-19 20:25:34 +01:00
|
|
|
CORS(application, resources={r"*": {"origins": "*"}})
|
2022-03-17 13:33:19 +01:00
|
|
|
|
2022-03-13 20:43:24 +01:00
|
|
|
application.app_context().push()
|
|
|
|
|
|
|
|
db.init_app(application)
|
|
|
|
|
2022-03-14 17:10:00 +01:00
|
|
|
# api blueprints
|
|
|
|
application.register_blueprint(keyword_blueprint)
|
|
|
|
application.register_blueprint(shares_blueprint)
|
2022-03-14 18:13:16 +01:00
|
|
|
application.register_blueprint(transaction_blueprint)
|
2022-03-14 23:40:36 +01:00
|
|
|
application.register_blueprint(portfolio_blueprint)
|
2022-03-14 17:10:00 +01:00
|
|
|
application.register_blueprint(users_blueprint)
|
2022-03-27 17:24:26 +02:00
|
|
|
application.register_blueprint(telegram_blueprint)
|
2022-03-14 17:10:00 +01:00
|
|
|
|
2022-03-22 11:21:58 +01:00
|
|
|
@application.before_first_request
|
|
|
|
def init_database():
|
|
|
|
db.create_all()
|
|
|
|
|
2022-03-27 17:23:33 +02:00
|
|
|
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
|
2022-03-27 10:55:48 +02:00
|
|
|
bot = User(
|
2022-03-27 17:23:33 +02:00
|
|
|
email=os.getenv("BOT_EMAIL"),
|
|
|
|
username=os.getenv("BOT_USERNAME"),
|
2022-03-27 10:55:48 +02:00
|
|
|
password=hash_password(os.getenv("BOT_PASSWORD")),
|
|
|
|
admin=False
|
|
|
|
)
|
|
|
|
db.session.add(bot)
|
|
|
|
db.session.commit()
|
2022-03-27 10:42:53 +02:00
|
|
|
|
2022-03-27 17:23:33 +02:00
|
|
|
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
|
2022-03-27 10:55:48 +02:00
|
|
|
admin = User(
|
2022-03-27 17:23:33 +02:00
|
|
|
email=os.getenv("ADMIN_EMAIL"),
|
|
|
|
username=os.getenv("ADMIN_USERNAME"),
|
2022-03-27 10:55:48 +02:00
|
|
|
password=hash_password(os.getenv("ADMIN_PASSWORD")),
|
|
|
|
admin=True
|
|
|
|
)
|
|
|
|
db.session.add(admin)
|
|
|
|
db.session.commit()
|
2022-03-27 10:42:53 +02:00
|
|
|
|
2022-03-13 20:43:24 +01:00
|
|
|
return application
|
|
|
|
|
|
|
|
|
|
|
|
app = create_app()
|
|
|
|
|
|
|
|
# Start development web server
|
2022-03-08 15:11:39 +01:00
|
|
|
if __name__ == '__main__':
|
2022-03-12 20:21:26 +01:00
|
|
|
app.run()
|