-
Notifications
You must be signed in to change notification settings - Fork 122
/
helper.go
47 lines (40 loc) · 1.14 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package common
import (
"fmt"
"net/http"
"reflect"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
// WrapFunc will wrap func(args ...interface{}) (interface{}, <error>) as a Gin HandlerFunc
func WrapFunc(f interface{}, args ...interface{}) gin.HandlerFunc {
fn := reflect.ValueOf(f)
if fn.Type().NumIn() != len(args) {
panic(fmt.Sprintf("invaild input parameters of function %v", fn.Type()))
}
outNum := fn.Type().NumOut()
if outNum == 0 {
panic(fmt.Sprintf("invaild output parameters of function %v, at least one, but got %d", fn.Type(), outNum))
}
inputs := make([]reflect.Value, len(args))
for k, in := range args {
inputs[k] = reflect.ValueOf(in)
}
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
logrus.Warnf("panic: %v", err)
ResponseFailed(c, http.StatusInternalServerError, fmt.Errorf("%v", err))
}
}()
outputs := fn.Call(inputs)
if len(outputs) > 1 {
err, ok := outputs[len(outputs)-1].Interface().(error)
if ok && err != nil {
ResponseFailed(c, http.StatusInternalServerError, err)
return
}
}
c.JSON(http.StatusOK, outputs[0].Interface())
}
}