http.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package http
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "net/http"
  6. "net/url"
  7. "strings"
  8. "go-common/app/interface/main/spread/conf"
  9. "go-common/app/interface/main/spread/service"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/net/http/blademaster/render"
  14. )
  15. var (
  16. svc *service.Service
  17. business = make(map[string]string)
  18. )
  19. // Init init
  20. func Init(c *conf.Config) {
  21. initService(c)
  22. // init router
  23. engine := bm.DefaultServer(c.BM)
  24. outerRouter(engine)
  25. if err := engine.Start(); err != nil {
  26. log.Error("xhttp.Serve error(%v)", err)
  27. panic(err)
  28. }
  29. for _, b := range c.Businesses {
  30. business[b.Appkey] = b.AppSecret
  31. }
  32. }
  33. // initService init services.
  34. func initService(c *conf.Config) {
  35. svc = service.New(c)
  36. }
  37. // outerRouter init outer router api path.
  38. func outerRouter(e *bm.Engine) {
  39. //init api
  40. e.Ping(ping)
  41. group := e.Group("/x/spread", Verify)
  42. {
  43. bangumi := group.Group("/bangumi")
  44. {
  45. bangumi.GET("/content", bangumiContent)
  46. bangumi.GET("/offshelve", bangumiOff)
  47. }
  48. }
  49. }
  50. // ping check server ok.
  51. func ping(c *bm.Context) {
  52. }
  53. // Verify will inject into handler func as verify required
  54. func Verify(ctx *bm.Context) {
  55. var (
  56. secret string
  57. ok bool
  58. )
  59. req := ctx.Request
  60. params := req.Form
  61. if req.Method == "POST" {
  62. // Give priority to sign in url query, otherwise check sign in post form.
  63. q := req.URL.Query()
  64. if q.Get("sign") != "" {
  65. params = q
  66. }
  67. }
  68. // check timestamp is not empty (TODO : Check if out of some seconds.., like 100s)
  69. if params.Get("ts") == "" {
  70. log.Error("ts is empty")
  71. ctx.JSON(nil, ecode.RequestErr)
  72. ctx.Abort()
  73. return
  74. }
  75. sign := params.Get("sign")
  76. params.Del("sign")
  77. sappkey := params.Get("appkey")
  78. secret, ok = business[sappkey]
  79. if !ok {
  80. ctx.JSON(nil, ecode.AppKeyInvalid)
  81. ctx.Abort()
  82. return
  83. }
  84. if hsign := paramsSign(params, sappkey, secret, true); hsign != sign {
  85. if hsign1 := paramsSign(params, sappkey, secret, false); hsign1 != sign {
  86. log.Error("Get sign: %s, expect %s", sign, hsign)
  87. ctx.JSON(nil, ecode.SignCheckErr)
  88. ctx.Abort()
  89. return
  90. }
  91. }
  92. }
  93. // Render .
  94. func Render(c *bm.Context, code int, msg string, data interface{}, total int64, err error) {
  95. c.Error = err
  96. bcode := ecode.Cause(err)
  97. if err != nil {
  98. c.Render(http.StatusOK, render.JSON{
  99. Code: bcode.Code(),
  100. Message: bcode.Message(),
  101. Data: data,
  102. })
  103. return
  104. }
  105. c.Render(http.StatusOK, render.MapJSON{
  106. "code": code,
  107. "message": msg,
  108. "data": data,
  109. "total": total,
  110. })
  111. }
  112. // sign is used to sign form params by given condition.
  113. func paramsSign(params url.Values, appkey string, secret string, lower bool) (hexdigest string) {
  114. data := params.Encode()
  115. if strings.IndexByte(data, '+') > -1 {
  116. data = strings.Replace(data, "+", "%20", -1)
  117. }
  118. if lower {
  119. data = strings.ToLower(data)
  120. }
  121. digest := md5.Sum([]byte(data + secret))
  122. hexdigest = hex.EncodeToString(digest[:])
  123. return
  124. }