-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
160 lines (135 loc) · 3.53 KB
/
handlers.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"strings"
)
// DefaultServerPort is the port the http server listens on
// XXX: allow this to be overridden
const DefaultServerPort = "8080"
// Server is the http server for handling queries and serving the static
// files for the website.
type Server struct {
mux *http.ServeMux
port string
querier *Querier
fileMgr *FileManager
}
// NewServer inits an http server and attaches the handlers.
func NewServer(q *Querier, m *FileManager) *Server {
port := DefaultServerPort
if envPort, ok := os.LookupEnv("PORT"); ok {
port = envPort
}
s := &Server{
mux: http.NewServeMux(),
port: port,
querier: q,
fileMgr: m,
}
s.initHandlers()
return s
}
// Listen starts the server listening for requests.
func (s *Server) Listen() error {
fmt.Printf("Listening on port %s...\n", s.port)
return http.ListenAndServe(fmt.Sprintf(":%s", s.port), s.mux)
}
func (s *Server) initHandlers() {
// search static assets
fs := http.FileServer(http.Dir("static"))
s.mux.Handle("/", fs)
// endpoints for dynamically requesting data
s.mux.HandleFunc("/summary.json", s.summaryHandler)
s.mux.HandleFunc("/preview", s.previewHandler)
s.mux.HandleFunc("/search", s.searchHandler)
}
/* Request Handler Functions */
func (s *Server) summaryHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.String())
summary := s.querier.idx.Summary()
data, err := json.Marshal(summary)
if err != nil {
fmt.Printf("Error creating summary: %s\n", err)
fmt.Fprint(w, "{\"error\": \"error generating summary\"}")
return
}
fmt.Fprint(w, string(data))
}
func (s *Server) previewHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.String())
params := r.URL.Query()
if len(params) == 0 {
fmt.Fprint(w, "")
return
}
file, ok := params["file"]
if !ok {
fmt.Fprint(w, "{\"error\": \"must specify file\"")
return
}
line, ok := params["line"]
if !ok {
fmt.Fprint(w, "{\"error\": \"must specify line number\"}")
return
}
if len(line) == 0 || len(file) == 0 {
fmt.Fprint(w, "{\"error\": \"not enough arguments\"}")
return
}
lineNum, err := strconv.Atoi(line[0])
if err != nil {
fmt.Fprint(w, "{\"error\": \"line must be an integer\"}")
return
}
preview, err := s.fileMgr.GetFilePreview(file[0], lineNum)
if err != nil {
fmt.Printf("Error generating preview for file %s line %d: %s\n", file, lineNum, err)
fmt.Fprint(w, "{\"error\": \"error generating preview\"}")
return
}
data, err := json.Marshal(preview)
if err != nil {
fmt.Printf("Error marshalling preview response: %s\n", err)
fmt.Fprint(w, "{\"error\": \"error generating preview\"}")
return
}
fmt.Fprint(w, string(data))
}
func (s *Server) searchHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.String())
params := r.URL.Query()
if len(params) == 0 {
fmt.Fprint(w, "")
return
}
query, ok := params["query"]
if !ok {
fmt.Fprint(w, "")
return
}
// parse possible query filters
opts := DefaultQueryOptions()
if file, ok := params["file"]; ok {
opts.file = strings.ToLower(file[0])
}
if wtype, ok := params["type"]; ok {
opts.wtype = strings.ToLower(wtype[0])
}
if limit, ok := params["limit"]; ok {
if l, err := strconv.Atoi(limit[0]); err == nil {
opts.limit = l
}
}
refs := s.querier.Query(strings.Join(query, ""), opts)
data, err := json.Marshal(refs.Format())
if err != nil {
fmt.Printf("Error running search: %s\n", err)
fmt.Fprint(w, fmt.Sprintf("{\"error\":\"%s\"}", err))
return
}
fmt.Fprint(w, string(data))
}