mirror of
https://github.com/umputun/reproxy.git
synced 2025-09-16 08:46:17 +02:00
add headers mw
This commit is contained in:
33
README.md
33
README.md
@@ -1,3 +1,36 @@
|
||||
# docker-proxy
|
||||
|
||||
Simple edge HTTP(s) proxy for docker containers
|
||||
|
||||
```
|
||||
Application Options:
|
||||
-l, --listen= listen on host:port (default: 127.0.0.1:8080) [$LISTEN]
|
||||
-t, --timeout= proxy timeout (default: 5s) [$TIMEOUT]
|
||||
--max= max response size (default: 64000) [$MAX_SIZE]
|
||||
-g, --gzip enable gz compression [$GZIP]
|
||||
-x, --header= proxy headers [$HEADER]
|
||||
--dbg debug mode [$DEBUG]
|
||||
|
||||
assets:
|
||||
-a, --assets.location= assets location [$ASSETS_LOCATION]
|
||||
--assets.root= assets web root (default: /) [$ASSETS_ROOT]
|
||||
|
||||
docker:
|
||||
--docker.enabled enable docker provider [$DOCKER_ENABLED]
|
||||
--docker.host= docker host (default: unix:///var/run/docker.sock) [$DOCKER_HOST]
|
||||
--docker.exclude= excluded containers [$DOCKER_EXCLUDE]
|
||||
|
||||
file:
|
||||
--file.enabled enable file provider [$FILE_ENABLED]
|
||||
--file.name= file name (default: dpx.conf) [$FILE_NAME]
|
||||
--file.interval= file check interval (default: 3s) [$FILE_INTERVAL]
|
||||
--file.delay= file event delay (default: 500ms) [$FILE_DELAY]
|
||||
|
||||
static:
|
||||
--static.enabled enable static provider [$STATIC_ENABLED]
|
||||
--static.rule= routing rules [$STATIC_RULES]
|
||||
|
||||
Help Options:
|
||||
-h, --help Show this help message
|
||||
|
||||
```
|
11
app/main.go
11
app/main.go
@@ -20,10 +20,11 @@ import (
|
||||
)
|
||||
|
||||
var opts struct {
|
||||
Listen string `short:"l" long:"listen" env:"LISTEN" default:"127.0.0.1:8080" description:"listen on host:port"`
|
||||
TimeOut time.Duration `short:"t" long:"timeout" env:"TIMEOUT" default:"5s" description:"proxy timeout"`
|
||||
MaxSize int64 `long:"m" long:"max" env:"MAX_SIZE" default:"64000" description:"max response size"`
|
||||
GzipEnabled bool `short:"g" long:"gzip" env:"GZIP" description:"enable gz compression"`
|
||||
Listen string `short:"l" long:"listen" env:"LISTEN" default:"127.0.0.1:8080" description:"listen on host:port"`
|
||||
TimeOut time.Duration `short:"t" long:"timeout" env:"TIMEOUT" default:"5s" description:"proxy timeout"`
|
||||
MaxSize int64 `long:"m" long:"max" env:"MAX_SIZE" default:"64000" description:"max response size"`
|
||||
GzipEnabled bool `short:"g" long:"gzip" env:"GZIP" description:"enable gz compression"`
|
||||
ProxyHeaders []string `short:"x" long:"header" env:"HEADER" description:"proxy headers"`
|
||||
|
||||
Assets struct {
|
||||
Location string `short:"a" long:"location" env:"LOCATION" default:"" description:"assets location"`
|
||||
@@ -44,7 +45,7 @@ var opts struct {
|
||||
} `group:"file" namespace:"file" env-namespace:"FILE"`
|
||||
|
||||
Static struct {
|
||||
Enabled bool `long:"enabled" env:"ENABLED" description:"enable file provider"`
|
||||
Enabled bool `long:"enabled" env:"ENABLED" description:"enable static provider"`
|
||||
Rules []string `long:"rule" env:"RULES" description:"routing rules"`
|
||||
} `group:"static" namespace:"static" env-namespace:"STATIC"`
|
||||
|
||||
|
25
app/proxy/middleware/headers.go
Normal file
25
app/proxy/middleware/headers.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Headers middleware adds headers to request
|
||||
func Headers(headers []string) func(http.Handler) http.Handler {
|
||||
|
||||
return func(h http.Handler) http.Handler {
|
||||
|
||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||
for _, h := range headers {
|
||||
elems := strings.Split(h, ":")
|
||||
if len(elems) != 2 {
|
||||
continue
|
||||
}
|
||||
r.Header.Set(strings.TrimSpace(elems[0]), strings.TrimSpace(elems[1]))
|
||||
}
|
||||
h.ServeHTTP(w, r)
|
||||
}
|
||||
return http.HandlerFunc(fn)
|
||||
}
|
||||
}
|
23
app/proxy/middleware/headers_test.go
Normal file
23
app/proxy/middleware/headers_test.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestHeaders(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/something", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
h := Headers([]string{"h1:v1", "bad", "h2:v2"})(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
|
||||
h.ServeHTTP(w, req)
|
||||
resp := w.Result()
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
t.Logf("%+v", req.Header)
|
||||
assert.Equal(t, "v1", req.Header.Get("h1"))
|
||||
assert.Equal(t, "v2", req.Header.Get("h2"))
|
||||
assert.Equal(t, 2, len(req.Header))
|
||||
}
|
@@ -21,6 +21,7 @@ type Http struct {
|
||||
AssetsWebRoot string
|
||||
MaxBodySize int64
|
||||
GzEnabled bool
|
||||
ProxyHeaders []string
|
||||
Version string
|
||||
}
|
||||
|
||||
@@ -41,6 +42,7 @@ func (h *Http) Do(ctx context.Context) error {
|
||||
rest.Ping,
|
||||
logger.New(logger.Prefix("[DEBUG] PROXY")).Handler,
|
||||
rest.SizeLimit(h.MaxBodySize),
|
||||
middleware.Headers(h.ProxyHeaders),
|
||||
h.gzipHandler(),
|
||||
),
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
|
Reference in New Issue
Block a user