-
Notifications
You must be signed in to change notification settings - Fork 8
/
ui_handlers.go
110 lines (98 loc) · 3.05 KB
/
ui_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
package eventmaster
import (
"fmt"
"html/template"
"net/http"
"strings"
"github.com/julienschmidt/httprouter"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
eventmaster "github.com/wish/eventmaster/proto"
)
var funcMap = template.FuncMap{
"getCommaSeparated": func(strs []string) template.HTML {
return template.HTML(strings.Join(strs, ","))
},
"getChecked": func(val bool) template.HTMLAttr {
if val == true {
return template.HTMLAttr("checked")
}
return template.HTMLAttr("")
},
"getSelectedTopic": func(topics []string, name string) template.HTMLAttr {
for _, topic := range topics {
if topic == name {
return template.HTMLAttr(`selected="selected"`)
}
}
return template.HTMLAttr("")
},
}
// GetEventPageData stores information renderd in the query form template.
type GetEventPageData struct {
Topics []Topic
Query *eventmaster.Query
}
func executeTemplate(w http.ResponseWriter, t *template.Template, data interface{}) {
if err := t.Execute(w, data); err != nil {
log.Errorf("Error executing template: %v", err)
}
}
// HandleMainPage redirects to /event.
func (s *Server) HandleMainPage(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if r.URL.RawQuery == "" {
http.Redirect(w, r, "/event", 301)
} else {
http.Redirect(w, r, "/event?"+r.URL.RawQuery, 301)
}
}
// HandleGetEventPage renders all recent events to html.
func (s *Server) HandleGetEventPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
q, err := getQueryFromRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
topics, err := s.store.GetTopics()
if err != nil {
http.Error(w, errors.Wrap(err, "get topics").Error(), http.StatusInternalServerError)
return
}
getEventQuery := GetEventPageData{
Topics: topics,
Query: q,
}
t, err := s.templates.Get("query_form.html")
if err != nil {
http.Error(w, fmt.Sprintf("error parsing template main.html: %v", err), http.StatusInternalServerError)
return
}
executeTemplate(w, t, getEventQuery)
}
// HandleCreatePage deals with creating an event.
func (s *Server) HandleCreatePage(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
t, err := s.templates.Get("create_form.html")
if err != nil {
http.Error(w, fmt.Sprintf("error parsing template main.html: %v", err), http.StatusInternalServerError)
return
}
executeTemplate(w, t, nil)
}
// HandleTopicPage renders the topic page.
func (s *Server) HandleTopicPage(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
t, err := s.templates.Get("topic_form.html")
if err != nil {
http.Error(w, fmt.Sprintf("error parsing template main.html: %v", err), http.StatusInternalServerError)
return
}
executeTemplate(w, t, nil)
}
// HandleDCPage renders the datacenter page.
func (s *Server) HandleDCPage(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
t, err := s.templates.Get("dc_form.html")
if err != nil {
http.Error(w, fmt.Sprintf("error parsing template main.html: %v", err), http.StatusInternalServerError)
return
}
executeTemplate(w, t, nil)
}