forked from elastic/package-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategories.go
115 lines (96 loc) · 2.73 KB
/
categories.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// 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"
"fmt"
"net/http"
"sort"
"strconv"
"time"
"github.com/elastic/package-registry/util"
)
type Category struct {
Id string `yaml:"id" json:"id"`
Title string `yaml:"title" json:"title"`
Count int `yaml:"count" json:"count"`
}
// categoriesHandler is a dynamic handler as it will also allow filtering in the future.
func categoriesHandler(packagesBasePaths []string, cacheTime time.Duration) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
packages, err := util.GetPackages(packagesBasePaths)
if err != nil {
notFoundError(w, err)
return
}
query := r.URL.Query()
var experimental bool
// Read query filter params which can affect the output
if len(query) > 0 {
if v := query.Get("experimental"); v != "" {
experimental, err = strconv.ParseBool(v)
if err != nil {
badRequest(w, fmt.Sprintf("invalid 'experimental' query param: '%s'", v))
return
}
}
}
packageList := map[string]util.Package{}
// Get unique list of newest packages
for _, p := range packages {
// Skip internal packages
if p.Internal {
continue
}
// Skip experimental packages if flag is not specified
if p.Release == util.ReleaseExperimental && !experimental {
continue
}
// Check if the version exists and if it should be added or not.
// If the package in the list is newer or equal, do nothing.
if pp, ok := packageList[p.Name]; ok && pp.IsNewerOrEqual(p) {
continue
}
// Otherwise delete and later add the new one.
packageList[p.Name] = p
}
categories := map[string]*Category{}
for _, p := range packageList {
for _, c := range p.Categories {
if _, ok := categories[c]; !ok {
categories[c] = &Category{
Id: c,
Title: c,
Count: 0,
}
}
categories[c].Count = categories[c].Count + 1
}
}
data, err := getCategoriesOutput(categories)
if err != nil {
notFoundError(w, err)
return
}
cacheHeaders(w, cacheTime)
jsonHeader(w)
fmt.Fprint(w, string(data))
}
}
func getCategoriesOutput(categories map[string]*Category) ([]byte, error) {
var keys []string
for k := range categories {
keys = append(keys, k)
}
sort.Strings(keys)
var outputCategories []*Category
for _, k := range keys {
c := categories[k]
if title, ok := util.CategoryTitles[c.Title]; ok {
c.Title = title
}
outputCategories = append(outputCategories, c)
}
return json.MarshalIndent(outputCategories, "", " ")
}