Skip to content

Commit

Permalink
Add caching headers to all responses
Browse files Browse the repository at this point in the history
The content of the registry only changes if a new package is added or if the registry is updated. Because of this, all responses can be cached. Caching headers are introduced for all requests and the caching time is currently set to 1h. This could be increased in the future especially for the `catchAll` endpoint as these assets basically never change.
  • Loading branch information
ruflin committed Sep 11, 2019
1 parent 29a05e3 commit e0ef98f
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

### Changed

Expand Down
1 change: 1 addition & 0 deletions categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"os"
"os/signal"
"strconv"
"syscall"

ucfgYAML "github.com/elastic/go-ucfg/yaml"
Expand All @@ -22,6 +23,7 @@ var (
packagesBasePath string
address string
configPath = "config.yml"
cacheTime = strconv.Itoa(60 * 60) // 1 hour
)

func init() {
Expand Down
1 change: 1 addition & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
}
1 change: 1 addition & 0 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit e0ef98f

Please sign in to comment.