answerer/middleware/middleware.go
2024-02-19 21:27:12 +03:00

29 lines
465 B
Go

package middleware
import (
"github.com/gofiber/fiber/v2"
"github.com/rs/xid"
)
type ContextKey string
const (
SessionKey = "X-SessionKey"
)
func AnswererChain() fiber.Handler {
return func(c *fiber.Ctx) error {
session := c.Get(SessionKey)
if session == "" {
session := xid.New().String()
c.Set(SessionKey, session)
c.Locals(ContextKey(SessionKey), session)
} else {
c.Locals(ContextKey(SessionKey), session)
}
return c.Next()
}
}