/
churchill.go
84 lines (64 loc) · 1.8 KB
/
churchill.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"nimitz"
"os"
"roosevelt"
"sir"
"winston"
)
type tv struct {
Location string
GramsLen int
}
type stv struct {
Result roosevelt.QueryResult
Score int
}
func AddHandler(w http.ResponseWriter, r *http.Request) {
go roosevelt.Add(r.FormValue("website"))
http.Redirect(w, r, "/", http.StatusFound)
}
func SearchHandler(w http.ResponseWriter, r *http.Request) {
stvs := make([]stv, 0)
for index, location := range roosevelt.Query(r.FormValue("query")) {
stvs = append(stvs, stv{location, index})
}
nimitz.ThePool.Pools["search"].Execute(w, stvs)
}
func IndexHandler(w http.ResponseWriter, r *http.Request) {
tvs := make([]tv, 0)
tvs = append(tvs, tv{"Index", roosevelt.IndexDataLen()})
for i := 0; i < len(winston.TheDocuments); i++ {
tvs = append(tvs, tv{winston.TheDocuments[i].Location, len(winston.TheDocuments[i].Grams)})
}
nimitz.ThePool.Pools["index"].Execute(w, tvs)
}
type Response map[string]interface{}
func (r Response) String() string {
b, err := json.Marshal(r)
sir.CheckError(err)
return string(b)
}
func TestHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, Response{"success": true, "message": "Hello!"})
return
}
func init() {
nimitz.ThePool.Fill("index", "templates/layout.html", "templates/index.html")
nimitz.ThePool.Fill("search", "templates/layout.html", "templates/search.html")
}
func main() {
wd, err := os.Getwd()
sir.CheckError(err)
http.HandleFunc("/", IndexHandler)
http.HandleFunc("/add", AddHandler)
http.HandleFunc("/test", TestHandler)
http.HandleFunc("/search", SearchHandler)
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir(wd+`/public`))))
err = http.ListenAndServe(":8080", nil)
sir.CheckError(err)
}