-
Notifications
You must be signed in to change notification settings - Fork 4
/
init.go
162 lines (142 loc) · 5.79 KB
/
init.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
161
162
package serves
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . HtmlTemplater
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"github.com/gobuffalo/packr/v2"
"github.com/goji/httpauth"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"github.com/orange-cloudfoundry/statusetat/config"
"github.com/orange-cloudfoundry/statusetat/extemplate"
"github.com/orange-cloudfoundry/statusetat/storages"
)
type HtmlTemplater interface {
ExecuteTemplate(wr io.Writer, name string, data interface{}) error
}
type Serve struct {
store storages.Store
xt HtmlTemplater
config config.Config
adminMenuItems []menuItem
}
func Register(
store storages.Store,
router *mux.Router,
userInfo *url.Userinfo,
config config.Config,
) error {
xt := extemplate.New()
box := packr.New("templates", "../website/templates")
err := xt.ParseDir(box, []string{".gohtml"})
if err != nil {
return err
}
return RegisterWithHtmlTemplater(store, router, userInfo, xt, config)
}
func RegisterWithHtmlTemplater(
store storages.Store,
router *mux.Router,
userInfo *url.Userinfo,
htmlTemplater HtmlTemplater,
config config.Config,
) error {
api := &Serve{
store: store,
config: config,
adminMenuItems: []menuItem{
{
ID: "incident",
DisplayName: "incident",
},
{
ID: "maintenance",
DisplayName: "maintenance",
},
{
ID: "persistent_incident",
DisplayName: config.Theme.PersistentDisplayName,
},
{
ID: "info",
DisplayName: "info",
},
},
}
api.xt = htmlTemplater
router.HandleFunc("/", api.Index)
router.HandleFunc("/index", api.Index)
router.HandleFunc("/history", api.History)
router.HandleFunc("/incidents/{guid}", api.ShowIncident)
router.HandleFunc("/rss.xml", api.Rss)
router.HandleFunc("/atom.xml", api.Atom)
router.HandleFunc("/cal.ics", api.Ical)
router.HandleFunc("/healthy", api.HealthCheck)
subRouter := router.PathPrefix("/v1").Subrouter()
subRouter.HandleFunc("/subscribe", api.SubscribeEmail).Methods(http.MethodGet, http.MethodPatch, http.MethodPost, http.MethodPut)
subRouter.HandleFunc("/unsubscribe", api.UnsubscribeEmail).Methods(http.MethodGet, http.MethodPatch, http.MethodPost, http.MethodPut)
subRouter.HandleFunc("/components", api.ShowComponents).Methods(http.MethodGet)
subRouter.HandleFunc("/flags/incident_states", api.ShowFlagIncidentStates).Methods(http.MethodGet)
subRouter.HandleFunc("/flags/component_states", api.ShowFlagComponentStates).Methods(http.MethodGet)
subRouter.HandleFunc("/markdown/preview", api.preview).Methods(http.MethodPost)
subRouter.HandleFunc("/incidents/{guid}", api.Incident).Methods(http.MethodGet)
subRouter.HandleFunc("/incidents", api.ByDate).Methods(http.MethodGet)
subRouter.HandleFunc("/persistent_incidents", api.Persistents).Methods(http.MethodGet)
subRouter.HandleFunc("/incidents/{incident_guid}/messages", api.ReadMessages).Methods(http.MethodGet)
subRouter.HandleFunc("/incidents/{incident_guid}/messages/{message_guid}", api.ReadMessage).Methods(http.MethodGet)
pass, _ := userInfo.Password()
bauthHandler := httpauth.SimpleBasicAuth(userInfo.Username(), pass)
subRouter.Handle("/subscribers", bauthHandler(http.HandlerFunc(api.ListSubscribers))).Methods(http.MethodGet)
subRouter.Handle("/incidents", bauthHandler(http.HandlerFunc(api.CreateIncident))).Methods(http.MethodPost)
subRouter.Handle("/incidents/{guid}", bauthHandler(http.HandlerFunc(api.Update))).Methods(http.MethodPut)
subRouter.Handle("/incidents/{guid}", bauthHandler(http.HandlerFunc(api.Delete))).Methods(http.MethodDelete)
subRouter.Handle("/incidents/{guid}/notify", bauthHandler(http.HandlerFunc(api.Notify))).Methods(http.MethodPut)
subRouter.Handle("/incidents/{incident_guid}/messages", bauthHandler(http.HandlerFunc(api.AddMessage))).Methods(http.MethodPost)
subRouter.Handle("/incidents/{incident_guid}/messages/{message_guid}", bauthHandler(http.HandlerFunc(api.UpdateMessage))).Methods(http.MethodPut)
subRouter.Handle("/incidents/{incident_guid}/messages/{message_guid}", bauthHandler(http.HandlerFunc(api.DeleteMessage))).Methods(http.MethodDelete)
subrouterAdmin := router.PathPrefix("/admin").Subrouter()
subrouterAdmin.Use(bauthHandler)
subrouterAdmin.HandleFunc("/dashboard", api.AdminIncidents)
subrouterAdmin.HandleFunc("/incident", api.AdminIncidents)
subrouterAdmin.HandleFunc("/persistent_incident", api.AdminPersistentIncidents)
subrouterAdmin.HandleFunc("/maintenance", api.AdminMaintenance)
subrouterAdmin.HandleFunc("/info", api.AdminInfo)
subrouterAdmin.HandleFunc("/incident/add", api.AdminAddEditIncident)
subrouterAdmin.HandleFunc("/incident/edit/{guid}", api.AdminAddEditIncident)
subrouterAdmin.HandleFunc("/maintenance/add", api.AdminAddEditMaintenance)
subrouterAdmin.HandleFunc("/maintenance/edit/{guid}", api.AdminAddEditMaintenance)
return nil
}
type HttpError struct {
Description string `json:"description"`
Detail string `json:"detail"`
Status int `json:"status"`
}
func (he HttpError) Error() string {
return fmt.Sprintf("Http error (code: %d), %s: %s", he.Status, he.Detail, he.Description)
}
func JSONError(w http.ResponseWriter, err error, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(code)
LogError(err, code)
err = json.NewEncoder(w).Encode(HttpError{
Status: code,
Description: http.StatusText(code),
Detail: err.Error(),
})
if err != nil {
panic(err)
}
}
func LogError(err error, code int) {
log.WithField("code", code).WithField("status_text", http.StatusText(code)).Debug(err.Error())
}
func HTMLError(w http.ResponseWriter, err error, code int) {
w.WriteHeader(code)
LogError(err, code)
w.Write([]byte(fmt.Sprintf("%d %s: %s", code, http.StatusText(code), err.Error())))
}