From 4fe0608194fa340821971dba80baec400fcfc702 Mon Sep 17 00:00:00 2001 From: viktorstrate Date: Wed, 30 Sep 2020 14:08:30 +0200 Subject: [PATCH] Setup cache headers for photo routes --- api/routes/photos.go | 3 +++ api/server/cors_middleware.go | 9 +++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/api/routes/photos.go b/api/routes/photos.go index 6c75857c..f0f828da 100644 --- a/api/routes/photos.go +++ b/api/routes/photos.go @@ -103,6 +103,9 @@ func RegisterPhotoRoutes(db *sql.DB, router *mux.Router) { w.Header().Set("Content-Length", fmt.Sprintf("%d", stats.Size())) } + // Allow caching the resource for 1 day + w.Header().Set("Cache-Control", "private, max-age=86400, immutable") + io.Copy(w, file) }) } diff --git a/api/server/cors_middleware.go b/api/server/cors_middleware.go index 419e0f52..69e4b565 100644 --- a/api/server/cors_middleware.go +++ b/api/server/cors_middleware.go @@ -14,12 +14,13 @@ func CORSMiddleware(devMode bool) mux.MiddlewareFunc { return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { methods := []string{http.MethodGet, http.MethodPost, http.MethodOptions} - headers := []string{"authorization", "content-type", "content-length", "TokenPassword"} + requestHeaders := []string{"authorization", "content-type", "content-length", "TokenPassword"} + responseHeaders := []string{"content-length"} - w.Header().Set("Access-Control-Allow-Methods", strings.Join(methods, ",")) - w.Header().Set("Access-Control-Allow-Headers", strings.Join(headers, ",")) + w.Header().Set("Access-Control-Allow-Methods", strings.Join(methods, ", ")) + w.Header().Set("Access-Control-Allow-Headers", strings.Join(requestHeaders, ", ")) w.Header().Set("Access-Control-Allow-Credentials", "true") - w.Header().Set("Access-Control-Expose-Headers", "content-length") + w.Header().Set("Access-Control-Expose-Headers", strings.Join(responseHeaders, ", ")) endpoint := utils.ApiEndpointUrl() endpoint.Path = path.Join(endpoint.Path, "graphql")