fixed middleware implementation, added middleware tracing for easier debug

This commit is contained in:
yusing
2024-10-02 13:55:41 +08:00
parent d172552fb0
commit ba13b81b0e
31 changed files with 561 additions and 196 deletions

View File

@@ -3,6 +3,7 @@ package middleware
import (
"net/http"
"github.com/yusing/go-proxy/internal/common"
D "github.com/yusing/go-proxy/internal/docker"
E "github.com/yusing/go-proxy/internal/error"
)
@@ -34,9 +35,11 @@ var ModifyResponse = func() (mr *modifyResponse) {
func NewModifyResponse(optsRaw OptionsRaw) (*Middleware, E.NestedError) {
mr := new(modifyResponse)
mr.m = &Middleware{
impl: mr,
modifyResponse: mr.modifyResponse,
mr.m = &Middleware{impl: mr}
if common.IsDebug {
mr.m.modifyResponse = mr.modifyResponseWithTrace
} else {
mr.m.modifyResponse = mr.modifyResponse
}
mr.modifyResponseOpts = new(modifyResponseOpts)
err := Deserialize(optsRaw, mr.modifyResponseOpts)
@@ -58,3 +61,10 @@ func (mr *modifyResponse) modifyResponse(resp *http.Response) error {
}
return nil
}
func (mr *modifyResponse) modifyResponseWithTrace(resp *http.Response) error {
mr.m.AddTraceResponse("before modify response", resp)
err := mr.modifyResponse(resp)
mr.m.AddTraceResponse("after modify response", resp)
return err
}