From 085e5cb3ba361ba520612b08a2640f8f11071c7a Mon Sep 17 00:00:00 2001 From: David Gamba Date: Fri, 21 Jun 2024 21:33:55 -0600 Subject: [PATCH] reverseproxy: support routing by host --- reverseproxy/go.mod | 4 ++-- reverseproxy/go.sum | 8 ++++---- reverseproxy/main.go | 22 ++++++++++++++++------ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/reverseproxy/go.mod b/reverseproxy/go.mod index 51e2fa1..8e37a4b 100644 --- a/reverseproxy/go.mod +++ b/reverseproxy/go.mod @@ -3,6 +3,6 @@ module github.com/DavidGamba/dgtools/reverseproxy go 1.17 require ( - github.com/DavidGamba/go-getoptions v0.25.3 // indirect - github.com/gorilla/mux v1.8.0 // indirect + github.com/DavidGamba/go-getoptions v0.30.0 + github.com/gorilla/mux v1.8.1 ) diff --git a/reverseproxy/go.sum b/reverseproxy/go.sum index 62a13d3..65ccbfe 100644 --- a/reverseproxy/go.sum +++ b/reverseproxy/go.sum @@ -1,4 +1,4 @@ -github.com/DavidGamba/go-getoptions v0.25.3 h1:lSPcMkwWvVZU05C+Uz4DKnKN5wz4bcD1QvJ/QHCRexo= -github.com/DavidGamba/go-getoptions v0.25.3/go.mod h1:qLaLSYeQ8sUVOfKuu5JT5qKKS3OCwyhkYSJnoG+ggmo= -github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= -github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/DavidGamba/go-getoptions v0.30.0 h1:8x69Fc8k/mEWVE0GknpwQ3uGj56MXOUp17egPxCEAG4= +github.com/DavidGamba/go-getoptions v0.30.0/go.mod h1:zE97E3PR9P3BI/HKyNYgdMlYxodcuiC6W68KIgeYT84= +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= diff --git a/reverseproxy/main.go b/reverseproxy/main.go index 8e293c7..8c48a66 100644 --- a/reverseproxy/main.go +++ b/reverseproxy/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "errors" "fmt" "io" "log" @@ -23,6 +24,9 @@ func main() { } func program(args []string) int { + ctx, cancel, done := getoptions.InterruptContext() + defer func() { cancel(); <-done }() + opt := getoptions.New() opt.Bool("help", false, opt.Alias("?")) opt.Bool("quiet", false) @@ -31,6 +35,7 @@ func program(args []string) int { opt.StringSlice("base-path", 1, 1, opt.Required()) opt.String("cert", "") opt.String("key", "") + opt.SetCommandFn(run) remaining, err := opt.Parse(args[1:]) if opt.Called("help") { fmt.Println(opt.Help()) @@ -44,12 +49,15 @@ func program(args []string) int { Logger.SetOutput(io.Discard) } - ctx, cancel, done := getoptions.InterruptContext() - defer func() { cancel(); <-done }() - - err = run(ctx, opt, remaining) + err = opt.Dispatch(ctx, remaining) if err != nil { + if errors.Is(err, getoptions.ErrorHelpCalled) { + return 1 + } fmt.Fprintf(os.Stderr, "ERROR: %s\n", err) + if errors.Is(err, getoptions.ErrorParsing) { + fmt.Fprintf(os.Stderr, "\n"+opt.Help()) + } return 1 } return 0 @@ -91,13 +99,15 @@ func run(ctx context.Context, opt *getoptions.GetOpt, args []string) error { } Logger.Printf("Target URL: host %s, path %s, scheme %s, %s", turl.Host, turl.Path, turl.Scheme, turl.String()) basePath := basePaths[i] + host := strings.Split(turl.Host, ":")[0] rp := &RP{ + host: host, targetURL: turl, basePath: basePath, proto: proto, } - r.PathPrefix(basePath).Handler(rp) + r.Host(host).PathPrefix(basePath).Handler(rp) } if cert != "" && key != "" { @@ -115,7 +125,7 @@ func run(ctx context.Context, opt *getoptions.GetOpt, args []string) error { } type RP struct { - target string + host string targetURL *url.URL basePath string proto string