|
|
|
@ -1,4 +1,3 @@
|
|
|
|
import json
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import logging
|
|
|
|
import time
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
@ -7,7 +6,6 @@ from flask import request
|
|
|
|
from configs import dify_config
|
|
|
|
from configs import dify_config
|
|
|
|
from contexts.wrapper import RecyclableContextVar
|
|
|
|
from contexts.wrapper import RecyclableContextVar
|
|
|
|
from dify_app import DifyApp
|
|
|
|
from dify_app import DifyApp
|
|
|
|
from extensions.ext_logging import get_request_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------
|
|
|
|
# ----------------------------
|
|
|
|
@ -27,21 +25,17 @@ def create_flask_app_with_configs() -> DifyApp:
|
|
|
|
# add an unique identifier to each request
|
|
|
|
# add an unique identifier to each request
|
|
|
|
RecyclableContextVar.increment_thread_recycles()
|
|
|
|
RecyclableContextVar.increment_thread_recycles()
|
|
|
|
|
|
|
|
|
|
|
|
# log request data info
|
|
|
|
|
|
|
|
request_id = get_request_id()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
if (
|
|
|
|
if (
|
|
|
|
request.method.lower() == "post"
|
|
|
|
request.method.lower() == "post"
|
|
|
|
and "application/json" in request.headers.get("content-type", "").lower()
|
|
|
|
and "application/json" in request.headers.get("content-type", "").lower()
|
|
|
|
):
|
|
|
|
):
|
|
|
|
logging.info(
|
|
|
|
logging.info(
|
|
|
|
f"[before request]|request_id: {request_id},"
|
|
|
|
f"[before request]| method: "
|
|
|
|
f" method: {request.method}, url: {request.url}, request_data: {request.get_json()}"
|
|
|
|
f"{request.method}, url: {request.url}, request_data: {request.get_json()}"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
logging.info(f"[before request]|request_id: {request_id}, method: {request.method}, url: {request.url}")
|
|
|
|
logging.info(f"[before request]| method: {request.method}, url: {request.url}")
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
logging.info(f"before_request handler err {e}")
|
|
|
|
logging.info(f"before_request handler err {e}")
|
|
|
|
|
|
|
|
|
|
|
|
@ -50,10 +44,7 @@ def create_flask_app_with_configs() -> DifyApp:
|
|
|
|
def add_extra_info(resp):
|
|
|
|
def add_extra_info(resp):
|
|
|
|
obj = resp.get_json()
|
|
|
|
obj = resp.get_json()
|
|
|
|
if obj is not None and isinstance(obj, dict):
|
|
|
|
if obj is not None and isinstance(obj, dict):
|
|
|
|
request_id = get_request_id()
|
|
|
|
logging.info(f"[finish request]|response: {obj}")
|
|
|
|
obj["request_id"] = request_id
|
|
|
|
|
|
|
|
resp.set_data(json.dumps(obj))
|
|
|
|
|
|
|
|
logging.info(f"[finish request]|request_id: {request_id}, response: {obj}")
|
|
|
|
|
|
|
|
return resp
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
|
|
|
return dify_app
|
|
|
|
return dify_app
|
|
|
|
|