mirror of
https://github.com/gomods/athens
synced 2026-02-03 11:00:32 +00:00
config: accept PORT env + ensure colon (#1181)
* config: accept PORT env + ensure colon * ensure precedence
This commit is contained in:
committed by
Aaron Schlesinger
parent
f14707e5e5
commit
71aeca7f30
+2
-2
@@ -71,8 +71,8 @@ StorageType = "memory"
|
||||
#TLSKeyFile = "server.key"
|
||||
|
||||
# Port sets the port the proxy listens on
|
||||
# Env override: ATHENS_PORT
|
||||
# Note that PORT needs to be prefixed by :
|
||||
# Env override: ATHENS_PORT or PORT
|
||||
# The PORT must be a number or a number prefixed by ":"
|
||||
Port = ":3000"
|
||||
|
||||
# The endpoint for a package registry in case of a proxy cache miss
|
||||
|
||||
@@ -160,12 +160,27 @@ func envOverride(config *Config) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
portEnv := os.Getenv("PORT")
|
||||
// ATHENS_PORT takes precedence over PORT
|
||||
if portEnv != "" && os.Getenv("ATHENS_PORT") == "" {
|
||||
config.Port = portEnv
|
||||
}
|
||||
if config.Port == "" {
|
||||
config.Port = defaultPort
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ensurePortFormat(s string) string {
|
||||
if len(s) == 0 {
|
||||
return ""
|
||||
}
|
||||
if s[0] != ':' {
|
||||
return ":" + s
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func validateConfig(config Config) error {
|
||||
validate := validator.New()
|
||||
err := validate.StructExcept(config, "Storage")
|
||||
|
||||
@@ -114,6 +114,34 @@ func TestEnvOverridesPreservingPort(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvOverridesPORT(t *testing.T) {
|
||||
conf := &Config{Port: ""}
|
||||
oldVal := os.Getenv("PORT")
|
||||
defer os.Setenv("PORT", oldVal)
|
||||
os.Setenv("PORT", "5000")
|
||||
err := envOverride(conf)
|
||||
if err != nil {
|
||||
t.Fatalf("Env override failed: %v", err)
|
||||
}
|
||||
if conf.Port != "5000" {
|
||||
t.Fatalf("expected PORT env to be 5000 but got %v", conf.Port)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsurePortFormat(t *testing.T) {
|
||||
port := "3000"
|
||||
expected := ":3000"
|
||||
given := ensurePortFormat(port)
|
||||
if given != expected {
|
||||
t.Fatalf("expected ensurePortFormat to add a colon to %v but got %v", port, given)
|
||||
}
|
||||
port = ":3000"
|
||||
given = ensurePortFormat(port)
|
||||
if given != expected {
|
||||
t.Fatalf("expected ensurePortFormat to not add a colon when it's present but got %v", given)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStorageEnvOverrides(t *testing.T) {
|
||||
expStorage := &StorageConfig{
|
||||
Disk: &DiskConfig{
|
||||
|
||||
Reference in New Issue
Block a user