-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
49 lines (33 loc) · 780 Bytes
/
server.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
package main
import (
"encoding/json"
"github.com/bwells/trie"
"net/http"
"text/template"
)
func queryHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
query := r.FormValue("term")
results := multiMatch(search_trie, query)
if len(results) == 0 {
w.Write([]byte("[]"))
return
}
output, err := json.Marshal(results)
if err != nil {
w.Write([]byte("[]"))
return
}
w.Write(output)
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
tpl.Execute(w, nil)
}
var search_trie *trie.Trie
var tpl *template.Template
func serve() {
tpl, _ = template.ParseFiles("form.html")
http.HandleFunc("/", indexHandler)
http.HandleFunc("/search", queryHandler)
go http.ListenAndServe(":9000", nil)
}