2024-11-23 17:50:32 +08:00
|
|
|
|
# flask_app/start_up.py
|
2024-10-12 18:01:59 +08:00
|
|
|
|
|
2024-11-23 17:50:32 +08:00
|
|
|
|
import logging
|
2024-11-25 16:24:38 +08:00
|
|
|
|
from flask import Flask, request, g
|
2024-11-23 17:50:32 +08:00
|
|
|
|
|
2024-11-25 09:15:56 +08:00
|
|
|
|
from flask_app.ConnectionLimiter import ConnectionLimiter
|
2024-11-23 17:50:32 +08:00
|
|
|
|
from flask_app.logger_setup import CSTFormatter, create_logger
|
|
|
|
|
from flask_app.routes.get_deviation import get_deviation_bp
|
|
|
|
|
from flask_app.routes.little_zbparse import little_zbparse_bp
|
|
|
|
|
from flask_app.routes.upload import upload_bp
|
|
|
|
|
from flask_app.routes.test_zbparse import test_zbparse_bp
|
|
|
|
|
|
2024-11-25 09:15:56 +08:00
|
|
|
|
class FlaskAppWithLimiter(Flask):
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
super().__init__(*args, **kwargs)
|
2024-11-25 16:04:53 +08:00
|
|
|
|
# 初始化一个字典来存储每个蓝图的限流器
|
|
|
|
|
self.connection_limiters = {}
|
2024-11-23 17:50:32 +08:00
|
|
|
|
def create_app():
|
2024-11-25 09:15:56 +08:00
|
|
|
|
app = FlaskAppWithLimiter(__name__)
|
2024-11-23 17:50:32 +08:00
|
|
|
|
|
|
|
|
|
# 设置日志的全局配置(如果需要)
|
|
|
|
|
handler = logging.StreamHandler()
|
|
|
|
|
handler.setFormatter(CSTFormatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
|
|
|
|
|
app.logger.addHandler(handler)
|
|
|
|
|
app.logger.setLevel(logging.INFO)
|
2024-11-26 11:32:24 +08:00
|
|
|
|
# @app.before_request
|
|
|
|
|
# def before_request():
|
|
|
|
|
# """
|
|
|
|
|
# 每个请求开始前初始化 logger 和 output_folder,
|
|
|
|
|
# 根据请求的端点选择不同的子文件夹。
|
|
|
|
|
# """
|
|
|
|
|
# # 确定当前请求的端点
|
|
|
|
|
# blueprint = request.blueprint
|
|
|
|
|
# # 映射端点到子文件夹
|
|
|
|
|
# subfolder_map = {
|
|
|
|
|
# 'get_deviation': 'output3',
|
|
|
|
|
# 'little_zbparse': 'output2',
|
|
|
|
|
# 'upload': 'output1',
|
|
|
|
|
# 'test_zbparse': 'test_output'
|
|
|
|
|
# }
|
|
|
|
|
# # 获取对应的子文件夹,默认为 'output1'
|
|
|
|
|
# subfolder = subfolder_map.get(blueprint, 'output1')
|
|
|
|
|
# # 创建 logger 和 output_folder
|
|
|
|
|
# create_logger(app, subfolder)
|
2024-08-29 16:37:09 +08:00
|
|
|
|
|
2024-11-23 17:50:32 +08:00
|
|
|
|
# 注册蓝图
|
|
|
|
|
app.register_blueprint(get_deviation_bp)
|
|
|
|
|
app.register_blueprint(little_zbparse_bp)
|
|
|
|
|
app.register_blueprint(upload_bp)
|
|
|
|
|
app.register_blueprint(test_zbparse_bp)
|
2024-11-26 11:32:24 +08:00
|
|
|
|
app.connection_limiters['upload'] = ConnectionLimiter(max_connections=10)
|
|
|
|
|
app.connection_limiters['get_deviation'] = ConnectionLimiter(max_connections=10)
|
2024-11-25 16:04:53 +08:00
|
|
|
|
app.connection_limiters['default'] = ConnectionLimiter(max_connections=10)
|
2024-11-25 16:24:38 +08:00
|
|
|
|
|
|
|
|
|
@app.teardown_request
|
|
|
|
|
def teardown_request(exception):
|
|
|
|
|
limiter = getattr(g, 'limiter', None)
|
|
|
|
|
monitor = getattr(g, 'monitor', None)
|
|
|
|
|
if limiter and not monitor.is_timeout:
|
|
|
|
|
limiter.semaphore.release()
|
2024-11-23 17:50:32 +08:00
|
|
|
|
return app
|
2024-08-30 11:56:11 +08:00
|
|
|
|
|
2024-11-23 17:50:32 +08:00
|
|
|
|
#TODO:培训要求、总体要求、进度要求、'建设要求'到技术要求中,归类到其他要求中
|
2024-11-25 16:27:29 +08:00
|
|
|
|
|
2024-08-29 16:37:09 +08:00
|
|
|
|
if __name__ == '__main__':
|
2024-11-23 17:50:32 +08:00
|
|
|
|
app = create_app()
|
2024-11-25 16:54:43 +08:00
|
|
|
|
app.run(debug=True, host='0.0.0.0', port=5001)
|