mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 04:50:52 +08:00
refactor(handlers): improve request logging and payload handling
This commit is contained in:
@@ -116,19 +116,29 @@ func (h *BaseAPIHandler) GetContextWithCancel(handler interfaces.APIHandler, c *
|
|||||||
newCtx = context.WithValue(newCtx, "gin", c)
|
newCtx = context.WithValue(newCtx, "gin", c)
|
||||||
newCtx = context.WithValue(newCtx, "handler", handler)
|
newCtx = context.WithValue(newCtx, "handler", handler)
|
||||||
return newCtx, func(params ...interface{}) {
|
return newCtx, func(params ...interface{}) {
|
||||||
if h.Cfg.RequestLog {
|
if h.Cfg.RequestLog && len(params) == 1 {
|
||||||
if len(params) == 1 {
|
var payload []byte
|
||||||
data := params[0]
|
switch data := params[0].(type) {
|
||||||
switch data.(type) {
|
case []byte:
|
||||||
case []byte:
|
payload = data
|
||||||
appendAPIResponse(c, data.([]byte))
|
case error:
|
||||||
case error:
|
if data != nil {
|
||||||
appendAPIResponse(c, []byte(data.(error).Error()))
|
payload = []byte(data.Error())
|
||||||
case string:
|
|
||||||
appendAPIResponse(c, []byte(data.(string)))
|
|
||||||
case bool:
|
|
||||||
case nil:
|
|
||||||
}
|
}
|
||||||
|
case string:
|
||||||
|
payload = []byte(data)
|
||||||
|
}
|
||||||
|
if len(payload) > 0 {
|
||||||
|
if existing, exists := c.Get("API_RESPONSE"); exists {
|
||||||
|
if existingBytes, ok := existing.([]byte); ok && len(existingBytes) > 0 {
|
||||||
|
trimmedPayload := bytes.TrimSpace(payload)
|
||||||
|
if len(trimmedPayload) > 0 && bytes.Contains(existingBytes, trimmedPayload) {
|
||||||
|
cancel()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
appendAPIResponse(c, payload)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user