-
Notifications
You must be signed in to change notification settings - Fork 15
/
http.go
329 lines (268 loc) · 9.25 KB
/
http.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// SPDX-License-Identifier: MIT OR Unlicense
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
str "github.com/boyter/go-string"
"github.com/boyter/gocodewalker"
"github.com/rs/zerolog/log"
"html"
"html/template"
"net/http"
"os"
"runtime"
"sort"
"strconv"
"strings"
)
func StartHttpServer() {
http.HandleFunc("/file/raw/", func(w http.ResponseWriter, r *http.Request) {
path := strings.Replace(r.URL.Path, "/file/raw/", "", 1)
log.Info().
Str("unique_code", "f24a4b1d").
Str("path", path).
Msg("raw page")
w.Header().Set("Content-Type", "text/plain")
http.ServeFile(w, r, path)
})
http.HandleFunc("/file/", func(w http.ResponseWriter, r *http.Request) {
startTime := makeTimestampMilli()
startPos := tryParseInt(r.URL.Query().Get("sp"), 0)
endPos := tryParseInt(r.URL.Query().Get("ep"), 0)
path := strings.Replace(r.URL.Path, "/file/", "", 1)
log.Info().
Str("unique_code", "9212b49c").
Int("startpos", startPos).
Int("endpos", endPos).
Str("path", path).
Msg("file view page")
var content []byte
var err error
if strings.TrimSpace(Directory) != "" {
path = "/" + path
}
content, err = os.ReadFile(path)
if err != nil {
log.Error().
Str("unique_code", "d063c1fd").
Int("startpos", startPos).
Int("endpos", endPos).
Str("path", path).
Msg("error reading file")
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
}
// Create a random str to define where the start and end of
// out highlight should be which we swap out later after we have
// HTML escaped everything
md5Digest := md5.New()
fmtBegin := hex.EncodeToString(md5Digest.Sum([]byte(fmt.Sprintf("begin_%d", makeTimestampNano()))))
fmtEnd := hex.EncodeToString(md5Digest.Sum([]byte(fmt.Sprintf("end_%d", makeTimestampNano()))))
coloredContent := str.HighlightString(string(content), [][]int{{startPos, endPos}}, fmtBegin, fmtEnd)
coloredContent = html.EscapeString(coloredContent)
coloredContent = strings.Replace(coloredContent, fmtBegin, fmt.Sprintf(`<strong id="%d">`, startPos), -1)
coloredContent = strings.Replace(coloredContent, fmtEnd, "</strong>", -1)
t := template.Must(template.New("display.tmpl").Parse(httpFileTemplate))
if DisplayTemplate != "" {
t = template.Must(template.New("display.tmpl").ParseFiles(DisplayTemplate))
}
err = t.Execute(w, fileDisplay{
Location: path,
Content: template.HTML(coloredContent),
RuntimeMilliseconds: makeTimestampMilli() - startTime,
})
if err != nil {
panic(err)
}
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
startTime := makeTimestampMilli()
query := r.URL.Query().Get("q")
snippetLength := tryParseInt(r.URL.Query().Get("ss"), 300)
ext := r.URL.Query().Get("ext")
page := tryParseInt(r.URL.Query().Get("p"), 0)
pageSize := 20
var results []*FileJob
var fileCount int64
log.Info().
Str("unique_code", "1e38548a").
Msg("search page")
if query != "" {
log.Info().
Str("unique_code", "1a54b0cd").
Str("query", query).
Int("snippetlength", snippetLength).
Str("ext", ext).
Msg("search query")
// If the user asks we should look back till we find the .git or .hg directory and start the search from there
dirFilePaths = []string{"."}
if strings.TrimSpace(Directory) != "" {
dirFilePaths = []string{Directory}
}
if FindRoot {
dirFilePaths[0] = gocodewalker.FindRepositoryRoot(dirFilePaths[0])
}
if len(ext) != 0 {
AllowListExtensions = []string{ext}
} else {
AllowListExtensions = []string{}
}
// walk back through the query to see if we have a shorter one that matches
files := FindFiles(query)
toProcessQueue := make(chan *FileJob, runtime.NumCPU()) // Files to be read into memory for processing
summaryQueue := make(chan *FileJob, runtime.NumCPU()) // Files that match and need to be displayed
q, fuzzy := PreParseQuery(strings.Fields(query))
fileReaderWorker := NewFileReaderWorker(files, toProcessQueue)
fileReaderWorker.FuzzyMatch = fuzzy
fileSearcher := NewSearcherWorker(toProcessQueue, summaryQueue)
fileSearcher.SearchString = q
resultSummarizer := NewResultSummarizer(summaryQueue)
resultSummarizer.FileReaderWorker = fileReaderWorker
resultSummarizer.SnippetCount = SnippetCount
go fileReaderWorker.Start()
go fileSearcher.Start()
for f := range summaryQueue {
results = append(results, f)
}
fileCount = fileReaderWorker.GetFileCount()
rankResults(int(fileReaderWorker.GetFileCount()), results)
}
// Create a random str to define where the start and end of
// out highlight should be which we swap out later after we have
// HTML escaped everything
md5Digest := md5.New()
fmtBegin := hex.EncodeToString(md5Digest.Sum([]byte(fmt.Sprintf("begin_%d", makeTimestampNano()))))
fmtEnd := hex.EncodeToString(md5Digest.Sum([]byte(fmt.Sprintf("end_%d", makeTimestampNano()))))
documentTermFrequency := calculateDocumentTermFrequency(results)
var searchResults []searchResult
extensionFacets := map[string]int{}
// if we have more than the page size of results, lets just show the first page
displayResults := results
pages := calculatePages(results, pageSize, query, snippetLength, ext)
if displayResults != nil && len(displayResults) > pageSize {
displayResults = displayResults[:pageSize]
}
if page != 0 && page <= len(pages) {
end := page*pageSize + pageSize
if end > len(results) {
end = len(results)
}
displayResults = results[page*pageSize : end]
}
// loop over all results so we can get the facets
for _, res := range results {
extensionFacets[gocodewalker.GetExtension(res.Filename)] = extensionFacets[gocodewalker.GetExtension(res.Filename)] + 1
}
for _, res := range displayResults {
v3 := extractRelevantV3(res, documentTermFrequency, snippetLength)[0]
// We have the snippet so now we need to highlight it
// we get all the locations that fall in the snippet length
// and then remove the length of the snippet cut which
// makes out location line up with the snippet size
var l [][]int
for _, value := range res.MatchLocations {
for _, s := range value {
if s[0] >= v3.StartPos && s[1] <= v3.EndPos {
s[0] = s[0] - v3.StartPos
s[1] = s[1] - v3.StartPos
l = append(l, s)
}
}
}
// We want to escape the output, so we highlight, then escape then replace
// our special start and end strings with actual HTML
coloredContent := v3.Content
// If endpos = 0 don't highlight anything because it means its a filename match
if v3.EndPos != 0 {
coloredContent = str.HighlightString(v3.Content, l, fmtBegin, fmtEnd)
coloredContent = html.EscapeString(coloredContent)
coloredContent = strings.Replace(coloredContent, fmtBegin, "<strong>", -1)
coloredContent = strings.Replace(coloredContent, fmtEnd, "</strong>", -1)
}
searchResults = append(searchResults, searchResult{
Title: res.Location,
Location: res.Location,
Content: []template.HTML{template.HTML(coloredContent)},
StartPos: v3.StartPos,
EndPos: v3.EndPos,
Score: res.Score,
})
}
t := template.Must(template.New("search.tmpl").Parse(httpSearchTemplate))
if SearchTemplate != "" {
// If we have been supplied a template then load it up
t = template.Must(template.New("search.tmpl").ParseFiles(SearchTemplate))
}
err := t.Execute(w, search{
SearchTerm: query,
SnippetSize: snippetLength,
Results: searchResults,
ResultsCount: len(results),
RuntimeMilliseconds: makeTimestampMilli() - startTime,
ProcessedFileCount: fileCount,
ExtensionFacet: calculateExtensionFacet(extensionFacets, query, snippetLength),
Pages: pages,
Ext: ext,
})
if err != nil {
panic(err)
}
})
log.Info().Str("unique_code", "03148801").Str("address", Address).Msg("ready to serve requests")
log.Fatal().Msg(http.ListenAndServe(Address, nil).Error())
}
func calculateExtensionFacet(extensionFacets map[string]int, query string, snippetLength int) []facetResult {
var ef []facetResult
for k, v := range extensionFacets {
ef = append(ef, facetResult{
Title: k,
Count: v,
SearchTerm: query,
SnippetSize: snippetLength,
})
}
sort.Slice(ef, func(i, j int) bool {
// If the same count sort by the name to ensure it's consistent on the display
if ef[i].Count == ef[j].Count {
return strings.Compare(ef[i].Title, ef[j].Title) < 0
}
return ef[i].Count > ef[j].Count
})
return ef
}
func calculatePages(results []*FileJob, pageSize int, query string, snippetLength int, ext string) []pageResult {
var pages []pageResult
if len(results) == 0 {
return pages
}
if len(results) <= pageSize {
pages = append(pages, pageResult{
SearchTerm: query,
SnippetSize: snippetLength,
Value: 0,
Name: "1",
})
return pages
}
a := 1
if len(results)%pageSize == 0 {
a = 0
}
for i := 0; i < len(results)/pageSize+a; i++ {
pages = append(pages, pageResult{
SearchTerm: query,
SnippetSize: snippetLength,
Value: i,
Name: strconv.Itoa(i + 1),
Ext: ext,
})
}
return pages
}
func tryParseInt(x string, def int) int {
t, err := strconv.Atoi(x)
if err != nil {
return def
}
return t
}