init.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package init
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. "flag"
  5. "github.com/zeromicro/go-zero/core/conf"
  6. "jyBXCore/api/internal/config"
  7. "log"
  8. "regexp"
  9. "time"
  10. )
  11. var configFile = flag.String("cf", "etc/bxcore-api.yaml", "the config file")
  12. var C config.Config
  13. var dbFile = flag.String("df", "etc/db.yaml", "the db file")
  14. var DB config.Db
  15. var routesFile = flag.String("rf", "etc/routes.yaml", "the routes file")
  16. var Routes config.Routes
  17. var ExcludeUrl = []*regexp.Regexp{}
  18. var HotKeyArrLoginEd, HotKeyArrUnLogin []string
  19. func init() {
  20. log.Println("初始化配置")
  21. //读取基础配置
  22. conf.MustLoad(*configFile, &C)
  23. //读取数据库配置
  24. conf.MustLoad(*dbFile, &DB)
  25. //初始redis
  26. RedisInit(&DB.Redis)
  27. //初始mongodb
  28. MongoDBInit(&DB.Mongo)
  29. //读取路由配置
  30. conf.MustLoad(*routesFile, &Routes)
  31. if len(Routes.ExcludeRoute) > 0 {
  32. for _, v := range Routes.ExcludeRoute {
  33. ExcludeUrl = append(ExcludeUrl, regexp.MustCompile(MC.ObjToString(v)))
  34. }
  35. }
  36. // 每隔5分钟更新一次随机词
  37. go func() {
  38. HotKeyArrLoginEd, HotKeyArrUnLogin = GetIndexHotKey(), getIndexHotKeyNoLoginFunc()
  39. for range time.NewTicker(300 * time.Second).C {
  40. HotKeyArrLoginEd, HotKeyArrUnLogin = GetIndexHotKey(), getIndexHotKeyNoLoginFunc()
  41. }
  42. }()
  43. }
  44. var getIndexHotKeyNoLoginFunc = func() (loginEd []string) {
  45. // 定义一个函数,用于从数组words中随机取出几个词
  46. m := map[string]bool{}
  47. for _, v := range C.MobileIndexHotKeyUnLogin {
  48. m[v] = true
  49. }
  50. for key, _ := range m {
  51. loginEd = append(loginEd, key)
  52. if len(loginEd) == int(C.MobileIndexHotKeyLimit) {
  53. break
  54. }
  55. }
  56. return
  57. }
  58. func GetIndexHotKey() (loginEd []string) {
  59. // 定义一个函数,用于从数组words中随机取出几个词
  60. m := map[string]bool{}
  61. for _, v := range C.MobileIndexHotKey {
  62. m[v] = true
  63. }
  64. for key, _ := range m {
  65. loginEd = append(loginEd, key)
  66. if len(loginEd) == int(C.MobileIndexHotKeyLimit) {
  67. break
  68. }
  69. }
  70. return
  71. }