Skip to content

Commit

Permalink
feat: Refactor proxy configuration to include ProxyPort in main.go an…
Browse files Browse the repository at this point in the history
…d config.go (#5)
  • Loading branch information
miseyu authored Apr 21, 2024
1 parent 22cfc2b commit 87158ad
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions pkg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
5 changes: 3 additions & 2 deletions pkg/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log/slog"
"net"
"net/http"
"strconv"
"strings"
"sync"
"time"
Expand All @@ -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}
}
Expand Down

0 comments on commit 87158ad

Please sign in to comment.