redirect.go 630 B

12345678910111213141516171819202122232425262728293031323334
  1. package middleware
  2. import (
  3. "net/http"
  4. "strings"
  5. "github.com/gin-gonic/gin"
  6. )
  7. func RedirectMiddleware(basePath string) gin.HandlerFunc {
  8. return func(c *gin.Context) {
  9. // Redirect from old '/xui' path to '/panel'
  10. redirects := map[string]string{
  11. "panel/API": "panel/api",
  12. "xui/API": "panel/api",
  13. "xui": "panel",
  14. }
  15. path := c.Request.URL.Path
  16. for from, to := range redirects {
  17. from, to = basePath+from, basePath+to
  18. if strings.HasPrefix(path, from) {
  19. newPath := to + path[len(from):]
  20. c.Redirect(http.StatusMovedPermanently, newPath)
  21. c.Abort()
  22. return
  23. }
  24. }
  25. c.Next()
  26. }
  27. }