|
@@ -21,6 +21,10 @@ const restErr = "{\"error\":\"An unknown error occurred\"}"
|
21
|
21
|
// way to do it, given how HTTP handlers dispatch and work.
|
22
|
22
|
var restAPIServer *Server
|
23
|
23
|
|
|
24
|
+type restVersionResp struct {
|
|
25
|
+ Version string `json:"version"`
|
|
26
|
+}
|
|
27
|
+
|
24
|
28
|
type restStatusResp struct {
|
25
|
29
|
Clients int `json:"clients"`
|
26
|
30
|
Opers int `json:"opers"`
|
|
@@ -38,7 +42,25 @@ type restAcct struct {
|
38
|
42
|
}
|
39
|
43
|
|
40
|
44
|
type restAccountsResp struct {
|
41
|
|
- Accounts map[string]restAcct
|
|
45
|
+ Accounts map[string]restAcct `json:"accounts"`
|
|
46
|
+}
|
|
47
|
+
|
|
48
|
+type restRehashResp struct {
|
|
49
|
+ Successful bool `json:"successful"`
|
|
50
|
+ Error string `json:"error"`
|
|
51
|
+ Time time.Time `json:"time"`
|
|
52
|
+}
|
|
53
|
+
|
|
54
|
+func restVersion(w http.ResponseWriter, r *http.Request) {
|
|
55
|
+ rs := restVersionResp{
|
|
56
|
+ Version: SemVer,
|
|
57
|
+ }
|
|
58
|
+ b, err := json.Marshal(rs)
|
|
59
|
+ if err != nil {
|
|
60
|
+ fmt.Fprintln(w, restErr)
|
|
61
|
+ } else {
|
|
62
|
+ fmt.Fprintln(w, string(b))
|
|
63
|
+ }
|
42
|
64
|
}
|
43
|
65
|
|
44
|
66
|
func restStatus(w http.ResponseWriter, r *http.Request) {
|
|
@@ -55,7 +77,7 @@ func restStatus(w http.ResponseWriter, r *http.Request) {
|
55
|
77
|
}
|
56
|
78
|
}
|
57
|
79
|
|
58
|
|
-func restDLines(w http.ResponseWriter, r *http.Request) {
|
|
80
|
+func restGetDLines(w http.ResponseWriter, r *http.Request) {
|
59
|
81
|
rs := restDLinesResp{
|
60
|
82
|
DLines: restAPIServer.dlines.AllBans(),
|
61
|
83
|
}
|
|
@@ -67,7 +89,7 @@ func restDLines(w http.ResponseWriter, r *http.Request) {
|
67
|
89
|
}
|
68
|
90
|
}
|
69
|
91
|
|
70
|
|
-func restAccounts(w http.ResponseWriter, r *http.Request) {
|
|
92
|
+func restGetAccounts(w http.ResponseWriter, r *http.Request) {
|
71
|
93
|
rs := restAccountsResp{
|
72
|
94
|
Accounts: make(map[string]restAcct),
|
73
|
95
|
}
|
|
@@ -89,15 +111,42 @@ func restAccounts(w http.ResponseWriter, r *http.Request) {
|
89
|
111
|
}
|
90
|
112
|
}
|
91
|
113
|
|
|
114
|
+func restRehash(w http.ResponseWriter, r *http.Request) {
|
|
115
|
+ err := restAPIServer.rehash()
|
|
116
|
+
|
|
117
|
+ rs := restRehashResp{
|
|
118
|
+ Successful: err == nil,
|
|
119
|
+ Time: time.Now(),
|
|
120
|
+ }
|
|
121
|
+ if err != nil {
|
|
122
|
+ rs.Error = err.Error()
|
|
123
|
+ }
|
|
124
|
+
|
|
125
|
+ b, err := json.Marshal(rs)
|
|
126
|
+ if err != nil {
|
|
127
|
+ fmt.Fprintln(w, restErr)
|
|
128
|
+ } else {
|
|
129
|
+ fmt.Fprintln(w, string(b))
|
|
130
|
+ }
|
|
131
|
+}
|
|
132
|
+
|
92
|
133
|
func (s *Server) startRestAPI() {
|
93
|
134
|
// so handlers can ref it later
|
94
|
135
|
restAPIServer = s
|
95
|
136
|
|
96
|
137
|
// start router
|
97
|
138
|
r := mux.NewRouter()
|
98
|
|
- r.HandleFunc("/status", restStatus)
|
99
|
|
- r.HandleFunc("/status/dlines", restDLines)
|
100
|
|
- r.HandleFunc("/status/accounts", restAccounts)
|
|
139
|
+
|
|
140
|
+ // GET methods
|
|
141
|
+ rg := r.Methods("GET").Subrouter()
|
|
142
|
+ rg.HandleFunc("/version", restVersion)
|
|
143
|
+ rg.HandleFunc("/status", restStatus)
|
|
144
|
+ rg.HandleFunc("/dlines", restGetDLines)
|
|
145
|
+ rg.HandleFunc("/accounts", restGetAccounts)
|
|
146
|
+
|
|
147
|
+ // PUT methods
|
|
148
|
+ rp := r.Methods("POST").Subrouter()
|
|
149
|
+ rp.HandleFunc("/rehash", restRehash)
|
101
|
150
|
|
102
|
151
|
// start api
|
103
|
152
|
go http.ListenAndServe(s.restAPI.Listen, r)
|