mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 04:50:52 +08:00
Add request logging capabilities to API handlers and update .gitignore
Enhance API response handling by storing responses in context and updating request logger to include API responses
This commit is contained in:
@@ -38,7 +38,7 @@ func RequestLoggingMiddleware(logger logging.RequestLogger) gin.HandlerFunc {
|
||||
c.Next()
|
||||
|
||||
// Finalize logging after request processing
|
||||
if err := wrapper.Finalize(); err != nil {
|
||||
if err = wrapper.Finalize(c); err != nil {
|
||||
// Log error but don't interrupt the response
|
||||
// In a real implementation, you might want to use a proper logger here
|
||||
}
|
||||
|
||||
@@ -134,7 +134,7 @@ func (w *ResponseWriterWrapper) processStreamingChunks() {
|
||||
}
|
||||
|
||||
// Finalize completes the logging process for the response.
|
||||
func (w *ResponseWriterWrapper) Finalize() error {
|
||||
func (w *ResponseWriterWrapper) Finalize(c *gin.Context) error {
|
||||
if !w.logger.IsEnabled() {
|
||||
return nil
|
||||
}
|
||||
@@ -171,6 +171,26 @@ func (w *ResponseWriterWrapper) Finalize() error {
|
||||
finalHeaders[key] = values
|
||||
}
|
||||
|
||||
var apiRequestBody []byte
|
||||
apiRequest, isExist := c.Get("API_REQUEST")
|
||||
if isExist {
|
||||
var ok bool
|
||||
apiRequestBody, ok = apiRequest.([]byte)
|
||||
if !ok {
|
||||
apiRequestBody = nil
|
||||
}
|
||||
}
|
||||
|
||||
var apiResponseBody []byte
|
||||
apiResponse, isExist := c.Get("API_RESPONSE")
|
||||
if isExist {
|
||||
var ok bool
|
||||
apiResponseBody, ok = apiResponse.([]byte)
|
||||
if !ok {
|
||||
apiResponseBody = nil
|
||||
}
|
||||
}
|
||||
|
||||
// Log complete non-streaming response
|
||||
return w.logger.LogRequest(
|
||||
w.requestInfo.URL,
|
||||
@@ -180,6 +200,8 @@ func (w *ResponseWriterWrapper) Finalize() error {
|
||||
finalStatusCode,
|
||||
finalHeaders,
|
||||
w.body.Bytes(),
|
||||
apiRequestBody,
|
||||
apiResponseBody,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user