Strip trailing slash using middleware

This commit is contained in:
Pijus Kamandulis
2024-10-28 20:20:52 +02:00
parent 827046f634
commit 0e98e3481a
4 changed files with 27 additions and 40 deletions

View File

@@ -0,0 +1,18 @@
package middleware
import (
"github.com/gin-gonic/gin"
)
func StripTrailingSlashes(r *gin.Engine) gin.HandlerFunc {
return func(c *gin.Context) {
path := c.Request.URL.Path
if len(path) > 1 && path[len(path)-1] == '/' {
c.Request.URL.Path = path[:len(path)-1]
r.HandleContext(c)
c.Abort()
return
}
c.Next()
}
}

View File

@@ -1,17 +0,0 @@
package middleware
import (
"strings"
"github.com/gin-gonic/gin"
)
func TrailingSlashStripper() gin.HandlerFunc {
return func(c *gin.Context) {
if (len(c.Request.URL.Path)) > 1 { //dont strip root dir slash, path="/"
var stripped_path = strings.TrimSuffix(c.Request.URL.Path, "/")
c.Request.URL.Path = stripped_path
}
c.Next()
}
}