/
search.go
235 lines (219 loc) · 5.37 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
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
package search
import (
"encoding/json"
"net/http"
"os"
"regexp"
"strings"
"github.com/blevesearch/bleve/v2"
"github.com/blevesearch/bleve/v2/search/query"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"go.uber.org/zap"
)
type Search struct {
Root string `json:"root,omitempty"`
Endpoint string `json:"endpoint,omitempty"` // default: /search
Regexp string `json:"regexp,omitempty"`
logger *zap.Logger
watch *NotifyFile
index bleve.Index
}
func (Search) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers." + DirectiveName,
New: func() caddy.Module { return new(Search) },
}
}
// Provision sets up the module. #caddy.Provisioner
func (sch *Search) Provision(ctx caddy.Context) error {
sch.logger = ctx.Logger(sch)
if sch.Root == "" {
sch.Root = "."
}
if sch.Endpoint == "" {
sch.Endpoint = "/search"
}
if sch.Regexp == "" {
sch.Regexp = ".*.md"
}
index, err := CreateIndex()
if err != nil {
sch.logger.Debug("watch CreateIndex Error", zap.Error(err))
}
sch.index = index
// 监听文件变化
sch.logger.Debug("Provision", zap.String("root", sch.Root))
sch.watch = NewNotifyFile(sch.logger, sch.IndexDoc)
go sch.watch.WatchDir(sch.Root)
return nil
}
// Validate ensures md has a valid configuration. #caddy.Validator
// Validate should be a read-only function. It is run after the Provision() method.
func (sch *Search) Validate() error {
return nil
}
// ServeHTTP implements #caddyhttp.MiddlewareHandler.
func (sch *Search) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) (err error) {
if r.URL.Path == sch.Endpoint {
if r.Header.Get("Accept") == "application/json" {
return sch.SearchJSON(w, r)
}
return sch.SearchHTML(w, r)
}
return next.ServeHTTP(w, r)
}
type Result struct {
Path string
Body []string
}
func (sch *Search) searchResult(r *http.Request) []Result {
q := r.URL.Query().Get("q")
indexResult := sch.Search(q)
results := make([]Result, 0, len(indexResult))
for key, value := range indexResult {
results = append(results, Result{
Path: key,
Body: value,
})
}
return results
}
// SearchJSON renders the search results in JSON format
func (sch *Search) SearchJSON(w http.ResponseWriter, r *http.Request) error {
results := sch.searchResult(r)
jresp, err := json.Marshal(results)
if err != nil {
return err
}
_, err = w.Write(jresp)
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/json")
return nil
}
// SearchHTML renders the search results in the HTML template
func (sch *Search) SearchHTML(w http.ResponseWriter, r *http.Request) error {
results := sch.searchResult(r)
jresp, err := json.Marshal(results)
if err != nil {
return err
}
_, err = w.Write(jresp)
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/json")
return nil
//indexResult := s.Indexer.Search(q)
//
//results := make([]Result, len(indexResult))
//
//for i, result := range indexResult {
// results[i] = Result{
// Path: result.Path(),
// Title: result.Title(),
// Modified: result.Modified(),
// Body: string(result.Body()),
// }
//}
//
//qresults := QueryResults{
// Context: httpserver.Context{
// Root: http.Dir(s.SiteRoot),
// Req: r,
// URL: r.URL,
// },
// Query: q,
// Results: results,
//}
//
//var buf bytes.Buffer
//err := s.Config.Template.Execute(&buf, qresults)
//if err != nil {
// return http.StatusInternalServerError, err
//}
//w.Header().Set("Content-Type", "text/html; charset=utf-8")
//
//buf.WriteTo(w)
//return http.StatusOK, nil
}
// IndexDoc 索引文件
func (sch *Search) IndexDoc(path string, remove bool) {
found, err := regexp.MatchString(sch.Regexp, path)
if err != nil {
sch.logger.Debug("regexp Error",
zap.String("path", path),
zap.Error(err))
return
}
if !found {
return
}
key := path
if remove {
if err := sch.index.Delete(key); err != nil {
sch.logger.Debug("watch Delete Doc Error",
zap.String("key", key),
zap.Error(err))
}
return
}
bytes, err := os.ReadFile(path)
if err != nil {
sch.logger.Debug("watch index doc ReadFile Error",
zap.String("key", key),
zap.String("path", path),
zap.Error(err))
return
}
message := struct {
Id string
Path string
Body string
}{
Id: key,
Path: path,
Body: string(bytes),
}
err = sch.index.Index(message.Id, message)
if err != nil {
sch.logger.Debug("watch Index Doc Error",
zap.String("key", key),
zap.Error(err))
return
}
}
// Search 搜索文件
func (sch *Search) Search(queryStr string) map[string][]string {
var q query.Query
q = bleve.NewQueryStringQuery(queryStr)
phrases := strings.Split(queryStr, " ")
if len(phrases) > 1 {
q = bleve.NewPhraseQuery(phrases, "Body")
}
searchRequest := bleve.NewSearchRequest(q)
searchRequest.Highlight = bleve.NewHighlight()
searchResult, err := sch.index.Search(searchRequest)
if err != nil {
sch.logger.Debug("Search Error",
zap.String("query", queryStr),
zap.Error(err))
return nil
}
if searchResult.Total == 0 {
return nil
}
result := make(map[string][]string, len(searchResult.Hits))
for _, re := range searchResult.Hits {
result[re.ID] = re.Fragments["Body"]
}
return result
}
// Interface guards
var (
_ caddy.Provisioner = (*Search)(nil)
_ caddy.Validator = (*Search)(nil)
_ caddyhttp.MiddlewareHandler = (*Search)(nil)
)