urls_controller.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. "net/http"
  11. "ohurlshortener/service"
  12. "ohurlshortener/utils"
  13. "github.com/gin-gonic/gin"
  14. )
  15. func ShortUrlDetail(c *gin.Context) {
  16. url := c.Param("url")
  17. if utils.EemptyString(url) {
  18. c.HTML(http.StatusNotFound, "error.html", gin.H{
  19. "title": "404 - ohUrlShortener",
  20. "code": http.StatusNotFound,
  21. "message": "您访问的页面已失效",
  22. "label": "Status Not Found",
  23. })
  24. return
  25. }
  26. destUrl, err := service.Search4ShortUrl(url)
  27. if err != nil {
  28. c.HTML(http.StatusInternalServerError, "error.html", gin.H{
  29. "title": "内部错误 - ohUrlShortener",
  30. "code": http.StatusInternalServerError,
  31. "message": err.Error(),
  32. "label": "Error",
  33. })
  34. return
  35. }
  36. if utils.EemptyString(destUrl) {
  37. c.HTML(http.StatusNotFound, "error.html", gin.H{
  38. "title": "404 - ohUrlShortener",
  39. "code": http.StatusNotFound,
  40. "message": "您访问的页面已失效",
  41. "label": "Status Not Found",
  42. })
  43. return
  44. }
  45. go service.NewAccessLog(url, c.ClientIP(), c.Request.UserAgent(), c.Request.Referer()) //TODO: add more params to access logs
  46. c.Redirect(http.StatusFound, destUrl)
  47. }