123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #!/usr/bin/env python
- # -*- encoding: utf-8 -*-
- """
- @Contact : liuyuqi.gov@msn.cn
- @Time : 2023/10/04 11:09:00
- @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
- @Desc : None
- """
- from flask import Flask, render_template
- from flask_cors import CORS
- from apps.config import config
- from .extensions import init_extensions, init_docs
- from .script import init_script
- from .views import init_blueprints
- import os
- def create_app(config_name="default") -> Flask:
- """ 创建 flask app
- :param config_name: 配置名称, 可选值: development, production, default
- :return: flask app
- """
- # banner()
- config_name = os.getenv("FLASK_CONFIG") or config_name
- app = Flask(config[config_name].BASE_DIR)
- CORS(app, supports_credentials=True, resources={r"/api/*": {"origins": "*"}})
- app.config.from_object(config[config_name]) # 读取配置
- config[config_name].init_app(app)
- init_extensions(app)
- init_blueprints(app)
-
- init_hook(app)
- configure_error_handlers(app)
- init_script(app)
- # pirnt all route
- # with app.test_request_context():
- # for rule in app.url_map.iter_rules():
- # print(rule, "---------------")
- return app
- def configure_error_handlers(app: Flask):
- @app.errorhandler(403)
- def forbidden_page(error):
- # return "Oops! You don't have permission to access this page.", 403
- return render_template("errors/403.html"), 403
- @app.errorhandler(404)
- def page_not_found(error):
- # return jsonify({
- # 'code': 404,
- # 'msg': 'Oops! Page Not Found'
- # }), 404
- return render_template("errors/404.html"), 404
- @app.errorhandler(500)
- def server_error_page(error):
- # return "Oops! Internal server error. Please try after sometime.", 500
- return render_template("errors/500.html"), 500
- # @app.context_processor
- # def context_processor():
- """ 上下文处理 """
- # if hasattr(g," user"):
- # return {"user": g.user}
- # else:
- # return {"user": None}
- # pass
- def init_hook(app: Flask):
- @app.after_request
- def after_request(response):
- return response
- @app.before_request
- def before_request():
- # user_id = session.get("user_id")
- # if user_id:
- # try:
- # user = Users.query.get(user_id)
- # g.user = user
- # except Exception as e:
- # print(e)
- # g.user = None
- pass
- def banner():
- Version = "V 1.0\n"
- info = "\t\t\t\t\t\t\t 阿sir,我真的是卧底 | By liuyuqi.gov@msn.cn | {}".format(Version)
- banner1 = """
- 4444444444444444444
- {}""".format(
- info
- )
- print(banner1)
|