1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-02-12 11:46:10 +02:00

Add cache control

This commit is contained in:
DarthSim 2017-07-03 16:10:44 +06:00
parent 1ba9360599
commit 4773874fcd
2 changed files with 11 additions and 0 deletions

View File

@ -63,6 +63,8 @@ type config struct {
ReadTimeout int
WriteTimeout int
TTL int
MaxSrcDimension int
Quality int
@ -78,6 +80,7 @@ var conf = config{
Bind: ":8080",
ReadTimeout: 10,
WriteTimeout: 10,
TTL: 3600,
MaxSrcDimension: 4096,
Quality: 80,
GZipCompression: 5,
@ -92,6 +95,8 @@ func init() {
intEnvConfig(&conf.ReadTimeout, "IMGPROXY_READ_TIMEOUT")
intEnvConfig(&conf.WriteTimeout, "IMGPROXY_WRITE_TIMEOUT")
intEnvConfig(&conf.TTL, "IMGPROXY_TTL")
intEnvConfig(&conf.MaxSrcDimension, "IMGPROXY_MAX_SRC_DIMENSION")
intEnvConfig(&conf.Quality, "IMGPROXY_QUALITY")
@ -124,6 +129,10 @@ func init() {
log.Fatalf("Write timeout should be greater than 0, now - %d\n", conf.WriteTimeout)
}
if conf.TTL <= 0 {
log.Fatalf("TTL should be greater than 0, now - %d\n", conf.TTL)
}
if conf.MaxSrcDimension <= 0 {
log.Fatalf("Max src dimension should be greater than 0, now - %d\n", conf.MaxSrcDimension)
}

View File

@ -93,6 +93,8 @@ func logResponse(status int, msg string) {
func respondWithImage(r *http.Request, rw http.ResponseWriter, data []byte, imgURL string, po processingOptions, startTime time.Time) {
gzipped := strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") && conf.GZipCompression > 0
rw.Header().Set("Expires", time.Now().Add(time.Second*time.Duration(conf.TTL)).Format(http.TimeFormat))
rw.Header().Set("Cache-Control", fmt.Sprintf("Cache-Control: max-age=%d", conf.TTL))
rw.Header().Set("Content-Type", imageContentType(data))
if gzipped {
rw.Header().Set("Content-Encoding", "gzip")