del handlewrapper

This commit is contained in:
Pavel 2024-10-01 17:08:39 +03:00
parent a4782cb630
commit 9942b81b71

@ -3,11 +3,9 @@ package tools
import (
"bufio"
"context"
"encoding/json"
"fmt"
"github.com/gofiber/fiber/v2"
"net/http"
"reflect"
"time"
)
@ -90,40 +88,3 @@ func SSEPing(w http.ResponseWriter) {
panic("streaming unsupported")
}
}
func HandlerWrapper(f interface{}) http.HandlerFunc {
fRV := reflect.ValueOf(f)
fRT := fRV.Type()
var argRT reflect.Type
var argTypeRV reflect.Value
var argExist bool
if fRT.NumIn() > 1 {
argExist = true
argRT = fRT.In(1)
argTypeRV = reflect.New(argRT)
}
return func(w http.ResponseWriter, q *http.Request) {
q.Header.Set("Content-Type", "application/json")
var fResultRV []reflect.Value
if argExist {
arg := argTypeRV.Interface()
if err := json.NewDecoder(q.Body).Decode(&arg); err != nil {
http.Error(w, "invalid request data", http.StatusBadRequest)
} else {
fResultRV = fRV.Call([]reflect.Value{reflect.ValueOf(q.Context()), reflect.ValueOf(arg).Elem()})
}
fmt.Println("GET STRANGE", arg)
} else {
fResultRV = fRV.Call([]reflect.Value{reflect.ValueOf(q.Context())})
}
w.WriteHeader(fResultRV[1].Interface().(int))
if err := json.NewEncoder(w).Encode(fResultRV[0].Interface()); err != nil {
panic(err)
}
}
}