light-core/lightCore/Responder.go

163 lines
3.6 KiB
Go
Raw Normal View History

2024-12-17 22:28:48 +08:00
package lightCore
import (
"code.zhecent.com/gopkg/light-core/pkg/myAes"
"github.com/gin-gonic/gin"
"google.golang.org/protobuf/proto"
"net/http"
"reflect"
)
var RespondList []Responder
func init() {
RespondList = []Responder{
new(EmptyResponder),
new(StringResponder),
new(ModelResponder),
new(SuccessResponder),
new(JsonErrorResponder),
new(ViewResponder),
new(ProtobufResponder),
new(ProtobufAesResponder),
new(RedirectResponder),
}
}
type Responder interface {
RespondTo() gin.HandlerFunc
}
func Convert(handler interface{}) gin.HandlerFunc {
hRef := reflect.ValueOf(handler)
for _, r := range RespondList {
rRef := reflect.ValueOf(r).Elem()
if hRef.Type().ConvertibleTo(rRef.Type()) {
rRef.Set(hRef)
return rRef.Interface().(Responder).RespondTo()
}
}
return nil
}
type StringResponder func(*gin.Context) string
func (t StringResponder) RespondTo() gin.HandlerFunc {
return func(context *gin.Context) {
context.String(http.StatusOK, t(context))
}
}
type ModelResponder func(*gin.Context) Model
func (t ModelResponder) RespondTo() gin.HandlerFunc {
return func(context *gin.Context) {
context.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": "",
"data": t(context),
})
}
}
type JsonSuccess string
type SuccessResponder func(*gin.Context) JsonSuccess
func (t SuccessResponder) RespondTo() gin.HandlerFunc {
return func(context *gin.Context) {
context.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": string(t(context)),
"data": []int{},
})
}
}
type JsonError error
type JsonErrorResponder func(*gin.Context) JsonError
func (t JsonErrorResponder) RespondTo() gin.HandlerFunc {
return func(context *gin.Context) {
err := t(context)
if err != nil {
context.JSON(http.StatusOK, gin.H{
"code": 400,
"msg": err.Error(),
"data": []int{},
})
} else {
context.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": "success",
"data": []int{},
})
}
}
}
type EmptyResponder func(*gin.Context)
func (t EmptyResponder) RespondTo() gin.HandlerFunc {
//func(c *gin.Context)其实就是reply
//t,提示就是方法本身其实就是reply
return func(context *gin.Context) {
//context是gin给的全新的没有任何资料的
//t(context)其实就是运行reply
t(context)
//fmt.Println("========================")
//fmt.Println(context.Writer)
//fmt.Println("========================")
}
}
type View string
type ViewResponder func(*gin.Context) View
func (t ViewResponder) RespondTo() gin.HandlerFunc {
return func(context *gin.Context) {
context.HTML(http.StatusOK, string(t(context))+".html", context.Keys)
}
}
type Protobuf proto.Message
type ProtobufResponder func(*gin.Context) Protobuf
func (t ProtobufResponder) RespondTo() gin.HandlerFunc {
return func(context *gin.Context) {
context.ProtoBuf(http.StatusOK, t(context))
}
}
type ProtobufAes proto.Message
type ProtobufAesResponder func(*gin.Context) ProtobufAes
func (t ProtobufAesResponder) RespondTo() gin.HandlerFunc {
return func(context *gin.Context) {
str, err := proto.Marshal(t(context))
if err != nil {
context.String(http.StatusBadRequest, err.Error())
} else {
decrypt, err := myAes.AesTool(context.GetString("LightAppKey"), context.GetString("LightAppIv")).Encrypt(string(str))
if err != nil {
context.String(http.StatusBadRequest, err.Error())
} else {
context.String(http.StatusOK, string(decrypt))
}
}
}
}
type Redirect string
type RedirectResponder func(*gin.Context) Redirect
func (t RedirectResponder) RespondTo() gin.HandlerFunc {
return func(context *gin.Context) {
context.Redirect(http.StatusFound, string(t(context)))
}
}