This repository has been archived by the owner on Aug 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsearch.go
151 lines (123 loc) · 3.16 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
package search
import (
"bytes"
"encoding/json"
"net/http"
"time"
"github.com/mholt/caddy/caddyhttp/httpserver"
"github.com/pedronasser/caddy-search/indexer"
)
// Search represents this middleware structure
type Search struct {
Next httpserver.Handler
*Config
Indexer indexer.Handler
*Pipeline
}
// ServerHTTP is the HTTP handler for this middleware
func (s *Search) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
if httpserver.Path(r.URL.Path).Matches(s.Config.Endpoint) {
if r.Header.Get("Accept") == "application/json" || s.Config.Template == nil {
return s.SearchJSON(w, r)
}
return s.SearchHTML(w, r)
}
record := s.Indexer.Record(r.URL.String())
status, err := s.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)
}
}
if status != http.StatusOK {
record.Ignore()
}
go s.Pipeline.Pipe(record)
return status, err
}
// Result is the structure for the search result
type Result struct {
Path string
Title string
Body string
Modified time.Time
Indexed time.Time
}
// SearchJSON renders the search results in JSON format
func (s *Search) SearchJSON(w http.ResponseWriter, r *http.Request) (int, error) {
q := r.URL.Query().Get("q")
indexResult := s.Indexer.Search(q)
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(),
Body: string(body),
}
}
jresp, err := json.Marshal(results)
if err != nil {
return http.StatusInternalServerError, err
}
w.Write(jresp)
return http.StatusOK, err
}
// SearchHTML renders the search results in the HTML template
func (s *Search) SearchHTML(w http.ResponseWriter, r *http.Request) (int, error) {
q := r.URL.Query().Get("q")
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
}
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) {
defer r.record.Write(p)
n, err := r.w.Write(p)
return n, err
}