Skip to content

Commit

Permalink
Refactored to use options pattern for new command line args
Browse files Browse the repository at this point in the history
Signed-off-by: Graeme Christie <gchristie@bunnings.com.au>
  • Loading branch information
graemechristie committed Jul 14, 2023
1 parent 2b26646 commit 034166d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
43 changes: 29 additions & 14 deletions injectproxy/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/http/httputil"
"net/url"
Expand Down Expand Up @@ -50,10 +49,12 @@ type routes struct {
}

type options struct {
enableLabelAPIs bool
passthroughPaths []string
errorOnReplace bool
registerer prometheus.Registerer
enableLabelAPIs bool
passthroughPaths []string
errorOnReplace bool
registerer prometheus.Registerer
extraHttpHeaders map[string]string
rewriteHostHeader string
}

type Option interface {
Expand Down Expand Up @@ -97,6 +98,24 @@ func WithErrorOnReplace() Option {
})
}

func WithExtraHttpHeaders(headers []string) Option {
return optionFunc(func(o *options) {
o.extraHttpHeaders = make(map[string]string)
for _, headerArg := range headers {
header, val, found := strings.Cut(headerArg, ":")
if found {
o.extraHttpHeaders[strings.TrimSpace(header)] = strings.TrimSpace(val)
}
}
})
}

func WithRewriteHostHeader(host string) Option {
return optionFunc(func(o *options) {
o.rewriteHostHeader = host
})
}

// mux abstracts away the behavior we expect from the http.ServeMux type in this package.
type mux interface {
http.Handler
Expand Down Expand Up @@ -262,7 +281,8 @@ func (sle StaticLabelEnforcer) ExtractLabel(next http.HandlerFunc) http.Handler
})
}

func NewRoutes(upstream *url.URL, label string, extractLabeler ExtractLabeler, extraHttpHeaders []string, rewriteHostHeader string, opts ...Option) (*routes, error) {
func NewRoutes(upstream *url.URL, label string, extractLabeler ExtractLabeler, opts ...Option) (*routes, error) {
//extraHttpHeaders []string, rewriteHostHeader string
opt := options{}
for _, o := range opts {
o.apply(&opt)
Expand All @@ -275,17 +295,12 @@ func NewRoutes(upstream *url.URL, label string, extractLabeler ExtractLabeler, e
proxy := &httputil.ReverseProxy{
Rewrite: func(r *httputil.ProxyRequest) {
r.SetURL(upstream)
if len(strings.TrimSpace(rewriteHostHeader)) == 0 {
if len(opt.rewriteHostHeader) == 0 {
r.Out.Host = r.In.Host
} else {
r.Out.Host = strings.TrimSpace(rewriteHostHeader)
r.Out.Host = opt.rewriteHostHeader
}
for _, headerArg := range extraHttpHeaders {
header, val, found := strings.Cut(headerArg, ":")
if !found {
log.Printf("Header %s specified but ':' delimited not found", headerArg)
continue
}
for header, val := range opt.extraHttpHeaders {
r.Out.Header[strings.TrimSpace(header)] = []string{strings.TrimSpace(val)}
}
},
Expand Down
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ func main() {
opts = append(opts, injectproxy.WithErrorOnReplace())
}

if len(extraHttpHeaders) > 0 {
opts = append(opts, injectproxy.WithExtraHttpHeaders(extraHttpHeaders))
}

if len(rewriteHostHeader) > 0 {
opts = append(opts, injectproxy.WithRewriteHostHeader(rewriteHostHeader))
}

var extractLabeler injectproxy.ExtractLabeler
switch {
case len(labelValues) > 0:
Expand All @@ -151,7 +159,7 @@ func main() {

{
// Run the insecure HTTP server.
routes, err := injectproxy.NewRoutes(upstreamURL, label, extractLabeler, extraHttpHeaders, rewriteHostHeader, opts...)
routes, err := injectproxy.NewRoutes(upstreamURL, label, extractLabeler, opts...)
if err != nil {
log.Fatalf("Failed to create injectproxy Routes: %v", err)
}
Expand Down

0 comments on commit 034166d

Please sign in to comment.