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:
Peter Olds
2025-01-23 08:34:26 -08:00
committed by GitHub
parent 9e83fe7329
commit 7dd00d2424
2 changed files with 60 additions and 1 deletions

View File

@@ -2,6 +2,7 @@ package middleware
import (
"net/http"
"path/filepath"
"strings"
)
@@ -10,11 +11,12 @@ type (
ModifyRequestOpts
Tracer
}
// order: set_headers -> add_headers -> hide_headers
// order: add_prefix -> set_headers -> add_headers -> hide_headers
ModifyRequestOpts struct {
SetHeaders map[string]string
AddHeaders map[string]string
HideHeaders []string
AddPrefix string
needVarSubstitution bool
}
@@ -30,6 +32,8 @@ func (mr *ModifyRequestOpts) finalize() {
// before implements RequestModifier.
func (mr *modifyRequest) before(w http.ResponseWriter, r *http.Request) (proceed bool) {
mr.AddTraceRequest("before modify request", r)
mr.addPrefix(r, nil, r.URL.Path)
mr.modifyHeaders(r, nil, r.Header)
mr.AddTraceRequest("after modify request", r)
return true
@@ -77,3 +81,11 @@ func (mr *ModifyRequestOpts) modifyHeaders(req *http.Request, resp *http.Respons
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)
}