| Total Lines | 50 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | package controllers |
||
| 2 | |||
| 3 | import ( |
||
| 4 | "sync" |
||
| 5 | |||
| 6 | "github.com/gofiber/fiber/v2" |
||
| 7 | "github.com/gofiber/fiber/v2/log" |
||
| 8 | "github.com/memnix/memnix-rest/domain" |
||
| 9 | "github.com/memnix/memnix-rest/pkg/json" |
||
| 10 | "github.com/pkg/errors" |
||
| 11 | ) |
||
| 12 | |||
| 13 | // GetUserFromContext gets the user from the context. |
||
| 14 | func GetUserFromContext(ctx *fiber.Ctx) (domain.User, error) { |
||
| 15 | if ctx.Locals("user") == nil { |
||
| 16 | log.WithContext(ctx.UserContext()).Error("User not found in context") |
||
| 17 | return domain.User{}, errors.New("User is not initialized") |
||
| 18 | } |
||
| 19 | return ctx.Locals("user").(domain.User), nil |
||
| 20 | } |
||
| 21 | |||
| 22 | // SetUserToContext sets the user to the context. |
||
| 23 | func SetUserToContext(ctx *fiber.Ctx, user domain.User) { |
||
| 24 | ctx.Locals("user", user) |
||
| 25 | } |
||
| 26 | |||
| 27 | type JSONHelperSingleton struct { |
||
| 28 | jsonHelper json.Helper |
||
| 29 | } |
||
| 30 | |||
| 31 | var ( |
||
| 32 | instance *JSONHelperSingleton //nolint:gochecknoglobals //Singleton |
||
| 33 | once sync.Once //nolint:gochecknoglobals //Singleton |
||
| 34 | ) |
||
| 35 | |||
| 36 | func GetJSONHelperInstance() *JSONHelperSingleton { |
||
| 37 | once.Do(func() { |
||
| 38 | instance = &JSONHelperSingleton{ |
||
| 39 | jsonHelper: json.NewJSON(&json.SonicJSON{}), |
||
| 40 | } |
||
| 41 | }) |
||
| 42 | return instance |
||
| 43 | } |
||
| 44 | |||
| 45 | func (j *JSONHelperSingleton) GetJSONHelper() json.Helper { |
||
| 46 | return j.jsonHelper |
||
| 47 | } |
||
| 48 | |||
| 49 | func (j *JSONHelperSingleton) SetJSONHelper(helper json.Helper) { |
||
| 50 | j.jsonHelper = json.NewJSON(helper) |
||
| 51 | } |
||
| 52 |