diff --git a/CHANGELOG.md b/CHANGELOG.md index 20c382eeb..6f2e691a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added * Add validation check that Kibana min/max are valid semver versions. [#99](https://github.com/elastic/integrations-registry/pull/99) +* Adding Cache-Control max-age headers to all http responses set to 1h. [#101](https://github.com/elastic/integrations-registry/pull/101) ### Changed diff --git a/categories.go b/categories.go index 897b6821f..72f2fc2bf 100644 --- a/categories.go +++ b/categories.go @@ -23,6 +23,7 @@ type Category struct { // categoriesHandler is a dynamic handler as it will also allow filtering in the future. func categoriesHandler() func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { + cacheHeaders(w) packagePaths, err := util.GetPackagePaths(packagesBasePath) if err != nil { diff --git a/handler.go b/handler.go index cf0b42ed1..4ffd6358d 100644 --- a/handler.go +++ b/handler.go @@ -22,6 +22,7 @@ func notFound(w http.ResponseWriter, err error) { func catchAll(publicPath string) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { + cacheHeaders(w) path := r.RequestURI @@ -86,3 +87,8 @@ func sendHeader(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") } } + +func cacheHeaders(w http.ResponseWriter) { + w.Header().Add("Cache-Control", "max-age="+cacheTime) + w.Header().Add("Cache-Control", "public") +} diff --git a/main.go b/main.go index 50914915a..a882a139d 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "net/http" "os" "os/signal" + "strconv" "syscall" ucfgYAML "github.com/elastic/go-ucfg/yaml" @@ -22,6 +23,7 @@ var ( packagesBasePath string address string configPath = "config.yml" + cacheTime = strconv.Itoa(60 * 60) // 1 hour ) func init() { diff --git a/main_test.go b/main_test.go index f35cc674b..a8414b993 100644 --- a/main_test.go +++ b/main_test.go @@ -79,4 +79,5 @@ func runEndpoint(t *testing.T, endpoint, path, file string, handler func(w http. } assert.Equal(t, string(data), recorder.Body.String()) + assert.Equal(t, recorder.Header()["Cache-Control"], []string{"max-age=" + cacheTime, "public"}) } diff --git a/search.go b/search.go index c54ac86f5..055c8130c 100644 --- a/search.go +++ b/search.go @@ -14,6 +14,7 @@ import ( func searchHandler() func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { + cacheHeaders(w) query := r.URL.Query()