26 lines
493 B
Go
26 lines
493 B
Go
package middleware
|
|
|
|
import (
|
|
"encoding/json"
|
|
"game-driver/internal/schema"
|
|
"game-driver/leaf"
|
|
"game-driver/pkg/logger"
|
|
)
|
|
|
|
type JSONKey string
|
|
|
|
const PayloadJSONKey JSONKey = "payload_json"
|
|
|
|
// PayloadJSON 解析报文
|
|
func PayloadJSON[T schema.JsonModel]() leaf.HandlerFunc {
|
|
return func(c *leaf.Context) {
|
|
pm := new(T)
|
|
err := json.Unmarshal(c.Payload, pm)
|
|
if err != nil {
|
|
logger.Panicln("报文解析错误", err)
|
|
}
|
|
leaf.WithValue[*T](c, PayloadJSONKey, pm)
|
|
c.Next()
|
|
}
|
|
}
|