dynamic.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package http
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "net/http"
  6. "time"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/metadata"
  11. "go-common/library/stat/prom"
  12. )
  13. func dynamicNew(c *bm.Context) {
  14. params := c.Request.Form
  15. data, err := externalSvc.DynamicNew(c, params.Encode())
  16. dynamicResult(c, data, err)
  17. }
  18. func dynamicCount(c *bm.Context) {
  19. params := c.Request.Form
  20. data, err := externalSvc.DynamicCount(c, params.Encode())
  21. dynamicResult(c, data, err)
  22. }
  23. func dynamicHistory(c *bm.Context) {
  24. params := c.Request.Form
  25. data, err := externalSvc.DynamicHistory(c, params.Encode())
  26. dynamicResult(c, data, err)
  27. }
  28. func dynamicResult(c *bm.Context, data json.RawMessage, err error) {
  29. params := c.Request.Form
  30. path := c.Request.URL.Path
  31. code := ecode.OK
  32. if err != nil {
  33. code = ecode.Int(-22)
  34. c.JSON(nil, code)
  35. } else {
  36. if !bytes.Contains(data, []byte(`"code":0`)) {
  37. var res struct {
  38. Code int `json:"code"`
  39. }
  40. json.Unmarshal(data, &res)
  41. code = ecode.Int(res.Code)
  42. }
  43. c.Bytes(http.StatusOK, "text/json; charset=utf-8", data)
  44. }
  45. prom.HTTPServer.Incr("no_user", path[1:], code.Error())
  46. prom.HTTPServer.Timing("no_user", int64(time.Since(time.Now())/time.Millisecond), path[1:])
  47. log.Info("method:%s,mid:%v,ip:%s,params:%s,ret:%d[%s] stack:%+v", path, params.Get("uid"), metadata.String(c, metadata.RemoteIP), params.Encode(), code.Code(), code.Message(), err)
  48. }