app.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # File : app.py
  4. # Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
  5. # Date : 2022/9/6
  6. from flask.app import Flask
  7. from flask_migrate import Migrate
  8. from base import config
  9. from base.database import db
  10. from utils.log import logger
  11. from utils.system import get_wlan_info, getHost
  12. from controllers import *
  13. from js.rules import getRuleLists
  14. import sys
  15. def create_flask_app():
  16. app = Flask(__name__, static_folder='static', static_url_path='/static')
  17. app.config.from_object(config) # 单独的配置文件里写了,这里就不用弄json中文显示了
  18. app.register_blueprint(home.home, url_prefix='')
  19. app.register_blueprint(admin.admin, url_prefix='/admin')
  20. app.register_blueprint(vod.vod, url_prefix='')
  21. app.register_blueprint(cls.cls, url_prefix='/cls')
  22. app.register_blueprint(layui.layui, url_prefix='/layui')
  23. app.register_blueprint(parse.parse, url_prefix='/parse')
  24. # app.register_blueprint(web.web, url_prefix='/web',template_folder='templates/cmsV10/mxpro/html/index/')
  25. app.register_blueprint(web.web, url_prefix='/web')
  26. app.logger.name = "drLogger"
  27. # lsg = service.storage_service()
  28. logger.info(f"默认解析地址:{app.config.get('PLAY_URL')}")
  29. # logger.info(f"自定义播放解析地址:{lsg.getItem('PLAY_URL')}")
  30. logger.info(f'当前操作系统{sys.platform}')
  31. rule_list = getRuleLists()
  32. wlan_info, _ = get_wlan_info()
  33. logger.info(rule_list)
  34. logger.info(
  35. f'局域网: {getHost(1, app.config.get("HTTP_PORT"))}/index\n本地: {getHost(0, app.config.get("HTTP_PORT"))}/index\nwlan_info:{wlan_info}')
  36. db.init_app(app)
  37. db.app = app
  38. db.create_all(app=app)
  39. return app
  40. app = create_flask_app()
  41. migrate = Migrate(app, db)
  42. max_version = (3, 11) # 小于3.11的版本(3.6-3.10)
  43. max_version_str = ".".join([str(i) for i in max_version])
  44. now_python_ver = ".".join([str(i) for i in sys.version_info[:3]])
  45. no_error = True
  46. if sys.version_info < max_version:
  47. try:
  48. from gevent.pywsgi import WSGIServer
  49. # from gevent import monkey
  50. # monkey.patch_all() # 多线程,只能放在最开头,import其它包之前
  51. from gevent import monkey
  52. monkey.patch_socket() # 开启socket异步
  53. print(f'当前python版本{now_python_ver}为{max_version_str}及以下,支持gevent')
  54. except Exception as e:
  55. print(f'gevent使用过程中发生了错误:{e}')
  56. no_error = False
  57. else:
  58. print(f'当前python版本{now_python_ver}为{max_version_str}及以上,不支持gevent')
  59. if __name__ == "__main__":
  60. http_port = int(app.config.get('HTTP_PORT', 5705))
  61. http_host = app.config.get('HTTP_HOST', '0.0.0.0')
  62. threaded = app.config.get('THREAD')
  63. GEVENT = app.config.get('GEVENT')
  64. print(GEVENT)
  65. if threaded is None:
  66. threaded = True
  67. if GEVENT is None:
  68. debug = False
  69. # https://www.zhihu.com/question/64096559
  70. print(f'threaded:{threaded},GEVENT:{GEVENT}')
  71. # if sys.version_info < (3, 9) and not sys.platform.startswith('win'):
  72. use_gevent = sys.version_info < max_version # 是否使用协程
  73. is_debug = (not GEVENT) and sys.platform.startswith('win') # windows开调试模式
  74. print(f'开启调试模式:{is_debug}')
  75. if use_gevent and no_error and not is_debug:
  76. # server = WSGIServer(('0.0.0.0', 5705), app, handler_class=WebSocketHandler,log=app.logger)
  77. # server = WSGIServer(('0.0.0.0', 5705), app, handler_class=WebSocketHandler,log=None)
  78. server = WSGIServer((http_host, http_port), app, log=logger)
  79. server.serve_forever()
  80. else:
  81. app.run(debug=False, host=http_host, port=http_port, threaded=threaded)