Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate index.json #470

Merged
merged 9 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Use filepath.Walk to find valid package content data. [#438](https://github.com/elastic/package-registry/pull/438)
* Validate handlebarsjs stream configuration templates. [#445](https://github.com/elastic/package-registry/pull/445)
* Serve favicon as embedded resource. [#468](https://github.com/elastic/package-registry/pull/468)
* Generate index.json file. [#470](https://github.com/elastic/package-registry/pull/470)

### Deprecated

Expand Down
4 changes: 4 additions & 0 deletions docs/api/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"service.name": "package-registry",
"version": "0.4.0"
}
4 changes: 0 additions & 4 deletions docs/api/info.json

This file was deleted.

32 changes: 32 additions & 0 deletions index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package main

import (
"encoding/json"
"net/http"
"time"
)

type indexData struct {
ServiceName string `json:"service.name"`
Version string `json:"version"`
}

func indexHandler(cacheTime time.Duration) (func(w http.ResponseWriter, r *http.Request), error) {
data := indexData{
ServiceName: serviceName,
Version: version,
}
body, err := json.MarshalIndent(&data, "", " ")
if err != nil {
return nil, err
}
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
cacheHeaders(w, cacheTime)
w.Write(body)
}, nil
}
30 changes: 0 additions & 30 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ func Build() error {
return err
}
}

err = BuildRootFile()
if err != nil {
return err
}
return sh.Run("go", "build", ".")
}

Expand All @@ -95,31 +90,6 @@ func fetchPackageStorage() error {
packageStorageRevision)
}

// Creates the `index.json` file
// For now only containing the version.
func BuildRootFile() error {
rootData := map[string]string{
"version": "0.4.0",
"service.name": "package-registry",
}
return writeJsonFile(rootData, publicDir+"/index.json")
}

// Copy Favicon to public dir
func CopyFavicon() error {

favicon, err := ioutil.ReadFile("img/favicon.ico")
if err != nil {
return err
}

err = ioutil.WriteFile(publicDir+"/favicon.ico", favicon, 0644)
if err != nil {
return err
}
return nil
}

func writeJsonFile(v interface{}, path string) error {
data, err := json.MarshalIndent(v, "", " ")
if err != nil {
Expand Down
12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import (

const (
packageDir = "package"

serviceName = "package-registry"
version = "0.4.0"
)

var (
Expand Down Expand Up @@ -121,13 +124,20 @@ func getRouter(config Config, packagesBasePath string) (*mux.Router, error) {
if err != nil {
return nil, err
}
indexHandlerFunc, err := indexHandler(config.CacheTimeCatchAll)
if err != nil {
return nil, err
}

router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", indexHandlerFunc)
router.HandleFunc("/index.json", indexHandlerFunc)
router.HandleFunc("/search", searchHandler(packagesBasePath, config.CacheTimeSearch))
router.HandleFunc("/categories", categoriesHandler(packagesBasePath, config.CacheTimeCategories))
router.HandleFunc("/health", healthHandler)
router.HandleFunc("/favicon.ico", faviconHandleFunc)
router.PathPrefix("/").HandlerFunc(catchAll(http.Dir(config.PublicDir), config.CacheTimeCatchAll))
router.PathPrefix("/epr").HandlerFunc(catchAll(http.Dir(config.PublicDir), config.CacheTimeCatchAll))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mtojtek are the /epr and /package routes new?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the user perspective - no, they're already exposed.

Speaking about the implementation, this has been already adjusted (see the master branch). Archives exposed via the /epr endpoint are built dynamically, not served from the file system.

router.PathPrefix("/package").HandlerFunc(catchAll(http.Dir(config.PublicDir), config.CacheTimeCatchAll))
router.Use(loggingMiddleware)
return router, nil
}
Expand Down
6 changes: 5 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ func TestEndpoints(t *testing.T) {
faviconHandleFunc, err := faviconHandler(testCacheTime)
require.NoError(t, err)

indexHandleFunc, err := indexHandler(testCacheTime)
require.NoError(t, err)

tests := []struct {
endpoint string
path string
file string
handler func(w http.ResponseWriter, r *http.Request)
}{
{"/", "", "info.json", catchAll(http.Dir(publicPath), testCacheTime)},
{"/", "", "index.json", indexHandleFunc},
{"/index.json", "", "index.json", indexHandleFunc},
{"/search", "/search", "search.json", searchHandler(packagesBasePath, testCacheTime)},
{"/search?all=true", "/search", "search-all.json", searchHandler(packagesBasePath, testCacheTime)},
{"/categories", "/categories", "categories.json", categoriesHandler(packagesBasePath, testCacheTime)},
Expand Down
4 changes: 0 additions & 4 deletions testdata/public/index.json

This file was deleted.