diff --git a/config/config.go b/config/config.go index b60c81e3..05ded60d 100644 --- a/config/config.go +++ b/config/config.go @@ -406,7 +406,7 @@ func Configure() error { configurators.Bool(&SoReuseport, "IMGPROXY_SO_REUSEPORT") - configurators.String(&PathPrefix, "IMGPROXY_PATH_PREFIX") + configurators.URLPath(&PathPrefix, "IMGPROXY_PATH_PREFIX") configurators.MegaInt(&MaxSrcResolution, "IMGPROXY_MAX_SRC_RESOLUTION") configurators.Int(&MaxSrcFileSize, "IMGPROXY_MAX_SRC_FILE_SIZE") @@ -449,7 +449,7 @@ func Configure() error { configurators.Bool(&EnforceAvif, "IMGPROXY_ENFORCE_AVIF") configurators.Bool(&EnableClientHints, "IMGPROXY_ENABLE_CLIENT_HINTS") - configurators.String(&HealthCheckPath, "IMGPROXY_HEALTH_CHECK_PATH") + configurators.URLPath(&HealthCheckPath, "IMGPROXY_HEALTH_CHECK_PATH") if err := configurators.ImageTypes(&PreferredFormats, "IMGPROXY_PREFERRED_FORMATS"); err != nil { return err diff --git a/config/configurators/configurators.go b/config/configurators/configurators.go index 945d21da..de3052a1 100644 --- a/config/configurators/configurators.go +++ b/config/configurators/configurators.go @@ -102,6 +102,25 @@ func Bool(b *bool, name string) { } } +func URLPath(s *string, name string) { + if env := os.Getenv(name); len(env) > 0 { + if i := strings.IndexByte(env, '?'); i >= 0 { + env = env[:i] + } + if i := strings.IndexByte(env, '#'); i >= 0 { + env = env[:i] + } + if len(env) > 0 && env[len(env)-1] == '/' { + env = env[:len(env)-1] + } + if len(env) > 0 && env[0] != '/' { + env = "/" + env + } + + *s = env + } +} + func ImageTypes(it *[]imagetype.Type, name string) error { if env := os.Getenv(name); len(env) > 0 { parts := strings.Split(env, ",") diff --git a/healthcheck.go b/healthcheck.go index df69fd4a..9843795e 100644 --- a/healthcheck.go +++ b/healthcheck.go @@ -19,7 +19,7 @@ func healthcheck() int { configurators.String(&network, "IMGPROXY_NETWORK") configurators.String(&bind, "IMGPROXY_BIND") - configurators.String(&pathprefix, "IMGPROXY_PATH_PREFIX") + configurators.URLPath(&pathprefix, "IMGPROXY_PATH_PREFIX") httpc := http.Client{ Transport: &http.Transport{