Esempio n. 1
0
def create_app(cnf_name="development"):
    """
    :param cnf_name: Config name, e.g. "development"
    """
    cnf = config.get_config(cnf_name)

    app = flask.Flask(__name__)
    app.config.from_object(cnf)
    cnf.init_app(app)

    bootstrap = flask_bootstrap.Bootstrap()
    bootstrap.init_app(app)

    csrf = flask_wtf.csrf.CSRFProtect()
    csrf.init_app(app)

    app.register_blueprint(main.APP)

    app.register_blueprint(networks.APP)
    app.register_blueprint(networks.API)

    app.register_blueprint(fortios.APP)
    app.register_blueprint(fortios.API)

    return app
Esempio n. 2
0
def create_app(config_name):
    # 创建Flask应用对象
    app = Flask(__name__)
    conf = config_dict[config_name]

    # 设置flask的配置信息
    app.config.from_object(config_dict[config_name])

    # 初始化数据库
    db.init_app(app)

    # 初始化redis
    global redis_store
    redis_store = redis.StrictRedis(host=conf.REDIS_HOST, port=conf.REDIS_PORT)

    # 初始化csrf防护机制
    csrf.init_app(app)
    # 将Flask里的session数据保存在redis中
    Session(app)

    # 向app中添加自定义的路由转换器
    app.url_map.converters["re"] = RegexConverter

    # 注册蓝图 其中,api_1_0放在这里的导入是将其延迟了
    from ihome import api_1_0
    app.register_blueprint(api_1_0.api, url_prefix='/api/v1_0')

    # 提供html静态文件的蓝图
    import web_html
    app.register_blueprint(web_html.html)

    return app
Esempio n. 3
0
def create_app(config):
    app = Flask(__name__, instance_relative_config=False)
    app.config.from_object(config)
    app.static_folder = "static"
    db.init_app(app)
    login_manager.init_app(app)
    migrate.init_app(app, db)
    babel.init_app(app)
    csrf.init_app(app)

    with app.app_context():
        from application import library
        from application import users
        from application import models
        from application import functions

        # Create tables for our models
        db.create_all()

        return app
Esempio n. 4
0
def setup_app(_app: Flask) -> None:
    """
    setups flask application

    :param _app: application to configure
    """
    _app.config["CONFIG_PATH"] = os.environ.get(
        "MTG_COLLECTOR_CONFIG", os.path.join(_app.root_path, "config.cfg"))

    try:
        _app.config.from_pyfile(_app.config["CONFIG_PATH"])
    except FileNotFoundError:  # this is the first run
        from lib import conf
        conf.update_conf(SECRET_KEY=hashlib.sha512(
            str(random.randint(0, 2**64)).encode()).hexdigest(), )

    csrf.init_app(_app)
    login_manager.init_app(_app)
    lib.tasks.ImageHandler(_app)
    lib.tasks.DBUpdater(_app).start()

    _app.json_encoder = CustomJSONEncoder
    _app.notifier = lib.threading.Event()
EVENT_TITLE = os.getenv(
    "RPS_EVENT_TITLE",
    "International Asynchronous Rock Paper Scissors Tournament")
app.config['START_TIME'] = datetime.datetime.fromtimestamp(
    int(os.getenv("RPS_START_TIME",
                  datetime.datetime.now().timestamp())))

app.config['END_TIME'] = datetime.datetime.fromtimestamp(
    int(
        os.getenv("RPS_END_TIME", (app.config['START_TIME'] +
                                   datetime.timedelta(days=1)).timestamp())))

app.config['DATABASE_NAME'] = 'data/rps.db'

csrf = flask_wtf.csrf.CSRFProtect(app)
csrf.init_app(app)


@app.route('/')
def page_index():
    return flask.render_template('index.html',
                                 event_title=EVENT_TITLE,
                                 start_datetime=app.config['START_TIME'],
                                 end_datetime=app.config['END_TIME'])


@app.route('/about')
def page_about():
    return flask.render_template('about.html', event_title=EVENT_TITLE)