del handlewrapper
This commit is contained in:
parent
a4782cb630
commit
9942b81b71
@ -3,11 +3,9 @@ package tools
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -90,40 +88,3 @@ func SSEPing(w http.ResponseWriter) {
|
|||||||
panic("streaming unsupported")
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user