Toggle navigation
Toggle navigation
This project
Loading...
Sign in
lizhengwei
/
aigc-embedding-service
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
wangdongxu
2026-04-02 17:15:47 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
6b065fc6613fa475df7c6fb0c4f5e82bde0d0bd1
6b065fc6
1 parent
60fed58e
jira:NYJ-1460 desc:init
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
71 additions
and
65 deletions
api/http_log.py
api/router.py
cfg/config.yaml
api/http_log.py
View file @
6b065fc
import
json
import
time
import
logging
logger
=
logging
.
getLogger
(
__name__
)
from
fastapi
import
FastAPI
,
Request
from
fastapi
import
Request
from
starlette.middleware.base
import
BaseHTTPMiddleware
from
starlette.responses
import
StreamingResponse
from
config
import
settings
class
LoggingMiddleware
(
BaseHTTPMiddleware
):
"""HTTP请求/响应日志中间件."""
# 日志记录路径前缀
async
def
dispatch
(
self
,
request
:
Request
,
call_next
):
# 记录请求开始时间
start_time
=
time
.
time
()
# 获取请求信息
method
=
request
.
method
url
=
str
(
request
.
url
)
client_host
=
request
.
client
.
host
if
request
.
client
else
"unknown"
# 读取请求体
request_body
=
None
if
method
in
(
"POST"
,
"PUT"
,
"PATCH"
):
try
:
body
=
await
request
.
body
()
if
body
:
request_body
=
body
.
decode
(
"utf-8"
,
errors
=
"replace"
)
# 尝试格式化为JSON
try
:
request_body
=
json
.
loads
(
request_body
)
except
json
.
JSONDecodeError
:
pass
# 保持原始字符串
except
Exception
:
pass
# 判断是否需要记录日志
should_log
=
request
.
url
.
path
.
startswith
(
settings
.
url_prefix
)
# 记录请求日志
logger
.
info
(
f
"→ HTTP Request | {method} {url} | Client: {client_host}"
)
if
request_body
:
logger
.
info
(
f
" Request Body: {request_body}"
)
if
should_log
:
# 记录请求开始时间
start_time
=
time
.
time
()
# 处理请求
response
=
await
call_next
(
request
)
# 读取请求体
request_body
=
None
if
method
in
(
"POST"
,
"PUT"
,
"PATCH"
):
try
:
body
=
await
request
.
body
()
if
body
:
request_body
=
body
.
decode
(
"utf-8"
,
errors
=
"replace"
)
# 尝试格式化为JSON
try
:
request_body
=
json
.
loads
(
request_body
)
except
json
.
JSONDecodeError
:
pass
# 保持原始字符串
except
Exception
:
pass
# 记录请求日志
logger
.
info
(
f
"→ HTTP Request | {method} {url} | Client: {client_host}"
)
if
request_body
:
logger
.
info
(
f
" Request Body: {request_body}"
)
# 计算处理时间
process_time
=
time
.
time
()
-
start_time
# 处理请求
response
=
await
call_next
(
request
)
# 读取响应体(仅处理非流式响应)
response_body
=
None
if
not
isinstance
(
response
,
StreamingResponse
):
try
:
response_body_bytes
=
b
""
async
for
chunk
in
response
.
body_iterator
:
response_body_bytes
+=
chunk
# 计算处理时间
process_time
=
time
.
time
()
-
start_time
# 重新构建响应
response_body
=
response_body_bytes
.
decode
(
"utf-8"
,
errors
=
"replace"
)
# 尝试格式化为JSON
# 读取响应体(仅处理非流式响应)
response_body
=
None
if
not
isinstance
(
response
,
StreamingResponse
):
try
:
response_body
=
json
.
loads
(
response_body
)
except
json
.
JSONDecodeError
:
pass
# 保持原始字符串
response_body_bytes
=
b
""
async
for
chunk
in
response
.
body_iterator
:
response_body_bytes
+=
chunk
# 重新构建响应
response_body
=
response_body_bytes
.
decode
(
"utf-8"
,
errors
=
"replace"
)
# 尝试格式化为JSON
try
:
response_body
=
json
.
loads
(
response_body
)
except
json
.
JSONDecodeError
:
pass
# 保持原始字符串
# 重建响应对象
response
=
StreamingResponse
(
iter
([
response_body_bytes
]),
status_code
=
response
.
status_code
,
headers
=
dict
(
response
.
headers
),
media_type
=
response
.
media_type
,
)
except
Exception
:
pass
# 重建响应对象
response
=
StreamingResponse
(
iter
([
response_body_bytes
]),
status_code
=
response
.
status_code
,
headers
=
dict
(
response
.
headers
),
media_type
=
response
.
media_type
,
)
except
Exception
:
pass
# 记录响应日志
status_code
=
response
.
status_code
logger
.
info
(
f
"← HTTP Response | {method} {url} | Status: {status_code} | Time: {process_time:.3f}s"
)
if
response_body
:
logger
.
info
(
f
" Response Body: {response_body}"
)
# 记录响应日志
status_code
=
response
.
status_code
logger
.
info
(
f
"← HTTP Response | {method} {url} | Status: {status_code} | Time: {process_time:.3f}s"
)
if
response_body
:
logger
.
info
(
f
" Response Body: {response_body}"
)
return
response
\ No newline at end of file
return
response
else
:
# 不需要记录日志的请求,直接处理
return
await
call_next
(
request
)
...
...
api/router.py
View file @
6b065fc
"""API路由聚合模块."""
from
fastapi
import
APIRouter
from
config
import
settings
api_router
=
APIRouter
(
prefix
=
settings
.
url_prefix
)
# 创建主路由
api_router
=
APIRouter
(
prefix
=
"/aigc-embedding/api"
)
def
register_router
(
router
:
APIRouter
,
prefix
:
str
):
api_router
.
include_router
(
router
,
prefix
=
prefix
)
...
...
cfg/config.yaml
View file @
6b065fc
...
...
@@ -2,12 +2,10 @@ app_name: "Embedding Service"
app_version
:
"
1.0.0"
port
:
8000
debug
:
false
url_prefix
:
"
/aigc-embedding/api"
db_es_enable
:
true
db_es_url
:
"
http://localhost:9200"
db_postgres_enable
:
false
db_postgres_url
:
"
postgresql://postgres:postgres@localhost:5432/postgres"
service
:
face
:
db_client
:
"
es"
#db_postgres_enable: false
#db_postgres_url: "postgresql://postgres:postgres@localhost:5432/postgres"
\ No newline at end of file
...
...
Please
register
or
login
to post a comment