error.go 952 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package base
  2. import (
  3. "github.com/astaxie/beego"
  4. "github.com/astaxie/beego/logs"
  5. "net/http"
  6. config2 "nginx-ui/server/config"
  7. "nginx-ui/server/models"
  8. "strings"
  9. )
  10. type ErrorController struct {
  11. beego.Controller
  12. WebPath string
  13. }
  14. func getWebPath() string {
  15. config := config2.Config
  16. p := config.ContextPath
  17. if !strings.HasPrefix(p, "/") {
  18. p = "/" + p
  19. }
  20. if !strings.HasSuffix(p, "/") {
  21. p = p + "/"
  22. }
  23. return p
  24. }
  25. func (c *ErrorController) Error404() {
  26. if c.WebPath == "" {
  27. c.WebPath = getWebPath()
  28. }
  29. request := c.Ctx.Request
  30. accept := request.Header.Get("accept")
  31. logs.Warn("404", accept)
  32. if strings.Contains(accept, "json") {
  33. c.Data["json"] = models.RespData{
  34. Code: -2,
  35. Msg: "server error",
  36. }
  37. c.ServeJSON()
  38. } else if c.WebPath != request.RequestURI {
  39. c.Redirect(c.WebPath, http.StatusMovedPermanently)
  40. } else {
  41. c.Data["json"] = models.RespData{
  42. Code: 404,
  43. Msg: "Not Found",
  44. }
  45. c.ServeJSON()
  46. }
  47. }