mirror of
https://github.com/onsonr/sonr.git
synced 2025-03-10 21:09:11 +00:00
54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package context
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
config "github.com/onsonr/sonr/pkg/config/hway"
|
|
"github.com/onsonr/sonr/pkg/database/sessions"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Middleware creates a new session middleware
|
|
func Middleware(db *gorm.DB, env config.Hway) echo.MiddlewareFunc {
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
cc := NewHTTPContext(c, db)
|
|
if err := cc.InitSession(); err != nil {
|
|
return err
|
|
}
|
|
return next(cc)
|
|
}
|
|
}
|
|
}
|
|
|
|
// HTTPContext is the context for HTTP endpoints.
|
|
type HTTPContext struct {
|
|
echo.Context
|
|
db *gorm.DB
|
|
sess *sessions.Session
|
|
env config.Hway
|
|
}
|
|
|
|
// Get returns the HTTPContext from the echo context
|
|
func Get(c echo.Context) (*HTTPContext, error) {
|
|
ctx, ok := c.(*HTTPContext)
|
|
if !ok {
|
|
return nil, echo.NewHTTPError(http.StatusInternalServerError, "Session Context not found")
|
|
}
|
|
return ctx, nil
|
|
}
|
|
|
|
// NewHTTPContext creates a new session context
|
|
func NewHTTPContext(c echo.Context, db *gorm.DB) *HTTPContext {
|
|
return &HTTPContext{
|
|
Context: c,
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
// Session returns the current session
|
|
func (s *HTTPContext) Session() *sessions.Session {
|
|
return s.sess
|
|
}
|