gin context util + doc fix
This commit is contained in:
parent
f159014374
commit
a7edae9113
3 changed files with 34 additions and 1 deletions
32
pkg/utils/ginContext.go
Normal file
32
pkg/utils/ginContext.go
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type contextUtil interface {
|
||||||
|
GetUserUuidFromContext(c *gin.Context) (string, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) GetUserUuidFromContext(c *gin.Context) (string, error) {
|
||||||
|
if c == nil {
|
||||||
|
return "", errors.New("context is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
uuidRaw, exists := c.Get("userUuid")
|
||||||
|
if !exists {
|
||||||
|
return "", errors.New("user uuid not found in context")
|
||||||
|
}
|
||||||
|
|
||||||
|
userUuidStr, ok := uuidRaw.(string)
|
||||||
|
if !ok {
|
||||||
|
return "", errors.New("user uuid is not a string")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := uuid.Validate(userUuidStr); err != nil {
|
||||||
|
return "", errors.New("error validating user uuid")
|
||||||
|
}
|
||||||
|
return userUuidStr, nil
|
||||||
|
}
|
||||||
|
|
@ -3,4 +3,5 @@ package utils
|
||||||
type Utils interface {
|
type Utils interface {
|
||||||
timeUtil
|
timeUtil
|
||||||
cpuUtil
|
cpuUtil
|
||||||
|
contextUtil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ type timeUtil interface {
|
||||||
// TimeNowUTC Use it everywhere, when you need current time.
|
// TimeNowUTC Use it everywhere, when you need current time.
|
||||||
TimeNowUTC() time.Time
|
TimeNowUTC() time.Time
|
||||||
|
|
||||||
// DeletedNullTime returns empty sql.NullTime struct.
|
// DeletedNullTime returns empty sql.NullTime struct with valid = false.
|
||||||
DeletedNullTime() sql.NullTime
|
DeletedNullTime() sql.NullTime
|
||||||
|
|
||||||
// NullTimeNowUTC returns valid sql.NullTime struct with current time in UTC.
|
// NullTimeNowUTC returns valid sql.NullTime struct with current time in UTC.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue