29 lines
465 B
Go
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()
|
||
|
}
|
||
|
}
|