Skip to content

Commit

Permalink
reverseproxy: support routing by host
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Jun 22, 2024
1 parent 879a633 commit 085e5cb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions reverseproxy/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
8 changes: 4 additions & 4 deletions reverseproxy/go.sum
Original file line number Diff line number Diff line change
@@ -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=
22 changes: 16 additions & 6 deletions reverseproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"fmt"
"io"
"log"
Expand All @@ -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)
Expand All @@ -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())
Expand All @@ -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
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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
Expand Down

0 comments on commit 085e5cb

Please sign in to comment.