1
1
package main
2
2
3
3
import (
4
+ "context"
4
5
"database/sql"
5
6
"encoding/binary"
6
7
"encoding/json"
@@ -22,7 +23,8 @@ import (
22
23
)
23
24
24
25
type API struct {
25
- DB * sql.DB
26
+ DB * sql.DB
27
+ Ctx context.Context
26
28
}
27
29
28
30
// Node struct
@@ -136,7 +138,7 @@ func (a *API) handleAllStats(res http.ResponseWriter, req *http.Request) {
136
138
}
137
139
for _ , i := range interfaces .Element {
138
140
if h , ok := intNametoInterface [i ]; ok {
139
- stat := h .handleAPIReq (APIReq {Req : "stats" , NetInterface : i , NetWork : "" }, a .DB )
141
+ stat := h .handleAPIReq (a . Ctx , APIReq {Req : "stats" , NetInterface : i , NetWork : "" }, a .DB )
140
142
for _ , s := range stat .([]Stats ) {
141
143
result .Items = append (result .Items , s )
142
144
}
@@ -159,7 +161,7 @@ func (a *API) handleStats(res http.ResponseWriter, req *http.Request) {
159
161
vars := mux .Vars (req )
160
162
161
163
if h , ok := intNametoInterface [vars ["int" ]]; ok {
162
- stat := h .handleAPIReq (APIReq {Req : "stats" , NetInterface : vars ["int" ], NetWork : vars ["network" ]}, a .DB )
164
+ stat := h .handleAPIReq (a . Ctx , APIReq {Req : "stats" , NetInterface : vars ["int" ], NetWork : vars ["network" ]}, a .DB )
163
165
164
166
outgoingJSON , err := json .Marshal (stat )
165
167
@@ -180,7 +182,7 @@ func (a *API) handleDuplicates(res http.ResponseWriter, req *http.Request) {
180
182
vars := mux .Vars (req )
181
183
182
184
if h , ok := intNametoInterface [vars ["int" ]]; ok {
183
- stat := h .handleAPIReq (APIReq {Req : "duplicates" , NetInterface : vars ["int" ], NetWork : vars ["network" ]}, a .DB )
185
+ stat := h .handleAPIReq (a . Ctx , APIReq {Req : "duplicates" , NetInterface : vars ["int" ], NetWork : vars ["network" ]}, a .DB )
184
186
185
187
outgoingJSON , err := json .Marshal (stat )
186
188
@@ -201,7 +203,7 @@ func (a *API) handleDebug(res http.ResponseWriter, req *http.Request) {
201
203
vars := mux .Vars (req )
202
204
203
205
if h , ok := intNametoInterface [vars ["int" ]]; ok {
204
- stat := h .handleAPIReq (APIReq {Req : "debug" , NetInterface : vars ["int" ], Role : vars ["role" ]}, a .DB )
206
+ stat := h .handleAPIReq (a . Ctx , APIReq {Req : "debug" , NetInterface : vars ["int" ], Role : vars ["role" ]}, a .DB )
205
207
206
208
outgoingJSON , err := json .Marshal (stat )
207
209
@@ -219,14 +221,14 @@ func (a *API) handleDebug(res http.ResponseWriter, req *http.Request) {
219
221
220
222
func (a * API ) handleReleaseIP (res http.ResponseWriter , req * http.Request ) {
221
223
vars := mux .Vars (req )
222
- _ = InterfaceScopeFromMac (vars ["mac" ])
224
+ _ = InterfaceScopeFromMac (a . Ctx , vars ["mac" ])
223
225
224
226
var result = & Info {Mac : vars ["mac" ], Status : "ACK" }
225
227
226
228
res .Header ().Set ("Content-Type" , "application/json; charset=UTF-8" )
227
229
res .WriteHeader (http .StatusOK )
228
230
if err := json .NewEncoder (res ).Encode (result ); err != nil {
229
- log .LoggerWContext (ctx ).Error ("Error releasing IP: " + err .Error ())
231
+ log .LoggerWContext (a . Ctx ).Error ("Error releasing IP: " + err .Error () + " mac=" + vars [ "mac" ] )
230
232
}
231
233
}
232
234
@@ -243,14 +245,14 @@ func (a *API) handleOverrideOptions(res http.ResponseWriter, req *http.Request)
243
245
}
244
246
245
247
// Insert information in MySQL
246
- _ = MysqlInsert (vars ["mac" ], sharedutils .ConvertToString (body ), a .DB )
248
+ _ = MysqlInsert (a . Ctx , vars ["mac" ], sharedutils .ConvertToString (body ), a .DB )
247
249
248
250
var result = & Info {Mac : vars ["mac" ], Status : "ACK" }
249
251
250
252
res .Header ().Set ("Content-Type" , "application/json; charset=UTF-8" )
251
253
res .WriteHeader (http .StatusOK )
252
254
if err := json .NewEncoder (res ).Encode (result ); err != nil {
253
- log .LoggerWContext (ctx ).Error ("Error adding MAC options: " + err .Error ())
255
+ log .LoggerWContext (a . Ctx ).Error ("Error adding MAC options: " + err .Error () + " mac=" + vars [ "mac" ] )
254
256
}
255
257
}
256
258
@@ -267,7 +269,7 @@ func (a *API) handleOverrideNetworkOptions(res http.ResponseWriter, req *http.Re
267
269
}
268
270
269
271
// Insert information in MySQL
270
- _ = MysqlInsert (vars ["network" ], sharedutils .ConvertToString (body ), a .DB )
272
+ _ = MysqlInsert (a . Ctx , vars ["network" ], sharedutils .ConvertToString (body ), a .DB )
271
273
272
274
var result = & Info {Network : vars ["network" ], Status : "ACK" }
273
275
@@ -291,7 +293,7 @@ func (a *API) handleRemoveOptions(res http.ResponseWriter, req *http.Request) {
291
293
res .Header ().Set ("Content-Type" , "application/json; charset=UTF-8" )
292
294
res .WriteHeader (http .StatusOK )
293
295
if err := json .NewEncoder (res ).Encode (result ); err != nil {
294
- log .LoggerWContext (ctx ).Error ("Error removing MAC options: " + err .Error ())
296
+ log .LoggerWContext (ctx ).Error ("Error removing MAC options: " + err .Error () + " mac=" + vars [ "mac" ] )
295
297
}
296
298
}
297
299
@@ -312,9 +314,9 @@ func (a *API) handleRemoveNetworkOptions(res http.ResponseWriter, req *http.Requ
312
314
}
313
315
}
314
316
315
- func decodeOptions (b string , db * sql.DB ) (map [dhcp.OptionCode ][]byte , error ) {
317
+ func decodeOptions (ctx context. Context , b string , db * sql.DB ) (map [dhcp.OptionCode ][]byte , error ) {
316
318
var options []Options
317
- _ , value := MysqlGet (b , db )
319
+ _ , value := MysqlGet (ctx , b , db )
318
320
decodedValue := sharedutils .ConvertToByte (value )
319
321
var dhcpOptions = make (map [dhcp.OptionCode ][]byte )
320
322
if err := json .Unmarshal (decodedValue , & options ); err != nil {
@@ -364,7 +366,7 @@ func extractMembers(v Network) ([]Node, []string, int) {
364
366
return Members , Macs , Count
365
367
}
366
368
367
- func (h * Interface ) handleAPIReq (Request APIReq , db * sql.DB ) interface {} {
369
+ func (h * Interface ) handleAPIReq (ctx context. Context , Request APIReq , db * sql.DB ) interface {} {
368
370
var stats []Stats
369
371
370
372
if Request .Req == "duplicates" {
@@ -403,7 +405,7 @@ func (h *Interface) handleAPIReq(Request APIReq, db *sql.DB) interface{} {
403
405
}
404
406
405
407
// Add network options on the fly
406
- x , err := decodeOptions (v .network .IP .String (), db )
408
+ x , err := decodeOptions (ctx , v .network .IP .String (), db )
407
409
if err == nil {
408
410
for key , value := range x {
409
411
Options [key .String ()] = Tlv .Tlvlist [int (key )].Transform .String (value )
0 commit comments