-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
48 lines (42 loc) · 1.09 KB
/
main.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
package main
import (
"encoding/json"
"io/ioutil"
"luw/load"
"luw/trie"
"net/http"
"path/filepath"
)
var tree = trie.NewTrie()
func init() {
items := load.Load()
for _,item := range items {
tree.Insert(item.Word)
}
}
func main() {
http.HandleFunc("/word", func(writer http.ResponseWriter, request *http.Request) {
prefix := request.FormValue("prefix")
result,_ := tree.FindString(prefix)
writer.Header().Set("Content-Type","application/json")
b,err := json.Marshal(result)
if err != nil {
panic(err)
}
writer.Write(b)
})
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
c,err := load.CurrentDir()
if err != nil {
panic(err)
}
index := filepath.Join(c,"html/index.html")
b,err := ioutil.ReadFile(index)
if err != nil {
panic(err)
}
writer.Header().Set("Content-Type","text/html; charset=UTF-8")
writer.Write(b)
})
http.ListenAndServe(":9005",nil)
}