reply.go 605 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package reply
  2. import (
  3. "github.com/go-kratos/kratos/v2/transport/http"
  4. )
  5. type SuccessReply struct {
  6. Code int32 `json:"code"`
  7. Message string `json:"message"`
  8. Data interface{} `json:"data"`
  9. }
  10. type ReplyFunc func(ctx http.Context, req any) (any, error)
  11. var reply = DefaultFunc
  12. func DefaultFunc(ctx http.Context, out any) (any, error) {
  13. success := &SuccessReply{
  14. Code: 0,
  15. }
  16. if out != nil {
  17. success.Data = out
  18. }
  19. return success, nil
  20. }
  21. func SetReplyFunc(f ReplyFunc) {
  22. reply = f
  23. }
  24. func GetReplyFunc() ReplyFunc {
  25. if reply == nil {
  26. reply = DefaultFunc
  27. }
  28. return reply
  29. }