shorturl.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package http
  2. import (
  3. "context"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "go-common/app/interface/main/shorturl/conf"
  9. "go-common/app/interface/main/shorturl/model"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. bm "go-common/library/net/http/blademaster"
  13. )
  14. // add short url from long url.
  15. func add(c *bm.Context) {
  16. param := &model.Param{}
  17. if err := c.Bind(param); err != nil {
  18. return
  19. }
  20. // check args
  21. uri := strings.TrimSpace(param.Uri)
  22. if uri == "" {
  23. log.Error("add short url args empty long(%s)", uri)
  24. c.JSON(nil, ecode.RequestErr)
  25. return
  26. }
  27. short, err := suSvr.Add(c, param.Mid, uri)
  28. if err != nil {
  29. log.Error("suSvr.Add error(%v)", err)
  30. c.JSON(nil, ecode.RequestErr)
  31. return
  32. }
  33. data := map[string]string{
  34. "url": conf.Conf.Host.Default + short,
  35. }
  36. c.JSON(data, nil)
  37. }
  38. // jump redirect short url to long url.
  39. func jump(c *bm.Context) {
  40. // check path
  41. if len(c.Request.URL.Path) == 0 || c.Request.URL.Path == "/" || c.Request.URL.Path == "/favicon.ico" || strings.HasPrefix(c.Request.URL.Path, "/x/") {
  42. c.JSON(nil, ecode.NothingFound)
  43. return
  44. }
  45. su, err := suSvr.ShortCache(c, c.Request.URL.Path[1:])
  46. if err != nil {
  47. log.Error("suSvr.Get url(%v) error(%v)", c.Request.URL.Path[1:], err)
  48. c.JSON(nil, err)
  49. return
  50. }
  51. if su == nil || su.Long == "" || su.State == model.StateDelted {
  52. c.JSON(nil, ecode.NothingFound)
  53. return
  54. }
  55. if !strings.HasPrefix(su.Long, "http://") && !strings.HasPrefix(su.Long, "https://") {
  56. su.Long = "http://" + su.Long
  57. return
  58. }
  59. // redirect
  60. http.Redirect(c.Writer, c.Request, su.Long, http.StatusFound)
  61. }
  62. // shortAll get shorturl list
  63. func shortAll(c *bm.Context) {
  64. param := &model.Param{}
  65. if err := c.Bind(param); err != nil {
  66. return
  67. }
  68. pn, err := strconv.Atoi(param.Pn)
  69. if err != nil || pn < 1 {
  70. pn = 1
  71. }
  72. ps, err := strconv.Atoi(param.Ps)
  73. if err != nil || ps > 20 || ps <= 0 {
  74. ps = 20
  75. }
  76. long := strings.TrimSpace(param.Uri)
  77. data, err := suSvr.ShortLimit(c, pn, ps, param.Mid, long)
  78. if err != nil {
  79. log.Error("suSvr.ShortLimit error(%v)", err)
  80. c.JSON(nil, ecode.RequestErr)
  81. return
  82. }
  83. for _, su := range data {
  84. su.Short = conf.Conf.Host.Default + su.Short
  85. }
  86. c.JSONMap(map[string]interface{}{
  87. "data": data,
  88. "size": 2233,
  89. }, nil)
  90. }
  91. // shortState set state
  92. func shortUpdate(c *bm.Context) {
  93. param := &model.Param{}
  94. if err := c.Bind(param); err != nil {
  95. return
  96. }
  97. uri := strings.TrimSpace(param.Uri)
  98. if uri == "" {
  99. log.Error("add short url args empty long(%s)", param.Uri)
  100. c.JSON(nil, ecode.RequestErr)
  101. return
  102. }
  103. if param.Mid <= 0 {
  104. log.Error("mid less than 0 error(%v)", param.Mid)
  105. c.JSON(nil, ecode.RequestErr)
  106. return
  107. }
  108. err := suSvr.ShortUpdate(context.TODO(), param.ID, param.Mid, uri)
  109. if err != nil {
  110. log.Error("suSvr.ShortUpdate error(%v)", err)
  111. c.JSON(nil, err)
  112. return
  113. }
  114. c.JSON(nil, nil)
  115. }
  116. // shortState set state
  117. func shortDel(c *bm.Context) {
  118. param := &model.Param{}
  119. if err := c.Bind(param); err != nil {
  120. return
  121. }
  122. if param.Mid <= 0 {
  123. log.Error("mid less than 0 error(%v)", param.Mid)
  124. c.JSON(nil, ecode.RequestErr)
  125. return
  126. }
  127. err := suSvr.ShortDel(c, param.ID, param.Mid, time.Now())
  128. if err != nil {
  129. log.Error("suSvr.ShortState error(%v)", err)
  130. c.JSON(nil, err)
  131. return
  132. }
  133. c.JSON(nil, nil)
  134. }
  135. // shortById by id
  136. func shortByID(c *bm.Context) {
  137. param := &model.Param{}
  138. if err := c.Bind(param); err != nil {
  139. return
  140. }
  141. data, err := suSvr.ShortByID(c, param.ID)
  142. if err != nil {
  143. log.Error("suSvr.ShortState error(%v)", err)
  144. c.JSON(nil, err)
  145. return
  146. }
  147. c.JSON(data, nil)
  148. }
  149. func ping(c *bm.Context) {
  150. if err := suSvr.Ping(c); err != nil {
  151. c.AbortWithStatus(http.StatusServiceUnavailable)
  152. log.Error("shorturl service ping error(%v)", err)
  153. }
  154. }