__init__.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @Contact : liuyuqi.gov@msn.cn
  5. @Time : 2023/11/09 14:27:17
  6. @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
  7. @Desc : create flask app
  8. '''
  9. import os
  10. from flask import Flask
  11. from apps.config import config
  12. from apps.views import init_blueprints
  13. from apps.extensions import init_plugins
  14. def create_app(config_name="default") -> Flask:
  15. ''' create app '''
  16. config_name = os.getenv('FLASK_CONFIG') or config_name
  17. # CORS(app, supports_credentials=True, resources={
  18. # r"/api/*": {'origins': "*"}
  19. # })
  20. app = Flask(config[config_name].BASE_DIR)
  21. app.config.from_object(config[config_name]) # 读取配置
  22. config[config_name].init_app(app)
  23. init_dir()
  24. init_blueprints(app)
  25. init_plugins(app)
  26. init_hook(app)
  27. return app
  28. def init_dir():
  29. ''' init dir '''
  30. files = [
  31. 'tmp/ct', 'tmp/draw',
  32. 'tmp/image', 'tmp/mask', 'tmp/uploads'
  33. ]
  34. for ff in files:
  35. if not os.path.exists(ff):
  36. os.makedirs(ff)
  37. def init_hook(app: Flask):
  38. ''' init hook '''
  39. @app.after_request
  40. def after_request(response):
  41. ''' resolve cross-domain '''
  42. # response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
  43. # response.headers["Expires"] = 0
  44. # response.headers["Pragma"] = "no-cache"
  45. # response.set_cookie('remember_token', '', expires=0)
  46. # response.headers.add('X-Version', app.config['CURRENT_VERSION'])
  47. # response.headers.add('X-Env', app.config['DEPLOY_ENV'])
  48. response.headers['Access-Control-Allow-Origin'] = '*'
  49. response.headers['Access-Control-Allow-Credentials'] = 'true'
  50. response.headers['Access-Control-Allow-Methods'] = 'POST'
  51. response.headers['Access-Control-Allow-Headers'] = 'Content-Type, X-Requested-With'
  52. return response
  53. @app.before_request
  54. def before_request():
  55. pass