forked from elastic/package-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
132 lines (108 loc) · 3.09 KB
/
search.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// 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 (
"context"
"fmt"
"net/http"
"net/url"
"sort"
"strconv"
"time"
"github.com/Masterminds/semver/v3"
"github.com/pkg/errors"
"go.elastic.co/apm"
"github.com/elastic/package-registry/packages"
"github.com/elastic/package-registry/util"
)
func searchHandler(indexer Indexer, cacheTime time.Duration) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
filter, err := newSearchFilterFromQuery(r.URL.Query())
if err != nil {
badRequest(w, err.Error())
return
}
opts := packages.GetOptions{
Filter: filter,
}
packages, err := indexer.Get(r.Context(), &opts)
if err != nil {
notFoundError(w, errors.Wrapf(err, "fetching package failed"))
return
}
data, err := getPackageOutput(r.Context(), packages)
if err != nil {
notFoundError(w, err)
return
}
cacheHeaders(w, cacheTime)
jsonHeader(w)
fmt.Fprint(w, string(data))
}
}
func newSearchFilterFromQuery(query url.Values) (*packages.Filter, error) {
var filter packages.Filter
if len(query) == 0 {
return &filter, nil
}
var err error
if v := query.Get("kibana.version"); v != "" {
filter.KibanaVersion, err = semver.NewVersion(v)
if err != nil {
return nil, fmt.Errorf("invalid Kibana version '%s': %w", v, err)
}
}
if v := query.Get("category"); v != "" {
filter.Category = v
}
if v := query.Get("package"); v != "" {
filter.PackageName = v
}
if v := query.Get("type"); v != "" {
filter.PackageType = v
}
if v := query.Get("all"); v != "" {
// Default is false, also on error
filter.AllVersions, err = strconv.ParseBool(v)
if err != nil {
return nil, fmt.Errorf("invalid 'all' query param: '%s'", v)
}
}
// Deprecated: release tags to be removed.
if v := query.Get("experimental"); v != "" {
// In case of error, keep it false
filter.Experimental, err = strconv.ParseBool(v)
if err != nil {
return nil, fmt.Errorf("invalid 'experimental' query param: '%s'", v)
}
// For compatibility with older versions of Kibana.
if filter.Experimental {
filter.Prerelease = true
}
}
if v := query.Get("prerelease"); v != "" {
// In case of error, keep it false
filter.Prerelease, err = strconv.ParseBool(v)
if err != nil {
return nil, fmt.Errorf("invalid 'prerelease' query param: '%s'", v)
}
}
return &filter, nil
}
func getPackageOutput(ctx context.Context, packageList packages.Packages) ([]byte, error) {
span, ctx := apm.StartSpan(ctx, "GetPackageOutput", "app")
defer span.End()
// Packages need to be sorted to be always outputted in the same order
sort.Sort(packageList)
var output []packages.BasePackage
for _, p := range packageList {
data := p.BasePackage
output = append(output, data)
}
// Instead of return `null` in case of an empty array, return []
if len(output) == 0 {
return []byte("[]"), nil
}
return util.MarshalJSONPretty(output)
}