from flask import Flask,render_template
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
from apscheduler_elab import Config
from flask_apscheduler import APScheduler
from flask_bootstrap import Bootstrap
import importlib
import os
from file_util import FileUtil


sentry_sdk.init(
    dsn="https://e9f173e651684e9080a7b297d51512c7@o382660.ingest.sentry.io/5211871",
    integrations=[FlaskIntegration()])


def create_app():
    app_instance = Flask(__name__)
    app_instance.config.from_object(Config())
    blueprint_list = []
    path = os.path.dirname(os.path.abspath(__file__)) + r'/blue'
    file_list = FileUtil.load_file(path)
    for file_path in file_list:
        file_name = os.path.split(file_path)[1].split('.')[0]
        blueprint_list.append(file_name)

    for blueprint in blueprint_list:
        auto_blueprint_module = importlib.import_module('blue.' + str(blueprint))
        auto_blueprint = auto_blueprint_module.__dict__[blueprint]
        app_instance.register_blueprint(auto_blueprint, url_prefix='/' + str(blueprint))
    return app_instance


app = create_app()
bt = Bootstrap(app)


@app.route('/')
def index():
    return render_template('login-page.html')


if __name__ == '__main__':
    scheduler = APScheduler()
    scheduler.init_app(app)
    scheduler.start()
    app.run(
        host='127.0.0.1',
        port=5001
    )
