diff --git a/CHANGELOG.md b/CHANGELOG.md index 33d1cae92..ed4d29230 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +* Allow to set cache times through config. [#271](https://github.com/elastic/integrations-registry/pull/271) ### Deprecated diff --git a/categories.go b/categories.go index ce7c448b3..afc604b3b 100644 --- a/categories.go +++ b/categories.go @@ -9,6 +9,7 @@ import ( "fmt" "net/http" "sort" + "time" "github.com/elastic/package-registry/util" ) @@ -20,7 +21,7 @@ type Category struct { } // categoriesHandler is a dynamic handler as it will also allow filtering in the future. -func categoriesHandler(packagesBasePath, cacheTime string) func(w http.ResponseWriter, r *http.Request) { +func categoriesHandler(packagesBasePath string, cacheTime time.Duration) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { cacheHeaders(w, cacheTime) diff --git a/config.yml b/config.yml index 1ec710136..c7b0fde04 100644 --- a/config.yml +++ b/config.yml @@ -1 +1,5 @@ public_dir: "./public" + +cache_time.search: 10m +cache_time.categories: 10m +cache_time.catch_all: 10m diff --git a/handler.go b/handler.go index ec0a6932c..3d78a8256 100644 --- a/handler.go +++ b/handler.go @@ -10,6 +10,7 @@ import ( "net/http" "os" "path/filepath" + "time" ) func notFound(w http.ResponseWriter, err error) { @@ -20,7 +21,7 @@ func notFound(w http.ResponseWriter, err error) { http.Error(w, errString, http.StatusNotFound) } -func catchAll(publicPath, cacheTime string) func(w http.ResponseWriter, r *http.Request) { +func catchAll(publicPath string, cacheTime time.Duration) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { cacheHeaders(w, cacheTime) @@ -92,7 +93,8 @@ func sendHeader(w http.ResponseWriter, r *http.Request) { } } -func cacheHeaders(w http.ResponseWriter, cacheTime string) { - w.Header().Add("Cache-Control", "max-age="+cacheTime) +func cacheHeaders(w http.ResponseWriter, cacheTime time.Duration) { + maxAge := fmt.Sprintf("max-age=%.0f", cacheTime.Seconds()) + w.Header().Add("Cache-Control", maxAge) w.Header().Add("Cache-Control", "public") } diff --git a/main.go b/main.go index e7a458f1d..78736e4aa 100644 --- a/main.go +++ b/main.go @@ -11,8 +11,8 @@ import ( "net/http" "os" "os/signal" - "strconv" "syscall" + "time" "github.com/elastic/package-registry/util" @@ -30,10 +30,12 @@ var ( address string configPath = "config.yml" - // Cache times for the different endpoints - searchCacheTime = strconv.Itoa(10 * 60) // 10 min - categoriesCacheTime = strconv.Itoa(10 * 60) // 10 min - catchAllCacheTime = strconv.Itoa(10 * 60) // 10 min + defaultConfig = Config{ + PublicDir: "config.yml", + CacheTimeSearch: 10 * time.Minute, + CacheTimeCategories: 10 * time.Minute, + CacheTimeCatchAll: 10 * time.Minute, + } ) func init() { @@ -41,7 +43,10 @@ func init() { } type Config struct { - PublicDir string `config:"public_dir"` + PublicDir string `config:"public_dir"` + CacheTimeSearch time.Duration `config:"cache_time.search"` + CacheTimeCategories time.Duration `config:"cache_time.categories"` + CacheTimeCatchAll time.Duration `config:"cache_time.catch_all"` } func main() { @@ -90,22 +95,23 @@ func getConfig() (*Config, error) { return nil, err } - config := &Config{} - err = cfg.Unpack(config) + config := defaultConfig + err = cfg.Unpack(&config) if err != nil { return nil, err } - return config, nil + log.Println(config) + return &config, nil } func getRouter(config Config, packagesBasePath string) *mux.Router { router := mux.NewRouter().StrictSlash(true) - router.HandleFunc("/search", searchHandler(packagesBasePath, searchCacheTime)) - router.HandleFunc("/categories", categoriesHandler(packagesBasePath, categoriesCacheTime)) + router.HandleFunc("/search", searchHandler(packagesBasePath, config.CacheTimeSearch)) + router.HandleFunc("/categories", categoriesHandler(packagesBasePath, config.CacheTimeCategories)) router.HandleFunc("/health", healthHandler) - router.PathPrefix("/").HandlerFunc(catchAll(config.PublicDir, catchAllCacheTime)) + router.PathPrefix("/").HandlerFunc(catchAll(config.PublicDir, config.CacheTimeCatchAll)) return router } diff --git a/main_test.go b/main_test.go index 4e49db406..2ae782392 100644 --- a/main_test.go +++ b/main_test.go @@ -6,10 +6,12 @@ package main import ( "flag" + "fmt" "io/ioutil" "net/http" "net/http/httptest" "testing" + "time" "github.com/gorilla/mux" @@ -18,7 +20,7 @@ import ( var ( generateFlag = flag.Bool("generate", false, "Write golden files") - testCacheTime = "1" + testCacheTime = 1 * time.Second ) func TestEndpoints(t *testing.T) { @@ -82,5 +84,6 @@ 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=" + testCacheTime, "public"}) + cacheTime := fmt.Sprintf("%.0f", testCacheTime.Seconds()) + assert.Equal(t, recorder.Header()["Cache-Control"], []string{"max-age=" + cacheTime, "public"}) } diff --git a/search.go b/search.go index 280570639..55eac68e7 100644 --- a/search.go +++ b/search.go @@ -11,13 +11,14 @@ import ( "sort" "strconv" "strings" + "time" "github.com/blang/semver" "github.com/elastic/package-registry/util" ) -func searchHandler(packagesBasePath, cacheTime string) func(w http.ResponseWriter, r *http.Request) { +func searchHandler(packagesBasePath string, cacheTime time.Duration) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { cacheHeaders(w, cacheTime)