diff --git a/cmd/main.go b/cmd/main.go index 66fc405..0189906 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -21,7 +21,7 @@ func main() { _, _ = w.Write([]byte(`{"status":"ok"}`)) w.WriteHeader(http.StatusOK) })) - proxy := pkg.NewReverseProxy(cfg.OriginScheme, cfg.OriginBaseDomain, cfg.DefaultSubDomain, cfg.FeatureHeader) + proxy := pkg.NewReverseProxy(cfg.OriginScheme, cfg.OriginBaseDomain, cfg.DefaultSubDomain, cfg.FeatureHeader, cfg.ProxyPort) http.Handle("/", proxy) listenHost := fmt.Sprintf(":%v", port) slog.Info("Listen on", "host", listenHost) diff --git a/pkg/config.go b/pkg/config.go index 8767b19..5af84b0 100644 --- a/pkg/config.go +++ b/pkg/config.go @@ -8,6 +8,7 @@ var c *Config type Config struct { Port int `envconfig:"PORT" default:"18080"` + ProxyPort int `envconfig:"PORT" default:"443"` OriginBaseDomain string `envconfig:"ORIGIN_BASE_DOMAIN"` DefaultSubDomain string `envconfig:"DEFAULT_SUB_DOMAIN"` FeatureHeader string `envconfig:"FEATURE_HEADER" default:"X-Feature"` diff --git a/pkg/proxy.go b/pkg/proxy.go index 8b7ce60..8a045e9 100644 --- a/pkg/proxy.go +++ b/pkg/proxy.go @@ -6,6 +6,7 @@ import ( "log/slog" "net" "net/http" + "strconv" "strings" "sync" "time" @@ -20,14 +21,14 @@ type ReverseProxy struct { BaseDomain string } -func NewReverseProxy(schema string, baseDomain string, defaultSubdomain string, headerName string) *ReverseProxy { +func NewReverseProxy(schema string, baseDomain string, defaultSubdomain string, headerName string, port int) *ReverseProxy { director := func(req *http.Request) { subDomain := req.Header.Get(headerName) if subDomain == "" { subDomain = defaultSubdomain } req.URL.Scheme = schema - req.URL.Host = subDomain + "." + baseDomain + req.URL.Host = subDomain + "." + baseDomain + ":" + strconv.Itoa(port) } return &ReverseProxy{Director: director, BaseDomain: baseDomain} }