api_controller.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright (c) [2022] [巴拉迪维 BaratSemet]
  2. // [ohUrlShortener] is licensed under Mulan PSL v2.
  3. // You can use this software according to the terms and conditions of the Mulan PSL v2.
  4. // You may obtain a copy of Mulan PSL v2 at:
  5. // http://license.coscl.org.cn/MulanPSL2
  6. // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  7. // See the Mulan PSL v2 for more details.
  8. package controller
  9. import (
  10. "fmt"
  11. "net/http"
  12. "strconv"
  13. "strings"
  14. "ohurlshortener/core"
  15. "ohurlshortener/service"
  16. "ohurlshortener/utils"
  17. "github.com/gin-gonic/gin"
  18. )
  19. // APINewAdmin
  20. //
  21. // Add new admin user
  22. func APINewAdmin(ctx *gin.Context) {
  23. account := ctx.PostForm("account")
  24. password := ctx.PostForm("password")
  25. if utils.EmptyString(account) || utils.EmptyString(password) {
  26. ctx.JSON(http.StatusBadRequest, core.ResultJsonBadRequest("用户名或密码不能为空"))
  27. return
  28. }
  29. if len(password) < 8 {
  30. ctx.JSON(http.StatusBadRequest, core.ResultJsonBadRequest("密码长度最少8位"))
  31. return
  32. }
  33. err := service.NewUser(account, password)
  34. if err != nil {
  35. ctx.JSON(http.StatusBadRequest, core.ResultJsonBadRequest(err.Error()))
  36. return
  37. }
  38. ctx.JSON(http.StatusOK, core.ResultJsonSuccess())
  39. }
  40. // APIAdminUpdate
  41. //
  42. // Update password of given admin user
  43. func APIAdminUpdate(ctx *gin.Context) {
  44. account := ctx.Param("account")
  45. password := ctx.PostForm("password")
  46. if utils.EmptyString(account) || utils.EmptyString(password) {
  47. ctx.JSON(http.StatusBadRequest, core.ResultJsonBadRequest("用户名或密码不能为空"))
  48. return
  49. }
  50. if len(password) < 8 {
  51. ctx.JSON(http.StatusBadRequest, core.ResultJsonBadRequest("密码长度最少8位"))
  52. return
  53. }
  54. err := service.UpdatePassword(strings.TrimSpace(account), password)
  55. if err != nil {
  56. ctx.JSON(http.StatusBadRequest, core.ResultJsonBadRequest("修改失败"))
  57. return
  58. }
  59. ctx.JSON(http.StatusOK, core.ResultJsonSuccess())
  60. }
  61. // APIGenShortUrl Generate new short url
  62. func APIGenShortUrl(ctx *gin.Context) {
  63. url := ctx.PostForm("dest_url")
  64. memo := ctx.PostForm("memo")
  65. strOpenType := ctx.PostForm("open_type")
  66. openType, err := strconv.Atoi(strOpenType)
  67. if err != nil {
  68. openType = int(core.OpenInAll)
  69. }
  70. if utils.EmptyString(strings.TrimSpace(url)) {
  71. ctx.JSON(http.StatusBadRequest, core.ResultJsonBadRequest("dest_url 不能为空"))
  72. return
  73. }
  74. res, err := service.GenerateShortUrl(strings.TrimSpace(url), strings.TrimSpace(memo), openType)
  75. if err != nil {
  76. ctx.JSON(http.StatusBadRequest, core.ResultJsonBadRequest(err.Error()))
  77. return
  78. }
  79. json := map[string]string{
  80. "short_url": fmt.Sprintf("%s%s", utils.AppConfig.UrlPrefix, res),
  81. }
  82. ctx.JSON(http.StatusOK, core.ResultJsonSuccessWithData(json))
  83. }
  84. // APIUrlInfo Get Short Url Stat Info.
  85. func APIUrlInfo(ctx *gin.Context) {
  86. url := ctx.Param("url")
  87. if utils.EmptyString(strings.TrimSpace(url)) {
  88. ctx.JSON(http.StatusBadRequest, core.ResultJsonBadRequest("url 不能为空"))
  89. return
  90. }
  91. stat, err := service.GetShortUrlStats(strings.TrimSpace(url))
  92. if err != nil {
  93. ctx.JSON(http.StatusInternalServerError, core.ResultJsonError(err.Error()))
  94. return
  95. }
  96. ctx.JSON(http.StatusOK, core.ResultJsonSuccessWithData(stat))
  97. }
  98. // APIUpdateUrl Enable or Disable Short Url
  99. func APIUpdateUrl(ctx *gin.Context) {
  100. url := ctx.Param("url")
  101. enableStr := ctx.PostForm("enable")
  102. if utils.EmptyString(strings.TrimSpace(url)) {
  103. ctx.JSON(http.StatusBadRequest, core.ResultJsonBadRequest("url 不能为空"))
  104. return
  105. }
  106. enable, err := strconv.ParseBool(enableStr)
  107. if err != nil {
  108. ctx.JSON(http.StatusBadRequest, core.ResultJsonBadRequest("enable 参数值非法"))
  109. return
  110. }
  111. res, err := service.ChangeState(url, enable)
  112. if err != nil {
  113. ctx.JSON(http.StatusBadRequest, core.ResultJsonBadRequest(err.Error()))
  114. return
  115. }
  116. ctx.JSON(http.StatusOK, core.ResultJsonSuccessWithData(res))
  117. }
  118. // APIDeleteUrl Delete Short Url
  119. func APIDeleteUrl(ctx *gin.Context) {
  120. url := ctx.Param("url")
  121. if utils.EmptyString(strings.TrimSpace(url)) {
  122. ctx.JSON(http.StatusBadRequest, core.ResultJsonBadRequest("url 不能为空"))
  123. return
  124. }
  125. err := service.DeleteUrlAndAccessLogs(url)
  126. if err != nil {
  127. ctx.JSON(http.StatusBadRequest, core.ResultJsonBadRequest(err.Error()))
  128. return
  129. }
  130. ctx.JSON(http.StatusOK, core.ResultJsonSuccess)
  131. }