Skip to content

Commit 30f7340

Browse files
authored
feat: validate that env vars are configured and set defaults (#30)
1 parent f0b518b commit 30f7340

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ linters:
55
- exhaustive
66
- exhaustivestruct
77
- funlen
8+
- goerr113
89
- golint
910
- interfacer
1011
- lll

main.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var version = "development"
2222

2323
func main() {
2424
if err := mainCmd(); err != nil {
25-
fmt.Printf("google-analytics-proxy: %v", err) //nolint:forbidigo
25+
fmt.Println("joshdk/google-analytics-proxy:", err) //nolint:forbidigo
2626
os.Exit(1)
2727
}
2828
}
@@ -77,12 +77,47 @@ func mainCmd() error {
7777
// Example: "true"
7878
googleAnalyticsDryRun := os.Getenv("GOOGLE_ANALYTICS_DRY_RUN")
7979

80+
// Validate that the required settings are not empty.
81+
switch {
82+
case googleAnalyticsTrackingID == "":
83+
return fmt.Errorf("GOOGLE_ANALYTICS_TRACKING_ID was not provided")
84+
case googleAnalyticsPropertyName == "":
85+
return fmt.Errorf("GOOGLE_ANALYTICS_PROPERTY_NAME was not provided")
86+
case upstreamEndpoint == "":
87+
return fmt.Errorf("UPSTREAM_ENDPOINT was not provided")
88+
}
89+
90+
// Validate the TLS settings, and set sane defaults.
91+
switch {
92+
// Validate HTTP listen mode.
93+
case tlsCertFile == "" && tlsKeyFile == "":
94+
if listenAddress == "" {
95+
// Set a default listen address if none was given.
96+
listenAddress = "0.0.0.0:8080"
97+
}
98+
// Validate HTTPS listen mode.
99+
case tlsCertFile != "" && tlsKeyFile != "":
100+
if listenAddress == "" {
101+
// Set a default listen address if none was given.
102+
listenAddress = "0.0.0.0:8443"
103+
}
104+
default:
105+
// HTTPS listen mode was only partially (mis)configured.
106+
return fmt.Errorf("TLS_CERT_PATH and TLS_KEY_PATH were not both provided")
107+
}
108+
80109
// Parse the upstream endpoint address to ensure that it's valid.
81110
upstreamURL, err := url.Parse(upstreamEndpoint)
82111
if err != nil {
83112
return err
84113
}
85114

115+
// Use the property name for the upstream hostname, if one was not
116+
// explicitly given.
117+
if upstreamHostname == "" {
118+
upstreamHostname = googleAnalyticsPropertyName
119+
}
120+
86121
// Create a reverse proxy HTTP handler for our upstream. This handler is
87122
// responsible for relaying all downstream client requests to the upstream
88123
// service, and the upstream service responses back to the downstream

0 commit comments

Comments
 (0)