Always redirect to a postfixed path

main
Nikita Tokarchuk 2 years ago
parent 8f3e81fac3
commit 1e6cbd574f
Signed by: mainnika
GPG Key ID: A595FB7E3E56911C
  1. 19
      pkg/routes/index.go
  2. 1
      pkg/routes/routes.go

@ -25,6 +25,25 @@ func (r *Routes) rootRedirect(c *routing.Context) (err error) {
return r.relativeRedirectBytes(c, []byte(templates.URLIndex), fasthttp.StatusFound) return r.relativeRedirectBytes(c, []byte(templates.URLIndex), fasthttp.StatusFound)
} }
// rootRedirect forcefully adds postfix to the url
func (r *Routes) usePostfixForce(c *routing.Context) (err error) {
fullPath := c.Path()
if len(fullPath) <= 1 {
return c.Next()
}
dotIndex := bytes.LastIndexByte(fullPath, '.')
if dotIndex >= 0 {
return c.Next()
}
fullPath = append(fullPath, '.')
fullPath = append(fullPath, []byte(templates.URLPostfix)...)
return r.relativeRedirectBytes(c, fullPath, fasthttp.StatusFound)
}
// index handler renders index data // index handler renders index data
func (r *Routes) index(c *routing.Context) (err error) { func (r *Routes) index(c *routing.Context) (err error) {

@ -37,6 +37,7 @@ func (r *Routes) init() {
router.Use(r.useTemplateWriter) router.Use(r.useTemplateWriter)
router.Use(r.useErrorHandler) router.Use(r.useErrorHandler)
router.Use(r.usePostfixForce)
router.NotFound(r.errorNotFound) router.NotFound(r.errorNotFound)
root := router.Group(r.Base) root := router.Group(r.Base)

Loading…
Cancel
Save