-
Notifications
You must be signed in to change notification settings - Fork 2
/
admin.go
117 lines (96 loc) · 2.67 KB
/
admin.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
package clover
import (
"encoding/json"
"html/template"
"io/ioutil"
"net/http"
"github.com/go-chi/chi"
"github.com/nyaruka/rp-clover/models"
"github.com/sirupsen/logrus"
)
func newAdminRouter(s *Server) *chi.Mux {
router := chi.NewRouter()
router.Use(s.adminOnly)
router.Method(http.MethodGet, "/", s.newHandlerFunc(viewConfig))
router.Method(http.MethodPost, "/", s.newHandlerFunc(updateConfig))
return router
}
func renderInterchanges(s *Server, w http.ResponseWriter, r *http.Request, config []byte, message string, err error) error {
errMsg := ""
if err != nil {
errMsg = err.Error()
}
tpl, err := loadTemplate(s.fs, "/admin/index.html")
if err != nil {
return err
}
err = tpl.Execute(w, map[string]interface{}{
"config": string(config),
"message": message,
"error": errMsg,
})
if err != nil {
return err
}
return nil
}
func viewConfig(s *Server, w http.ResponseWriter, r *http.Request) error {
interchanges, err := models.GetInterchangeConfig(r.Context(), s.db)
if err != nil {
logrus.WithError(err).Error("error loading interchange config")
return err
}
config, err := json.MarshalIndent(interchanges, "", " ")
if err != nil {
return err
}
return renderInterchanges(s, w, r, config, "", err)
}
func updateConfig(s *Server, w http.ResponseWriter, r *http.Request) error {
interchanges, err := models.GetInterchangeConfig(r.Context(), s.db)
if err != nil {
return err
}
config, err := json.MarshalIndent(interchanges, "", " ")
if err != nil {
return err
}
err = r.ParseForm()
if err != nil {
return renderInterchanges(s, w, r, config, "", err)
}
config = []byte(r.Form.Get("config"))
logrus.WithField("config", string(config)).Info("received new config")
// try to create our config
interchanges = make([]models.Interchange, 0)
err = json.Unmarshal(config, &interchanges)
if err != nil {
return renderInterchanges(s, w, r, config, "", err)
}
err = models.UpdateInterchangeConfig(r.Context(), s.db, interchanges)
if err != nil {
return renderInterchanges(s, w, r, config, "", err)
}
// reselect our current interchanges
interchanges, err = models.GetInterchangeConfig(r.Context(), s.db)
if err != nil {
logrus.WithError(err).Error("error loading interchange config")
return err
}
config, err = json.MarshalIndent(interchanges, "", " ")
if err != nil {
return err
}
return renderInterchanges(s, w, r, config, "configuration saved", err)
}
func loadTemplate(fs http.FileSystem, name string) (*template.Template, error) {
file, err := fs.Open(name)
if err != nil {
return nil, err
}
text, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
return template.New(name).Parse(string(text))
}