mirror of
https://github.com/yusing/godoxy.git
synced 2025-07-22 12:24:02 +02:00
feat: add a add_prefix
middleware (#51)
This will allow you to translate: `foo.mydomain.com` => `192.168.1.99:8000/foo` (for example)
This commit is contained in:
parent
9e83fe7329
commit
7dd00d2424
2 changed files with 60 additions and 1 deletions
|
@ -2,6 +2,7 @@ package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -10,11 +11,12 @@ type (
|
||||||
ModifyRequestOpts
|
ModifyRequestOpts
|
||||||
Tracer
|
Tracer
|
||||||
}
|
}
|
||||||
// order: set_headers -> add_headers -> hide_headers
|
// order: add_prefix -> set_headers -> add_headers -> hide_headers
|
||||||
ModifyRequestOpts struct {
|
ModifyRequestOpts struct {
|
||||||
SetHeaders map[string]string
|
SetHeaders map[string]string
|
||||||
AddHeaders map[string]string
|
AddHeaders map[string]string
|
||||||
HideHeaders []string
|
HideHeaders []string
|
||||||
|
AddPrefix string
|
||||||
|
|
||||||
needVarSubstitution bool
|
needVarSubstitution bool
|
||||||
}
|
}
|
||||||
|
@ -30,6 +32,8 @@ func (mr *ModifyRequestOpts) finalize() {
|
||||||
// before implements RequestModifier.
|
// before implements RequestModifier.
|
||||||
func (mr *modifyRequest) before(w http.ResponseWriter, r *http.Request) (proceed bool) {
|
func (mr *modifyRequest) before(w http.ResponseWriter, r *http.Request) (proceed bool) {
|
||||||
mr.AddTraceRequest("before modify request", r)
|
mr.AddTraceRequest("before modify request", r)
|
||||||
|
|
||||||
|
mr.addPrefix(r, nil, r.URL.Path)
|
||||||
mr.modifyHeaders(r, nil, r.Header)
|
mr.modifyHeaders(r, nil, r.Header)
|
||||||
mr.AddTraceRequest("after modify request", r)
|
mr.AddTraceRequest("after modify request", r)
|
||||||
return true
|
return true
|
||||||
|
@ -77,3 +81,11 @@ func (mr *ModifyRequestOpts) modifyHeaders(req *http.Request, resp *http.Respons
|
||||||
delete(headers, k)
|
delete(headers, k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (mr *modifyRequest) addPrefix(r *http.Request, _ *http.Response, path string) {
|
||||||
|
if len(mr.AddPrefix) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
r.URL.Path = filepath.Join(mr.AddPrefix, path)
|
||||||
|
}
|
||||||
|
|
|
@ -95,4 +95,51 @@ func TestModifyRequest(t *testing.T) {
|
||||||
|
|
||||||
ExpectEqual(t, result.RequestHeaders.Get("X-Test-Arg-Arg_1"), "b")
|
ExpectEqual(t, result.RequestHeaders.Get("X-Test-Arg-Arg_1"), "b")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("add_prefix", func(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
path string
|
||||||
|
expectedPath string
|
||||||
|
upstreamURL string
|
||||||
|
addPrefix string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "no prefix",
|
||||||
|
path: "/foo",
|
||||||
|
expectedPath: "/foo",
|
||||||
|
upstreamURL: "http://test.example.com",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "slash only",
|
||||||
|
path: "/",
|
||||||
|
expectedPath: "/",
|
||||||
|
upstreamURL: "http://test.example.com",
|
||||||
|
addPrefix: "/", // should not change anything
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "some prefix",
|
||||||
|
path: "/test",
|
||||||
|
expectedPath: "/foo/test",
|
||||||
|
upstreamURL: "http://test.example.com",
|
||||||
|
addPrefix: "/foo",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
reqURL := types.MustParseURL("https://my.app" + tt.path)
|
||||||
|
upstreamURL := types.MustParseURL(tt.upstreamURL)
|
||||||
|
|
||||||
|
opts["add_prefix"] = tt.addPrefix
|
||||||
|
result, err := newMiddlewareTest(ModifyRequest, &testArgs{
|
||||||
|
middlewareOpt: opts,
|
||||||
|
reqURL: reqURL,
|
||||||
|
upstreamURL: upstreamURL,
|
||||||
|
})
|
||||||
|
ExpectNoError(t, err)
|
||||||
|
ExpectEqual(t, result.RequestHeaders.Get("X-Test-Req-Path"), tt.expectedPath)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue