error.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. c.EnableRender = false
  30. request := c.Ctx.Request
  31. accept := request.Header.Get("accept")
  32. logs.Warn("404", request.RequestURI, accept)
  33. if strings.Contains(accept, "json") {
  34. c.Ctx.Output.SetStatus(http.StatusOK)
  35. c.Data["json"] = models.RespData{
  36. Code: 404,
  37. Msg: "API NOT FOUND!",
  38. }
  39. c.ServeJSON()
  40. } else if c.WebPath != request.RequestURI {
  41. c.Redirect(c.WebPath, http.StatusMovedPermanently)
  42. } else {
  43. c.Data["json"] = models.RespData{
  44. Code: 404,
  45. Msg: "Not Found",
  46. }
  47. c.ServeJSON()
  48. }
  49. }