2024-10-15 14:31:19 -04:00
|
|
|
package ctx
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
|
)
|
|
|
|
|
|
2024-10-21 11:30:52 -04:00
|
|
|
// ╭───────────────────────────────────────────────────────────╮
|
|
|
|
|
// │ HwayContext struct methods │
|
|
|
|
|
// ╰───────────────────────────────────────────────────────────╯
|
|
|
|
|
|
|
|
|
|
// HwayContext is the context for Highway endpoints.
|
2024-10-15 14:31:19 -04:00
|
|
|
type HwayContext struct {
|
|
|
|
|
echo.Context
|
|
|
|
|
|
|
|
|
|
// Defaults
|
|
|
|
|
id string // Generated ksuid http cookie; Initialized on first request
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-21 11:30:52 -04:00
|
|
|
// ID returns the ksuid http cookie
|
2024-10-15 14:31:19 -04:00
|
|
|
func (s *HwayContext) ID() string {
|
|
|
|
|
return s.id
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-21 11:30:52 -04:00
|
|
|
// GetHwayContext returns the HwayContext from the echo context.
|
2024-10-15 14:31:19 -04:00
|
|
|
func GetHWAYContext(c echo.Context) (*HwayContext, error) {
|
|
|
|
|
ctx, ok := c.(*HwayContext)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, echo.NewHTTPError(http.StatusInternalServerError, "Highway Context not found")
|
|
|
|
|
}
|
|
|
|
|
return ctx, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HighwaySessionMiddleware establishes a Session Cookie.
|
|
|
|
|
func HighwaySessionMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
|
sessionID := GetSessionID(c)
|
|
|
|
|
cc := &HwayContext{
|
|
|
|
|
Context: c,
|
|
|
|
|
id: sessionID,
|
|
|
|
|
}
|
|
|
|
|
return next(cc)
|
|
|
|
|
}
|
|
|
|
|
}
|