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

@@ -13,15 +13,15 @@ import (
)
func CreateRouter() *gin.Engine {
router := gin.Default(func(e *gin.Engine) {
e.RemoveExtraSlash = true
e.RedirectTrailingSlash = false
})
if config.Config.Debug {
router.Use(middleware.RequestLogger())
}
router.Use(middleware.StripTrailingSlashes(router))
router.Use(middleware.Authentication())
router.GET("/dbs/:databaseId/colls/:collId/pkranges", handlers.GetPartitionKeyRanges)
@@ -52,30 +52,11 @@ func CreateRouter() *gin.Engine {
router.GET("/cosmium/export", handlers.CosmiumExport)
addRoutesForTrailingSlashes(router)
handlers.RegisterExplorerHandlers(router)
return router
}
func addRoutesForTrailingSlashes(router *gin.Engine) {
trailingSlashGroup := router.Group("/")
//prepend, so slash is stripped before authentication middleware reads path
trailingSlashGroup.Handlers = prepend(trailingSlashGroup.Handlers, middleware.TrailingSlashStripper())
for _, route := range router.Routes() {
if route.Path != "/" { //don't append slash to root path, already handled by RemoveExtraSlash
trailingSlashGroup.Handle(route.Method, route.Path+"/", route.HandlerFunc)
}
}
}
func prepend[T any](a []T, e T) []T {
a = append([]T{e}, a...)
return a
}
func StartAPI() {
if !config.Config.Debug {
gin.SetMode(gin.ReleaseMode)