feat(AntiGateV2): more verbose debug logging

master
sati.ac 2023-07-13 23:19:43 +03:00
parent 00b07f7e2b
commit 4d861f037e
1 changed files with 6 additions and 4 deletions

View File

@ -209,7 +209,7 @@ func (a *antigateV2Api) getBalance(struct{}) any {
}{0, 0, balance.InexactFloat64()} }{0, 0, balance.InexactFloat64()}
} }
func jsonHandler[T any](handler func(data T) any) func(http.ResponseWriter, *http.Request) { func jsonHandler[T any](api *antigateV2Api, handler func(data T) any) func(http.ResponseWriter, *http.Request) {
emptyRequest := reflect.Zero(reflect.TypeOf(handler).In(0)).Interface().(T) emptyRequest := reflect.Zero(reflect.TypeOf(handler).In(0)).Interface().(T)
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
@ -223,7 +223,9 @@ func jsonHandler[T any](handler func(data T) any) func(http.ResponseWriter, *htt
return return
} }
api.ctx.Logger.WithFields(logrus.Fields{"handler": api.Name(), "request": request}).Debug("request")
response := handler(request) response := handler(request)
api.ctx.Logger.WithFields(logrus.Fields{"handler": api.Name(), "response": response}).Debug("response")
marshaled, _ := json.Marshal(response) marshaled, _ := json.Marshal(response)
w.Write(marshaled) w.Write(marshaled)
@ -233,9 +235,9 @@ func jsonHandler[T any](handler func(data T) any) func(http.ResponseWriter, *htt
func newAntigateV2Api(ctx *ApiContext) ApiHandler { func newAntigateV2Api(ctx *ApiContext) ApiHandler {
api := &antigateV2Api{ctx, http.NewServeMux()} api := &antigateV2Api{ctx, http.NewServeMux()}
api.mux.HandleFunc("/createTask", jsonHandler(api.createTask)) api.mux.HandleFunc("/createTask", jsonHandler(api, api.createTask))
api.mux.HandleFunc("/getTaskResult", jsonHandler(api.getTaskResult)) api.mux.HandleFunc("/getTaskResult", jsonHandler(api, api.getTaskResult))
api.mux.HandleFunc("/getBalance", jsonHandler(api.getBalance)) api.mux.HandleFunc("/getBalance", jsonHandler(api, api.getBalance))
return api return api
} }