-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.go
71 lines (52 loc) · 1.53 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
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/willianpc/go-sample-app/dom"
"golang.org/x/net/html"
"golang.org/x/text/encoding/charmap"
)
func handleSearch(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
q := url.QueryEscape(r.URL.Query().Get("q"))
// Throws HTTP 500 Error
if q == "" {
w.WriteHeader(http.StatusInternalServerError)
_, _ = io.WriteString(w, `{"error": "A value for 'q' must be provided"}`)
return
}
// Read from cache
cacheRes := readCache(r.Context(), q)
var fromCache bool
if len(cacheRes) > 0 {
fromCache = true
} else {
cacheRes = dataFromGoogle(r, q)
}
cacheAsArray := `["` + strings.Join(cacheRes, `", "`) + `"]`
fmt.Fprintf(w, `{"total": %d,"query": "%s","results": %s, "cached": %v}`, len(cacheRes), q, cacheAsArray, fromCache)
}
func dataFromGoogle(incomingRequest *http.Request, q string) []string {
var cacheRes []string
clientReq, _ := http.NewRequest(http.MethodGet, "https://www.google.com/search?q="+q, nil)
clientResp, _ := c.Do(clientReq)
body, _ := io.ReadAll(clientResp.Body)
defer clientResp.Body.Close()
dec := charmap.Windows1250.NewDecoder()
body, _ = dec.Bytes(body)
doc, _ := html.Parse(bytes.NewReader(body))
de := dom.DomElement(*doc)
nodes := de.QuerySelector("h3")
for _, node := range nodes {
n := html.Node(node)
text := dom.InnerText(n)
cacheRes = append(cacheRes, text)
}
// Update cache
_ = writeCache(incomingRequest.Context(), q, cacheRes)
return cacheRes
}