-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathweb.go
127 lines (108 loc) · 2.67 KB
/
web.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
package main
import (
"embed"
"encoding/json"
"log"
"net/http"
"text/template"
"github.com/gorilla/mux"
)
//go:embed template
var content embed.FS
type pdnsHandler struct {
s Store
}
func (h *pdnsHandler) handleSearchTuples(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
searchType := vars["searchType"]
query := vars["query"]
if query == "" {
http.Error(w, "Missing parameter: q", http.StatusBadRequest)
return
}
var err error
var recs tupleResults
if searchType == "like" {
recs, err = h.s.LikeTuples(query)
} else {
recs, err = h.s.FindTuples(query)
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(recs)
}
func (h *pdnsHandler) handleSearchIndividual(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
searchType := vars["searchType"]
query := vars["query"]
if query == "" {
http.Error(w, "Missing parameter: q", http.StatusBadRequest)
return
}
var err error
var recs individualResults
if searchType == "like" {
recs, err = h.s.LikeIndividual(query)
} else {
recs, err = h.s.FindIndividual(query)
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(recs)
}
type Results struct {
Query string
Exact bool
Individual individualResults
Tuples tupleResults
Error error
}
func (h *pdnsHandler) handleUI(w http.ResponseWriter, req *http.Request) {
var err error
var res Results
res.Query = req.FormValue("query")
res.Exact = req.FormValue("exact") == "on"
if res.Query != "" {
if res.Exact {
res.Individual, err = h.s.FindIndividual(res.Query)
if err != nil {
res.Error = err
}
res.Tuples, err = h.s.FindTuples(res.Query)
if err != nil {
res.Error = err
}
} else {
res.Individual, err = h.s.LikeIndividual(res.Query)
if err != nil {
res.Error = err
}
res.Tuples, err = h.s.LikeTuples(res.Query)
if err != nil {
res.Error = err
}
}
}
var t = template.Must(template.New("index.html").ParseFS(content, "template/index.html"))
err = t.Execute(w, res)
if err != nil {
log.Printf("Error rendering template: %v", err)
}
}
func startWeb(s Store, bind string) {
h := &pdnsHandler{s: s}
r := mux.NewRouter()
r.HandleFunc("/dns/{searchType}/tuples/{query}", h.handleSearchTuples)
r.HandleFunc("/dns/{searchType}/individual/{query}", h.handleSearchIndividual)
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/ui/", http.StatusSeeOther)
})
r.HandleFunc("/ui/", h.handleUI)
http.Handle("/", r)
log.Printf("Listening on %q\n", bind)
log.Fatal(http.ListenAndServe(bind, nil))
}