init.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package init
  2. import (
  3. util "app.yhyue.com/moapp/jybase/common"
  4. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/entity"
  5. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/internal/config"
  6. "context"
  7. "flag"
  8. "fmt"
  9. "regexp"
  10. "time"
  11. "app.yhyue.com/moapp/jypkg/middleground"
  12. _ "github.com/go-sql-driver/mysql"
  13. "github.com/zeromicro/go-zero/core/conf"
  14. )
  15. var (
  16. configFile = flag.String("cf", "etc/bxcore.yaml", "the config file")
  17. C config.Config
  18. dbFile = flag.String("df", "etc/db.yaml", "the db file")
  19. DB config.Db
  20. logFile = flag.String("lf", "etc/logs.yaml", "the logs file")
  21. logc entity.Logc
  22. propertyFile = flag.String("pf", "etc/property.yaml", "the logs file")
  23. Property map[string][]string
  24. SearchLimitKey = "jy_limitSearchText_new" // 全文或附件搜索限制
  25. SearchLimitKeyNoLogin = "jy_limitSearchText_noLogin" //未登录
  26. Middleground *middleground.Middleground
  27. Search_Thread chan bool
  28. ReqLimitInit *ReqLimit
  29. )
  30. type ReqLimit struct {
  31. DoPool chan struct{}
  32. WaitPool chan struct{}
  33. }
  34. // -2 等待池已满
  35. // -1 超时
  36. // 1:可以执行查询
  37. func (r *ReqLimit) Limit(ctx context.Context) int {
  38. ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
  39. defer cancel()
  40. select {
  41. case <-r.WaitPool:
  42. defer func() {
  43. r.WaitPool <- struct{}{}
  44. }()
  45. select {
  46. case <-r.DoPool:
  47. return 1
  48. case <-ctx.Done(): //超时
  49. return -1
  50. }
  51. default:
  52. return -2
  53. }
  54. }
  55. func (r *ReqLimit) Release() {
  56. r.DoPool <- struct{}{}
  57. }
  58. func init() {
  59. //基本配置
  60. conf.MustLoad(*configFile, &C)
  61. util.ReadConfig("etc/property.json", &Property)
  62. //数据库配置
  63. conf.MustLoad(*dbFile, &DB)
  64. //初始mongodb
  65. MongoDBInit(&DB.Mongo)
  66. //初始化msyql
  67. MysqlInit(&DB.Mysql)
  68. //初始redis
  69. RedisInit(&DB.Redis)
  70. //初始es
  71. EsInit(&DB.Es)
  72. NoLoginEsInit(&DB.EsNoLogin)
  73. EsFreeInit(&DB.EsFree)
  74. Search_Thread = make(chan bool, C.SearchConcurrency)
  75. NoLoginPoolInit(C.NoLoginSearch.Switch, C.NoLoginSearch.ExecutionNum, C.NoLoginSearch.Wait)
  76. //初始化标签
  77. LabelInit()
  78. AreaInit()
  79. //
  80. SearchLimitKey = fmt.Sprintf(C.LimitSearchText.LimitKey, "jy_limitSearchText")
  81. //初始化中台相关
  82. Middleground = middleground.NewMiddleground(C.Middleground.Etcd.Hosts).
  83. RegUserCenter(C.Middleground.UserCenterKey).
  84. RegPowerCheckCenter(C.Middleground.PowerCheckCenterKey).
  85. RegResourceCenter(C.Middleground.ResourceCenterKey)
  86. //正则 winner
  87. if C.SearchWinner.RegWinner != "" {
  88. entity.RegWinner = regexp.MustCompile(C.SearchWinner.RegWinner)
  89. }
  90. }