mirror of
https://github.com/pikami/cosmium.git
synced 2025-12-19 00:40:47 +00:00
Strip trailing slash using middleware
This commit is contained in:
18
api/handlers/middleware/strip_trailing_slashes.go
Normal file
18
api/handlers/middleware/strip_trailing_slashes.go
Normal 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()
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user