Skip to content

Commit

Permalink
(non-breaking) rebrand changed environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
yusing committed Nov 3, 2024
1 parent cf1ecbc commit 99e43fe
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 38 deletions.
5 changes: 3 additions & 2 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@ runtimes:
- [email protected]
# This is the section where you manage your linters. (https://docs.trunk.io/check/configuration)
lint:
disabled:
- markdownlint
- yamllint
enabled:
- [email protected]
- [email protected]
- [email protected]
- git-diff-check
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
actions:
disabled:
- trunk-announce
Expand Down
72 changes: 38 additions & 34 deletions internal/common/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,33 @@ import (
)

var (
NoSchemaValidation = GetEnvBool("GOPROXY_NO_SCHEMA_VALIDATION", true)
IsTest = GetEnvBool("GOPROXY_TEST", false) || strings.HasSuffix(os.Args[0], ".test")
IsDebug = GetEnvBool("GOPROXY_DEBUG", IsTest)
IsDebugSkipAuth = GetEnvBool("GOPROXY_DEBUG_SKIP_AUTH", false)
IsTrace = GetEnvBool("GOPROXY_TRACE", false) && IsDebug
prefixes = []string{"GODOXY_", "GOPROXY_", ""}

NoSchemaValidation = GetEnvBool("NO_SCHEMA_VALIDATION", true)
IsTest = GetEnvBool("TEST", false) || strings.HasSuffix(os.Args[0], ".test")
IsDebug = GetEnvBool("DEBUG", IsTest)
IsDebugSkipAuth = GetEnvBool("DEBUG_SKIP_AUTH", false)
IsTrace = GetEnvBool("TRACE", false) && IsDebug

ProxyHTTPAddr,
ProxyHTTPHost,
ProxyHTTPPort,
ProxyHTTPURL = GetAddrEnv("GOPROXY_HTTP_ADDR", ":80", "http")
ProxyHTTPURL = GetAddrEnv("HTTP_ADDR", ":80", "http")

ProxyHTTPSAddr,
ProxyHTTPSHost,
ProxyHTTPSPort,
ProxyHTTPSURL = GetAddrEnv("GOPROXY_HTTPS_ADDR", ":443", "https")
ProxyHTTPSURL = GetAddrEnv("HTTPS_ADDR", ":443", "https")

APIHTTPAddr,
APIHTTPHost,
APIHTTPPort,
APIHTTPURL = GetAddrEnv("GOPROXY_API_ADDR", "127.0.0.1:8888", "http")
APIHTTPURL = GetAddrEnv("API_ADDR", "127.0.0.1:8888", "http")

APIJWTSecret = decodeJWTKey(GetEnv("GOPROXY_API_JWT_SECRET", ""))
APIJWTTokenTTL = GetDurationEnv("GOPROXY_API_JWT_TOKEN_TTL", time.Hour)
APIUser = GetEnv("GOPROXY_API_USER", "admin")
APIPasswordHash = HashPassword(GetEnv("GOPROXY_API_PASSWORD", "password"))
APIJWTSecret = decodeJWTKey(GetEnvString("API_JWT_SECRET", ""))
APIJWTTokenTTL = GetDurationEnv("API_JWT_TOKEN_TTL", time.Hour)
APIUser = GetEnvString("API_USER", "admin")
APIPasswordHash = HashPassword(GetEnvString("API_PASSWORD", "password"))
)

func init() {
Expand All @@ -45,28 +47,38 @@ func init() {
}
}

func GetEnvBool(key string, defaultValue bool) bool {
value, ok := os.LookupEnv(key)
func GetEnv[T any](key string, defaultValue T, parser func(string) (T, error)) T {
var value string
var ok bool
for _, prefix := range prefixes {
value, ok = os.LookupEnv(prefix + key)
if ok && value != "" {
break
}
}
if !ok || value == "" {
return defaultValue
}
b, err := strconv.ParseBool(value)
if err != nil {
log.Fatal().Msgf("env %s: invalid boolean value: %s", key, value)
parsed, err := parser(value)
if err == nil {
return parsed
}
return b
log.Fatal().Err(err).Msgf("env %s: invalid %T value: %s", key, parsed, value)
return defaultValue
}

func GetEnv(key, defaultValue string) string {
value, ok := os.LookupEnv(key)
if !ok || value == "" {
value = defaultValue
}
return value
func GetEnvString(key string, defaultValue string) string {
return GetEnv(key, defaultValue, func(s string) (string, error) {
return s, nil
})
}

func GetEnvBool(key string, defaultValue bool) bool {
return GetEnv(key, defaultValue, strconv.ParseBool)
}

func GetAddrEnv(key, defaultValue, scheme string) (addr, host, port, fullURL string) {
addr = GetEnv(key, defaultValue)
addr = GetEnvString(key, defaultValue)
host, port, err := net.SplitHostPort(addr)
if err != nil {
log.Fatal().Msgf("env %s: invalid address: %s", key, addr)
Expand All @@ -79,13 +91,5 @@ func GetAddrEnv(key, defaultValue, scheme string) (addr, host, port, fullURL str
}

func GetDurationEnv(key string, defaultValue time.Duration) time.Duration {
value, ok := os.LookupEnv(key)
if !ok || value == "" {
return defaultValue
}
d, err := time.ParseDuration(value)
if err != nil {
log.Fatal().Msgf("env %s: invalid duration value: %s", key, value)
}
return d
return GetEnv(key, defaultValue, time.ParseDuration)
}
2 changes: 1 addition & 1 deletion internal/route/provider/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var (

func DockerProviderImpl(name, dockerHost string, explicitOnly bool) (ProviderImpl, error) {
if dockerHost == common.DockerHostFromEnv {
dockerHost = common.GetEnv("DOCKER_HOST", client.DefaultDockerHost)
dockerHost = common.GetEnvString("DOCKER_HOST", client.DefaultDockerHost)
}
return &DockerProvider{
name,
Expand Down
2 changes: 1 addition & 1 deletion internal/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

var (
branch = common.GetEnv("GOPROXY_BRANCH", "v0.7")
branch = common.GetEnvString("BRANCH", "v0.7")
baseURL = "https://github.com/yusing/go-proxy/raw/" + branch
requiredConfigs = []Config{
{common.ConfigBasePath, true, false, ""},
Expand Down

0 comments on commit 99e43fe

Please sign in to comment.