Skip to content

Commit

Permalink
Remove packages.path config and replace it with public_dir (#118)
Browse files Browse the repository at this point in the history
The only thing needed to start the server is having it pointed to the public directory to serve files from there. What the internal name is for the packages directory is an implementation detail.

Some refactoring of the code was needed for this change. Now path variables are passed to the handler instead of using a global variable. This also makes it simpler to test individually instead of modifying global variables.
  • Loading branch information
ruflin committed Oct 21, 2019
1 parent 32abb5c commit b63b48a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Known Issue

### Breaking change

* Remove `packages.path` config and replace it with `public_dir` config. [#](https://github.com/elastic/integrations-registry/pull/)

## [0.1.0]

First tagged release. No changelog existed so far.
2 changes: 1 addition & 1 deletion categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Category struct {
}

// categoriesHandler is a dynamic handler as it will also allow filtering in the future.
func categoriesHandler(cacheTime string) func(w http.ResponseWriter, r *http.Request) {
func categoriesHandler(packagesBasePath, cacheTime string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
cacheHeaders(w, cacheTime)

Expand Down
2 changes: 1 addition & 1 deletion config.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
packages.path: "./public/package"
public_dir: "./public"
20 changes: 13 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import (
"github.com/gorilla/mux"
)

const (
packageDir = "package"
)

var (
packagesBasePath string
address string
Expand All @@ -37,7 +41,7 @@ func init() {
}

type Config struct {
PackagesPath string `config:"packages.path"`
PublicDir string `config:"public_dir"`
}

func main() {
Expand All @@ -50,7 +54,8 @@ func main() {
log.Print(err)
os.Exit(1)
}
packagesBasePath = config.PackagesPath

packagesBasePath := config.PublicDir + "/" + packageDir

// Prefill the package cache
packages, err := util.GetPackages(packagesBasePath)
Expand All @@ -60,7 +65,7 @@ func main() {
}
log.Printf("%v package manifests loaded into memory.\n", len(packages))

server := &http.Server{Addr: address, Handler: getRouter()}
server := &http.Server{Addr: address, Handler: getRouter(*config, packagesBasePath)}

go func() {
err := server.ListenAndServe()
Expand Down Expand Up @@ -90,15 +95,16 @@ func getConfig() (*Config, error) {
if err != nil {
return nil, err
}

return config, nil
}

func getRouter() *mux.Router {
func getRouter(config Config, packagesBasePath string) *mux.Router {
router := mux.NewRouter().StrictSlash(true)

router.HandleFunc("/search", searchHandler(searchCacheTime))
router.HandleFunc("/categories", categoriesHandler(categoriesCacheTime))
router.PathPrefix("/").HandlerFunc(catchAll("./public", catchAllCacheTime))
router.HandleFunc("/search", searchHandler(packagesBasePath, searchCacheTime))
router.HandleFunc("/categories", categoriesHandler(packagesBasePath, categoriesCacheTime))
router.PathPrefix("/").HandlerFunc(catchAll(config.PublicDir, catchAllCacheTime))

return router
}
16 changes: 8 additions & 8 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (

func TestEndpoints(t *testing.T) {

packagesBasePath = "./testdata/package"
packagesBasePath := "./testdata/package"

tests := []struct {
endpoint string
Expand All @@ -32,13 +32,13 @@ func TestEndpoints(t *testing.T) {
handler func(w http.ResponseWriter, r *http.Request)
}{
{"/", "", "info.json", catchAll("./testdata", testCacheTime)},
{"/search", "/search", "search.json", searchHandler(testCacheTime)},
{"/categories", "/categories", "categories.json", categoriesHandler(testCacheTime)},
{"/search?kibana=6.5.2", "/search", "search-kibana652.json", searchHandler(testCacheTime)},
{"/search?kibana=7.2.1", "/search", "search-kibana721.json", searchHandler(testCacheTime)},
{"/search?category=metrics", "/search", "search-category-metrics.json", searchHandler(testCacheTime)},
{"/search?category=logs", "/search", "search-category-logs.json", searchHandler(testCacheTime)},
{"/search?package=example", "/search", "search-package-example.json", searchHandler(testCacheTime)},
{"/search", "/search", "search.json", searchHandler(packagesBasePath, testCacheTime)},
{"/categories", "/categories", "categories.json", categoriesHandler(packagesBasePath, testCacheTime)},
{"/search?kibana=6.5.2", "/search", "search-kibana652.json", searchHandler(packagesBasePath, testCacheTime)},
{"/search?kibana=7.2.1", "/search", "search-kibana721.json", searchHandler(packagesBasePath, testCacheTime)},
{"/search?category=metrics", "/search", "search-category-metrics.json", searchHandler(packagesBasePath, testCacheTime)},
{"/search?category=logs", "/search", "search-category-logs.json", searchHandler(packagesBasePath, testCacheTime)},
{"/search?package=example", "/search", "search-package-example.json", searchHandler(packagesBasePath, testCacheTime)},
{"/package/example-1.0.0", "", "package.json", catchAll("./testdata", testCacheTime)},
}

Expand Down
2 changes: 1 addition & 1 deletion search.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/elastic/integrations-registry/util"
)

func searchHandler(cacheTime string) func(w http.ResponseWriter, r *http.Request) {
func searchHandler(packagesBasePath, cacheTime string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
cacheHeaders(w, cacheTime)

Expand Down

0 comments on commit b63b48a

Please sign in to comment.