1
0

init_user.go 575 B

12345678910111213141516171819202122232425262728
  1. package user
  2. import (
  3. "github.com/0xJacky/Nginx-UI/model"
  4. "github.com/gin-gonic/gin"
  5. "github.com/uozi-tech/cosy"
  6. )
  7. // GetInitUser get the init user from database with caching
  8. func GetInitUser(c *gin.Context) *model.User {
  9. const initUserID = 1
  10. // Try to get from cache first
  11. if cachedUser, found := GetCachedUser(initUserID); found {
  12. return cachedUser
  13. }
  14. // If not in cache, get from database
  15. db := cosy.UseDB(c)
  16. user := &model.User{}
  17. db.First(user, initUserID)
  18. // Cache the user for future requests
  19. if user.ID != 0 {
  20. CacheUser(user)
  21. }
  22. return user
  23. }