forked from hound-search/hound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
252 lines (209 loc) · 5.1 KB
/
api.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package api
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"time"
"github.com/etsy/hound/config"
"github.com/etsy/hound/index"
"github.com/etsy/hound/searcher"
)
const (
defaultLinesOfContext uint = 2
maxLinesOfContext uint = 20
)
type Stats struct {
FilesOpened int
Duration int
}
func writeJson(w http.ResponseWriter, data interface{}, status int) {
w.Header().Set("Content-Type", "application/json;charset=utf-8")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(data); err != nil {
log.Panicf("Failed to encode JSON: %v\n", err)
}
}
func writeResp(w http.ResponseWriter, data interface{}) {
writeJson(w, data, http.StatusOK)
}
func writeError(w http.ResponseWriter, err error, status int) {
writeJson(w, map[string]string{
"Error": err.Error(),
}, status)
}
type searchResponse struct {
repo string
res *index.SearchResponse
err error
}
/**
* Searches all repos in parallel.
*/
func searchAll(
query string,
opts *index.SearchOptions,
repos []string,
idx map[string]*searcher.Searcher,
filesOpened *int,
duration *int) (map[string]*index.SearchResponse, error) {
startedAt := time.Now()
n := len(repos)
// use a buffered channel to avoid routine leaks on errs.
ch := make(chan *searchResponse, n)
for _, repo := range repos {
go func(repo string) {
fms, err := idx[repo].Search(query, opts)
ch <- &searchResponse{repo, fms, err}
}(repo)
}
res := map[string]*index.SearchResponse{}
for i := 0; i < n; i++ {
r := <-ch
if r.err != nil {
return nil, r.err
}
if r.res.Matches == nil {
continue
}
res[r.repo] = r.res
*filesOpened += r.res.FilesOpened
}
*duration = int(time.Now().Sub(startedAt).Seconds() * 1000)
return res, nil
}
// Used for parsing flags from form values.
func parseAsBool(v string) bool {
v = strings.ToLower(v)
return v == "true" || v == "1" || v == "fosho"
}
func parseAsRepoList(v string, idx map[string]*searcher.Searcher) []string {
v = strings.TrimSpace(strings.ToLower(v))
var repos []string
if v == "*" {
for repo, _ := range idx {
repos = append(repos, repo)
}
return repos
}
for _, repo := range strings.Split(v, ",") {
if idx[repo] == nil {
continue
}
repos = append(repos, repo)
}
return repos
}
func parseAsUintValue(sv string, min, max, def uint) uint {
iv, err := strconv.ParseUint(sv, 10, 54)
if err != nil {
return def
}
if max != 0 && uint(iv) > max {
return max
}
if min != 0 && uint(iv) < min {
return max
}
return uint(iv)
}
func parseRangeInt(v string, i *int) {
*i = 0
if v == "" {
return
}
vi, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return
}
*i = int(vi)
}
func parseRangeValue(rv string) (int, int) {
ix := strings.Index(rv, ":")
if ix < 0 {
return 0, 0
}
var b, e int
parseRangeInt(rv[:ix], &b)
parseRangeInt(rv[ix+1:], &e)
return b, e
}
func Setup(m *http.ServeMux, idx map[string]*searcher.Searcher) {
m.HandleFunc("/api/v1/repos", func(w http.ResponseWriter, r *http.Request) {
res := map[string]*config.Repo{}
for name, srch := range idx {
res[name] = srch.Repo
}
writeResp(w, res)
})
m.HandleFunc("/api/v1/search", func(w http.ResponseWriter, r *http.Request) {
var opt index.SearchOptions
stats := parseAsBool(r.FormValue("stats"))
repos := parseAsRepoList(r.FormValue("repos"), idx)
query := r.FormValue("q")
opt.Offset, opt.Limit = parseRangeValue(r.FormValue("rng"))
opt.FileRegexp = r.FormValue("files")
opt.IgnoreCase = parseAsBool(r.FormValue("i"))
opt.LinesOfContext = parseAsUintValue(
r.FormValue("ctx"),
0,
maxLinesOfContext,
defaultLinesOfContext)
var filesOpened int
var durationMs int
results, err := searchAll(query, &opt, repos, idx, &filesOpened, &durationMs)
if err != nil {
// TODO(knorton): Return ok status because the UI expects it for now.
writeError(w, err, http.StatusOK)
return
}
var res struct {
Results map[string]*index.SearchResponse
Stats *Stats `json:",omitempty"`
}
res.Results = results
if stats {
res.Stats = &Stats{
FilesOpened: filesOpened,
Duration: durationMs,
}
}
writeResp(w, &res)
})
m.HandleFunc("/api/v1/excludes", func(w http.ResponseWriter, r *http.Request) {
repo := r.FormValue("repo")
res := idx[repo].GetExcludedFiles()
w.Header().Set("Content-Type", "application/json;charset=utf-8")
w.Header().Set("Access-Control-Allow", "*")
fmt.Fprint(w, res)
})
m.HandleFunc("/api/v1/update", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
writeError(w,
errors.New(http.StatusText(http.StatusMethodNotAllowed)),
http.StatusMethodNotAllowed)
return
}
repos := parseAsRepoList(r.FormValue("repos"), idx)
for _, repo := range repos {
searcher := idx[repo]
if searcher == nil {
writeError(w,
fmt.Errorf("No such repository: %s", repo),
http.StatusNotFound)
return
}
if !searcher.Update() {
writeError(w,
fmt.Errorf("Push updates are not enabled for repository %s", repo),
http.StatusForbidden)
return
}
}
writeResp(w, "ok")
})
}