server.go 733 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package routers
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "kpt.xdmy/apiserver/model"
  5. "kpt.xdmy/apiserver/model/http"
  6. "kpt.xdmy/apiserver/service"
  7. )
  8. var (
  9. svc *service.Service
  10. )
  11. func Init(s *service.Service) {
  12. svc = s
  13. }
  14. func eJSON(c *gin.Context, rp *http.Response, e error) {
  15. if rp == nil {
  16. rp = &http.Response{}
  17. }
  18. if e != nil {
  19. rp.Message = e.Error()
  20. rp.Success = false
  21. if rp.Code == 0 {
  22. rp.Code = 400
  23. }
  24. }
  25. if e == nil {
  26. rp.Success = true
  27. rp.Code = 200
  28. }
  29. c.JSON(rp.Code, &rp)
  30. return
  31. }
  32. func Bind(c *gin.Context, req model.Request) bool {
  33. if err := c.ShouldBind(req); err != nil {
  34. rp := &http.Response{
  35. Code: 500,
  36. Message: err.Error(),
  37. }
  38. eJSON(c, rp, nil)
  39. return false
  40. }
  41. return true
  42. }