mirror of
https://github.com/sonr-io/sonr.git
synced 2026-08-02 17:31:39 +00:00
refactor: move gateway package to app directory
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/onsonr/sonr/app/gateway/config"
|
||||
"github.com/onsonr/sonr/app/gateway/internal/database"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Middleware creates a new session middleware
|
||||
func Middleware(db *gorm.DB, env config.Env) 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 *database.Session
|
||||
env config.Env
|
||||
}
|
||||
|
||||
// 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() *database.Session {
|
||||
return s.sess
|
||||
}
|
||||
Reference in New Issue
Block a user