initialize.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package storage
  2. import (
  3. "log"
  4. "gogs.baozhida.cn/zoie/OAuth-core/pkg/captcha"
  5. "gogs.baozhida.cn/zoie/OAuth-core/sdk"
  6. "gogs.baozhida.cn/zoie/OAuth-core/sdk/config"
  7. )
  8. // Setup 配置storage组件
  9. func Setup() {
  10. //4. 设置缓存
  11. cacheAdapter, err := config.CacheConfig.Setup()
  12. if err != nil {
  13. log.Fatalf("cache setup error, %s\n", err.Error())
  14. }
  15. sdk.Runtime.SetCacheAdapter(cacheAdapter)
  16. //5. 设置验证码store
  17. captcha.SetStore(captcha.NewCacheStore(cacheAdapter, 600))
  18. //6. 设置队列
  19. if !config.QueueConfig.Empty() {
  20. if q := sdk.Runtime.GetQueueAdapter(); q != nil {
  21. q.Shutdown()
  22. }
  23. queueAdapter, err := config.QueueConfig.Setup()
  24. if err != nil {
  25. log.Fatalf("queue setup error, %s\n", err.Error())
  26. }
  27. sdk.Runtime.SetQueueAdapter(queueAdapter)
  28. defer func() {
  29. go queueAdapter.Run()
  30. }()
  31. }
  32. //7. 设置分布式锁
  33. if !config.LockerConfig.Empty() {
  34. lockerAdapter, err := config.LockerConfig.Setup()
  35. if err != nil {
  36. log.Fatalf("locker setup error, %s\n", err.Error())
  37. }
  38. sdk.Runtime.SetLockerAdapter(lockerAdapter)
  39. }
  40. }