2022-03-27 08:42:53 +00:00
|
|
|
import os
|
|
|
|
|
2022-03-17 08:26:25 +00:00
|
|
|
from apiflask import APIFlask
|
|
|
|
|
2022-03-13 19:43:24 +00:00
|
|
|
from dotenv import load_dotenv
|
2022-03-17 12:33:19 +00:00
|
|
|
from flask_cors import CORS
|
2022-03-13 19:43:24 +00:00
|
|
|
|
2022-03-27 08:42:53 +00:00
|
|
|
from api.helper_functions import hash_password
|
2022-03-14 16:36:38 +00:00
|
|
|
from models import *
|
|
|
|
from api_blueprint_keyword import keyword_blueprint
|
|
|
|
from api_blueprint_shares import shares_blueprint
|
|
|
|
from api_blueprint_user import users_blueprint
|
2022-03-14 17:13:16 +00:00
|
|
|
from api_blueprint_transactions import transaction_blueprint
|
2022-03-15 07:14:54 +00:00
|
|
|
from api_blueprint_portfolio import portfolio_blueprint
|
2022-03-13 20:00:14 +00:00
|
|
|
|
2022-03-08 14:11:39 +00:00
|
|
|
|
2022-03-13 19:43:24 +00:00
|
|
|
def create_app():
|
|
|
|
load_dotenv()
|
|
|
|
|
|
|
|
# Create Flask app load app.config
|
2022-03-17 18:09:14 +00:00
|
|
|
application = APIFlask(__name__, openapi_blueprint_url_prefix='/api')
|
2022-03-13 19:43:24 +00:00
|
|
|
application.config.from_object("config.ConfigClass")
|
|
|
|
|
2022-03-19 19:25:34 +00:00
|
|
|
CORS(application, resources={r"*": {"origins": "*"}})
|
2022-03-17 12:33:19 +00:00
|
|
|
|
2022-03-13 19:43:24 +00:00
|
|
|
application.app_context().push()
|
|
|
|
|
|
|
|
db.init_app(application)
|
|
|
|
|
2022-03-14 16:10:00 +00:00
|
|
|
# api blueprints
|
|
|
|
application.register_blueprint(keyword_blueprint)
|
|
|
|
application.register_blueprint(shares_blueprint)
|
2022-03-14 17:13:16 +00:00
|
|
|
application.register_blueprint(transaction_blueprint)
|
2022-03-14 22:40:36 +00:00
|
|
|
application.register_blueprint(portfolio_blueprint)
|
2022-03-14 16:10:00 +00:00
|
|
|
application.register_blueprint(users_blueprint)
|
|
|
|
|
2022-03-22 10:21:58 +00:00
|
|
|
@application.before_first_request
|
|
|
|
def init_database():
|
|
|
|
db.create_all()
|
|
|
|
|
2022-03-27 08:42:53 +00:00
|
|
|
if os.getenv("BOT_USER") is not None and os.getenv("BOT_PASSWORD") is not None:
|
2022-03-27 08:55:48 +00:00
|
|
|
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()
|
2022-03-27 08:42:53 +00:00
|
|
|
|
|
|
|
if os.getenv("ADMIN_USER") is not None and os.getenv("ADMIN_PASSWORD") is not None:
|
2022-03-27 08:55:48 +00:00
|
|
|
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()
|
2022-03-27 08:42:53 +00:00
|
|
|
|
2022-03-13 19:43:24 +00:00
|
|
|
return application
|
|
|
|
|
|
|
|
|
|
|
|
app = create_app()
|
|
|
|
|
|
|
|
# Start development web server
|
2022-03-08 14:11:39 +00:00
|
|
|
if __name__ == '__main__':
|
2022-03-12 19:21:26 +00:00
|
|
|
app.run()
|