forked from pedronasser/caddy-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
175 lines (148 loc) · 3.61 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
package search
import (
"bytes"
"encoding/json"
"html/template"
"net/http"
"strconv"
"time"
//TODO remove this
"github.com/caddyserver/caddy/caddyhttp/httpserver"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/caddyserver/caddy/v2/modules/caddy-search/indexer"
)
// ServerHTTP is the HTTP handler for this middleware
func (s *Search) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
if r.URL.Path == s.Endpoint {
if r.Header.Get("Accept") == "application/json" || s.Template == nil {
return s.SearchJSON(w, r)
}
return s.SearchHTML(w, r)
}
record := s.Indexer.Record(GetUrlPath(r.URL))
err := next.ServeHTTP(&searchResponseWriter{w, record}, r)
modif := w.Header().Get("Last-Modified")
if len(modif) > 0 {
modTime, err := time.Parse(`Mon, 2 Jan 2006 15:04:05 MST`, modif)
if err == nil {
record.SetModified(modTime)
} else {
record.SetModified(time.Now())
}
} else {
record.SetModified(time.Now())
}
s.IndexManager.Feed(record)
return err
}
// Result is the structure for the search result
type Result struct {
Path string
Title string
Body template.HTML
Json string
Modified time.Time
Indexed time.Time
From int
Size int
}
// SearchJSON renders the search results in JSON format
func (s *Search) SearchJSON(w http.ResponseWriter, r *http.Request) error {
qry := r.URL.Query()
q := qry.Get("q")
from := 0
size := 100
if f, err := strconv.Atoi(qry.Get("f")); err == nil {
from = f
}
if s, err := strconv.Atoi(qry.Get("s")); err == nil {
size = s
}
indexResult := s.Indexer.Search(q, from, size)
results := make([]Result, len(indexResult))
for i, result := range indexResult {
body := result.Body()
results[i] = Result{
Path: result.Path(),
Title: result.Title(),
Modified: result.Modified(),
Indexed: result.Indexed(),
Json: string(body),
From: from,
Size: size,
}
}
jresp, err := json.Marshal(results)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return err
}
w.Write(jresp)
return err
}
// SearchHTML renders the search results in the HTML template
func (s *Search) SearchHTML(w http.ResponseWriter, r *http.Request) error {
qry := r.URL.Query()
q := qry.Get("q")
from := 0
size := 100
if f, err := strconv.Atoi(qry.Get("f")); err == nil {
from = f
}
if s, err := strconv.Atoi(qry.Get("s")); err == nil {
size = s
}
indexResult := s.Indexer.Search(q, from, size)
results := make([]Result, len(indexResult))
for i, result := range indexResult {
results[i] = Result{
Path: result.Path(),
Title: result.Title(),
Modified: result.Modified(),
Body: template.HTML(result.Body()),
From: from,
Size: size,
}
}
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.Template.Execute(&buf, qresults)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return err
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
buf.WriteTo(w)
return nil
}
type QueryResults struct {
httpserver.Context
Query string
Results []Result
}
type searchResponseWriter struct {
w http.ResponseWriter
record indexer.Record
}
func (r *searchResponseWriter) Header() http.Header {
return r.w.Header()
}
func (r *searchResponseWriter) WriteHeader(code int) {
if code != http.StatusOK {
r.record.Ignore()
}
r.w.WriteHeader(code)
}
func (r *searchResponseWriter) Write(p []byte) (int, error) {
r.record.Write(p)
n, err := r.w.Write(p)
return n, err
}