Skip to content

Commit

Permalink
Refactor request context handling: consolidate context keys into a si…
Browse files Browse the repository at this point in the history
…ngle key for simplicity and improve data retrieval
  • Loading branch information
eduardolat committed Aug 29, 2024
1 parent ac17ec7 commit 4805f57
Showing 1 changed file with 6 additions and 25 deletions.
31 changes: 6 additions & 25 deletions internal/view/reqctx/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import (
"github.com/labstack/echo/v4"
)

// Context keys to avoid typos
const (
isAuthedKey = "isAuthed"
sessionIDKey = "sessionId"
userKey = "user"
ctxKey = "PGBackWebCTX"
)

// Ctx represents the values passed through a single request context.
Expand All @@ -22,30 +19,14 @@ type Ctx struct {

// SetCtx inserts values into the Echo request context.
func SetCtx(c echo.Context, ctx Ctx) {
c.Set(isAuthedKey, ctx.IsAuthed)
c.Set(sessionIDKey, ctx.SessionID)
c.Set(userKey, ctx.User)
c.Set(ctxKey, ctx)
}

// GetCtx retrieves values from the Echo request context.
func GetCtx(c echo.Context) Ctx {
var isAuthed bool
var sessionID uuid.UUID
var user dbgen.User

if ia, ok := c.Get(isAuthedKey).(bool); ok {
isAuthed = ia
}
if sid, ok := c.Get(sessionIDKey).(uuid.UUID); ok {
sessionID = sid
}
if au, ok := c.Get(userKey).(dbgen.User); ok {
user = au
}

return Ctx{
IsAuthed: isAuthed,
SessionID: sessionID,
User: user,
ctx, ok := c.Get(ctxKey).(Ctx)
if !ok {
return Ctx{}
}
return ctx
}

0 comments on commit 4805f57

Please sign in to comment.