1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-01-08 10:45:04 +02:00

Fallback image http code config (#589)

This commit is contained in:
Svyatoslav Kryukov 2021-03-19 12:28:23 +03:00 committed by GitHub
parent 098f6b9fc9
commit b93aa2f144
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 6 deletions

View File

@ -279,9 +279,10 @@ type config struct {
WatermarkURL string
WatermarkOpacity float64
FallbackImageData string
FallbackImagePath string
FallbackImageURL string
FallbackImageData string
FallbackImagePath string
FallbackImageURL string
FallbackImageHTTPCode int
NewRelicAppName string
NewRelicKey string
@ -328,6 +329,7 @@ var conf = config{
UserAgent: fmt.Sprintf("imgproxy/%s", version),
Presets: make(presets),
WatermarkOpacity: 1,
FallbackImageHTTPCode: 200,
BugsnagStage: "production",
HoneybadgerEnv: "production",
SentryEnvironment: "production",
@ -451,6 +453,7 @@ func configure() error {
strEnvConfig(&conf.FallbackImageData, "IMGPROXY_FALLBACK_IMAGE_DATA")
strEnvConfig(&conf.FallbackImagePath, "IMGPROXY_FALLBACK_IMAGE_PATH")
strEnvConfig(&conf.FallbackImageURL, "IMGPROXY_FALLBACK_IMAGE_URL")
intEnvConfig(&conf.FallbackImageHTTPCode, "IMGPROXY_FALLBACK_IMAGE_HTTP_CODE")
strEnvConfig(&conf.NewRelicAppName, "IMGPROXY_NEW_RELIC_APP_NAME")
strEnvConfig(&conf.NewRelicKey, "IMGPROXY_NEW_RELIC_KEY")
@ -574,6 +577,10 @@ func configure() error {
return fmt.Errorf("Watermark opacity should be less than or equal to 1")
}
if conf.FallbackImageHTTPCode < 100 || conf.FallbackImageHTTPCode > 599 {
return fmt.Errorf("Fallback image HTTP code should be between 100 and 599")
}
if len(conf.PrometheusBind) > 0 && conf.PrometheusBind == conf.Bind {
return fmt.Errorf("Can't use the same binding for the main server and Prometheus")
}

View File

@ -185,6 +185,7 @@ You can set up a fallback image that will be used in case imgproxy can't fetch t
* `IMGPROXY_FALLBACK_IMAGE_DATA`: Base64-encoded image data. You can easily calculate it with `base64 tmp/fallback.png | tr -d '\n'`;
* `IMGPROXY_FALLBACK_IMAGE_PATH`: path to the locally stored image;
* `IMGPROXY_FALLBACK_IMAGE_URL`: fallback image URL.
* `IMGPROXY_FALLBACK_IMAGE_HTTP_CODE`: HTTP code for the fallback image response. Default: `200`.
## Skip processing

View File

@ -16,6 +16,10 @@ var (
fallbackImage *imageData
)
const (
fallbackImageUsedCtxKey = ctxKey("fallbackImageUsed")
)
func initProcessingHandler() error {
var err error
@ -94,12 +98,16 @@ func respondWithImage(ctx context.Context, reqID string, r *http.Request, rw htt
}
rw.Header().Set("Content-Length", strconv.Itoa(len(data)))
rw.WriteHeader(200)
statusCode := 200
if getFallbackImageUsed(ctx) {
statusCode = conf.FallbackImageHTTPCode
}
rw.WriteHeader(statusCode)
rw.Write(data)
imageURL := getImageURL(ctx)
logResponse(reqID, r, 200, nil, &imageURL, po)
logResponse(reqID, r, statusCode, nil, &imageURL, po)
}
func respondWithNotModified(ctx context.Context, reqID string, r *http.Request, rw http.ResponseWriter) {
@ -158,12 +166,13 @@ func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
}
logWarning("Could not load image. Using fallback image: %s", err.Error())
ctx = setFallbackImageUsedCtx(ctx)
ctx = context.WithValue(ctx, imageDataCtxKey, fallbackImage)
}
checkTimeout(ctx)
if conf.ETagEnabled {
if conf.ETagEnabled && !getFallbackImageUsed(ctx) {
eTag := calcETag(ctx)
rw.Header().Set("ETag", eTag)
@ -206,3 +215,12 @@ func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
respondWithImage(ctx, reqID, r, rw, imageData)
}
func setFallbackImageUsedCtx(ctx context.Context) context.Context {
return context.WithValue(ctx, fallbackImageUsedCtxKey, true)
}
func getFallbackImageUsed(ctx context.Context) bool {
result, _ := ctx.Value(fallbackImageUsedCtxKey).(bool)
return result
}