Skip to content

Commit

Permalink
fix: Adding cache-control header to fix generated payload etag
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed May 18, 2022
1 parent 1f959fc commit 118fc2d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
3 changes: 3 additions & 0 deletions pkg/crud/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ func (a App) serveGeoJSON(w http.ResponseWriter, r *http.Request, request provid
hash = a.exifHash(ctx, items)
} else if exifs, err := a.exifApp.ListDir(ctx, item); err == nil {
hash = sha.New(exifs)
} else if err != nil {
logger.Error("unable to list geojson directory: %s", err)
}

etag, ok := provider.EtagMatch(w, r, hash)
Expand All @@ -178,6 +180,7 @@ func (a App) serveGeoJSON(w http.ResponseWriter, r *http.Request, request provid
}

w.Header().Add("Content-Type", "application/json; charset=utf-8")
w.Header().Add("Cache-Control", "max-age=60, must-revalidate")
w.Header().Add("Etag", etag)
w.WriteHeader(http.StatusOK)

Expand Down
8 changes: 4 additions & 4 deletions pkg/provider/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,13 @@ func WriteToStorage(ctx context.Context, storageApp absto.Storage, output string
}

// EtagMatch check that given hash match the existing etag
func EtagMatch(w http.ResponseWriter, r *http.Request, hash string) (string, bool) {
etag := fmt.Sprintf(`W/"%s"`, hash)
func EtagMatch(w http.ResponseWriter, r *http.Request, hash string) (etag string, match bool) {
etag = fmt.Sprintf(`W/"%s"`, hash)

if r.Header.Get("If-None-Match") == etag {
w.WriteHeader(http.StatusNotModified)
return etag, true
match = true
}

return etag, false
return
}
1 change: 1 addition & 0 deletions pkg/thumbnail/thumbnail.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ func (a App) List(w http.ResponseWriter, r *http.Request, item absto.Item, items
}

w.Header().Add("Content-Type", "text/plain; charset=utf-8")
w.Header().Add("Cache-Control", "max-age=60, must-revalidate")
w.Header().Add("Etag", etag)
w.WriteHeader(http.StatusOK)

Expand Down

0 comments on commit 118fc2d

Please sign in to comment.