admin.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package controller
  2. import (
  3. "fmt"
  4. "net/http"
  5. "ohurlshortener/service"
  6. "ohurlshortener/utils"
  7. "github.com/gin-gonic/gin"
  8. )
  9. func LoginPage(c *gin.Context) {
  10. c.HTML(http.StatusOK, "login.html", gin.H{
  11. "title": "登录 - ohUrlShortener",
  12. })
  13. }
  14. func DoLogin(c *gin.Context) {
  15. //TODO: Login logic
  16. }
  17. func DoLogout(c *gin.Context) {
  18. //TODO: Login logic
  19. }
  20. func DashbaordPage(c *gin.Context) {
  21. c.HTML(http.StatusOK, "dashboard.html", gin.H{
  22. "title": "仪表盘 - ohUrlShortener",
  23. "current_url": c.Request.URL.Path,
  24. })
  25. }
  26. func UrlsPage(c *gin.Context) {
  27. c.HTML(http.StatusOK, "urls.html", gin.H{
  28. "title": "短链接列表 - ohUrlShortener",
  29. "current_url": c.Request.URL.Path,
  30. })
  31. }
  32. func AccessLogsPage(c *gin.Context) {
  33. c.HTML(http.StatusOK, "access_logs.html", gin.H{
  34. "title": "访问日志查询 - ohUrlShortener",
  35. "current_url": c.Request.URL.Path,
  36. })
  37. }
  38. func ShortUrlsStats(c *gin.Context) {
  39. url := c.Param("url")
  40. if utils.EemptyString(url) {
  41. c.JSON(http.StatusBadRequest, gin.H{
  42. "code": http.StatusBadRequest,
  43. "message": "缺少参数 url",
  44. "result": nil,
  45. })
  46. return
  47. }
  48. found, err := service.GetShortUrlStats(url)
  49. if err != nil {
  50. c.JSON(http.StatusInternalServerError, gin.H{
  51. "code": http.StatusInternalServerError,
  52. "message": err.Error(),
  53. "result": nil,
  54. })
  55. return
  56. }
  57. c.JSON(http.StatusOK, gin.H{
  58. "code": http.StatusOK,
  59. "message": "success",
  60. "result": found,
  61. })
  62. }
  63. func GetShortUrls(c *gin.Context) {
  64. urls, err := service.GetAllShortUrls()
  65. if err != nil {
  66. c.JSON(http.StatusInternalServerError, gin.H{
  67. "code": http.StatusInternalServerError,
  68. "message": err.Error(),
  69. "result": nil,
  70. })
  71. return
  72. }
  73. c.JSON(http.StatusOK, gin.H{
  74. "code": http.StatusOK,
  75. "message": "success",
  76. "result": urls,
  77. })
  78. }
  79. func ReloadRedis(c *gin.Context) {
  80. result, err := service.ReloadUrls()
  81. if err != nil {
  82. c.JSON(http.StatusBadRequest, gin.H{
  83. "code": http.StatusBadRequest,
  84. "message": err.Error(),
  85. "result": nil,
  86. })
  87. return
  88. }
  89. c.JSON(http.StatusOK, gin.H{
  90. "code": http.StatusOK,
  91. "message": "success",
  92. "result": result,
  93. })
  94. }
  95. func GenerateShortUrl(c *gin.Context) {
  96. destUrl := c.PostForm("dest_url")
  97. if utils.EemptyString(destUrl) {
  98. c.JSON(http.StatusBadRequest, gin.H{
  99. "code": http.StatusBadRequest,
  100. "message": "缺少参数 dest_url",
  101. "result": nil,
  102. })
  103. return
  104. }
  105. shortUrl, err := service.GenerateShortUrl(destUrl)
  106. if err != nil {
  107. c.JSON(http.StatusBadRequest, gin.H{
  108. "code": http.StatusBadRequest,
  109. "message": err.Error(),
  110. "result": nil,
  111. })
  112. return
  113. }
  114. c.JSON(http.StatusOK, gin.H{
  115. "code": http.StatusOK,
  116. "message": "success",
  117. "result": fmt.Sprintf("https://i.barats.cn/l/%s", shortUrl),
  118. })
  119. }