http_base.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package http
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/csv"
  6. "encoding/json"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "reflect"
  11. "go-common/app/admin/main/mcn/model"
  12. "go-common/library/ecode"
  13. "go-common/library/log"
  14. "go-common/library/net/http/blademaster"
  15. "go-common/library/net/http/blademaster/binding"
  16. "go-common/library/net/http/blademaster/render"
  17. "github.com/pkg/errors"
  18. )
  19. func bmHTTPErrorWithMsg(c *blademaster.Context, err error) {
  20. if c.IsAborted() {
  21. return
  22. }
  23. c.Error = err
  24. bcode, ok := errors.Cause(err).(ecode.Codes)
  25. var msg string
  26. if !ok {
  27. msg = err.Error()
  28. bcode = ecode.String(err.Error())
  29. } else {
  30. msg = bcode.Message()
  31. }
  32. c.Render(http.StatusOK, render.JSON{
  33. Code: bcode.Code(),
  34. Message: msg,
  35. Data: nil,
  36. })
  37. }
  38. func checkCookieFun(c *blademaster.Context) (err error) {
  39. _, _, err = checkCookie(c)
  40. return
  41. }
  42. // service的函数原型
  43. type serviceFunc func(context context.Context, arg interface{}) (res interface{}, err error)
  44. // response writer
  45. type responseWriter func(c *blademaster.Context, arg interface{}, res interface{}, err error)
  46. type argParser func(c *blademaster.Context, arg interface{}) (err error)
  47. func argGetParser(c *blademaster.Context, arg interface{}) (err error) {
  48. err = c.Bind(arg)
  49. return
  50. }
  51. // argPostJSONParser .
  52. func argPostJSONParser(c *blademaster.Context, arg interface{}) (err error) {
  53. respBody, _ := ioutil.ReadAll(c.Request.Body)
  54. if err = json.Unmarshal(respBody, arg); err != nil {
  55. log.Error("json unmarshal fail, err=%v", err)
  56. return
  57. }
  58. if err = binding.Validator.ValidateStruct(arg); err != nil {
  59. log.Error("binding.Validator.ValidateStruct(%+v) error(%+v)", arg, err)
  60. }
  61. return
  62. }
  63. func jsonWriter(c *blademaster.Context, arg interface{}, res interface{}, err error) {
  64. c.JSON(res, err)
  65. }
  66. func csvWriter(c *blademaster.Context, arg interface{}, res interface{}, err error) {
  67. formater, ok := res.(model.CsvFormatter)
  68. if !ok {
  69. log.Error("res cannot convert CsvFommater, res type=%s", reflect.TypeOf(res).Name())
  70. c.String(ecode.ServerErr.Code(), "res cannot convert CsvFommater")
  71. return
  72. }
  73. c.Writer.Header().Set("Content-Type", "application/csv")
  74. c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment;filename=\"%s\"", formater.GetFileName()))
  75. var buf = &bytes.Buffer{}
  76. var csvWriter = csv.NewWriter(buf)
  77. formater.ToCsv(csvWriter)
  78. csvWriter.Flush()
  79. c.Writer.Write(buf.Bytes())
  80. }
  81. func decideWriter(c *blademaster.Context, arg interface{}, res interface{}, err error) {
  82. var writer responseWriter
  83. var decider, ok = arg.(model.ExportArgInterface)
  84. if ok {
  85. switch decider.ExportFormat() {
  86. case model.ResponeModelJSON:
  87. writer = jsonWriter
  88. case model.ResponeModelCSV:
  89. writer = csvWriter
  90. }
  91. }
  92. if writer != nil {
  93. writer(c, arg, res, err)
  94. } else {
  95. jsonWriter(c, arg, res, err)
  96. }
  97. }
  98. func httpGenerateFunc(arg interface{}, sfunc serviceFunc, description string, parser argParser, writer responseWriter, preFuncs ...func(*blademaster.Context) error) func(*blademaster.Context) {
  99. return func(c *blademaster.Context) {
  100. var res interface{}
  101. var err error
  102. exitswitch:
  103. switch {
  104. default:
  105. for _, f := range preFuncs {
  106. err = f(c)
  107. if err != nil {
  108. break exitswitch
  109. }
  110. }
  111. if err = parser(c, arg); err != nil {
  112. log.Error("%s, request argument bind fail, err=%v", description, err)
  113. err = ecode.RequestErr
  114. break
  115. }
  116. var scoreRes, e = sfunc(c, arg)
  117. err = e
  118. if e != nil {
  119. log.Error("%s query fail, req=%+v, err=%+v", description, arg, err)
  120. break
  121. }
  122. log.Info("%s query ok, req=%+v, result=%+v", description, arg, scoreRes)
  123. res = scoreRes
  124. }
  125. if err != nil {
  126. bmHTTPErrorWithMsg(c, err)
  127. } else {
  128. writer(c, arg, res, err)
  129. }
  130. }
  131. }
  132. func httpPostJSONCookie(arg interface{}, sfunc serviceFunc, description string, preFuncs ...func(*blademaster.Context) error) func(*blademaster.Context) {
  133. preFuncs = append(preFuncs, checkCookieFun)
  134. return httpGenerateFunc(arg, sfunc, description, argPostJSONParser, jsonWriter, preFuncs...)
  135. }
  136. // func httpPostFormCookie(arg interface{}, sfunc serviceFunc, description string, preFuncs ...func(*blademaster.Context) error) func(*blademaster.Context) {
  137. // preFuncs = append(preFuncs, checkCookieFun)
  138. // return httpGenerateFunc(arg, sfunc, description, argGetParser, jsonWriter, preFuncs...)
  139. // }
  140. func httpGetFunCheckCookie(arg interface{}, sfunc serviceFunc, description string, preFuncs ...func(*blademaster.Context) error) func(*blademaster.Context) {
  141. preFuncs = append(preFuncs, checkCookieFun)
  142. return httpGenerateFunc(arg, sfunc, description, argGetParser, jsonWriter, preFuncs...)
  143. }
  144. // func httpGetFunc(arg interface{}, sfunc serviceFunc, description string) func(*blademaster.Context) {
  145. // return httpGetFuncWithWriter(arg, sfunc, description, jsonWriter)
  146. // }
  147. func httpGetFuncWithWriter(arg interface{}, sfunc serviceFunc, description string, writer responseWriter) func(*blademaster.Context) {
  148. return httpGenerateFunc(arg, sfunc, description, argGetParser, writer)
  149. }
  150. func httpGetWriterByExport(arg interface{}, sfunc serviceFunc, description string) func(*blademaster.Context) {
  151. return httpGetFuncWithWriter(arg, sfunc, description, decideWriter)
  152. }
  153. //func offlineactivityAdd(c *blademaster.Context) {
  154. // httpPostFunCheckCookie(
  155. // new(offlineactivity.AddActivityArg),
  156. // func(context context.Context, arg interface{}) (res interface{}, err error) {
  157. // return svr.AddOfflineActivity(context, arg.(*offlineactivity.AddActivityArg))
  158. // },
  159. // "offlineactivityAdd")(c)
  160. //}