config.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package conf
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/spf13/viper"
  6. )
  7. var (
  8. //错误字符串切片长度
  9. LenStackBuf = 4096
  10. ConsolePrompt = "grave# "
  11. // pprof文件路径
  12. ProfilePath string
  13. )
  14. // Config is a wrapper around a viper config
  15. type Config struct {
  16. *viper.Viper
  17. }
  18. // NewConfig creates a new config with a given viper config if given
  19. func NewConfig(conf *viper.Viper) *Config {
  20. var cfg *viper.Viper
  21. if conf == nil {
  22. cfg = viper.New()
  23. } else {
  24. cfg = conf
  25. }
  26. cfg.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
  27. cfg.AutomaticEnv()
  28. c := &Config{Viper: cfg}
  29. c.fillDefaultValues()
  30. // c.config.AddConfigPath("./")
  31. // c.config.SetConfigFile("gate.json")
  32. // c.config.WriteConfig()
  33. ProfilePath = cfg.GetString("console.profilepath")
  34. return c
  35. }
  36. type ConnAddr struct {
  37. Addr string
  38. Tag int
  39. }
  40. func (c *Config) fillDefaultValues() {
  41. defaultsMap := map[string]interface{}{
  42. "mysql.datasource": "root:123456@tcp(127.0.0.1:3306)/grave?charset=utf8&timeout=10s",
  43. "wx.appid": "wx64db79a6a7bcf3f4",
  44. "wx.appkey": "3e25707fff474fca653621861f0fecc9",
  45. "wx.code2idurl": "https://api.weixin.qq.com/sns/jscode2session",
  46. "qq.appid": "wx64db79a6a7bcf3f4",
  47. "qq.appkey": "3e25707fff474fca653621861f0fecc9",
  48. "qq.code2idurl": "https://api.weixin.qq.com/sns/jscode2session",
  49. "http.listenaddr": ":10108", //
  50. "cluster.id": "123",
  51. "cluster.servertype": "login",
  52. //
  53. "gate.maxconnnum": 20000, //最大连接数
  54. "gate.pendingwritenum": 2000, //
  55. "gate.maxmsglen": 5 * 1024 * 1024, //
  56. "gate.wsaddr": "", // :10110
  57. "gate.httptimeout": "10s", //
  58. "gate.certfile": "", //
  59. "gate.keyfile": "",
  60. "gate.tcpaddr": "", //监听地址 :10111
  61. "gate.lenmsglen": 4, //
  62. "gate.littleendian": false, //
  63. "skeleton.golen": 10000, //
  64. "skeleton.timerdispatcherlen": 10000, //
  65. "skeleton.asyncalllen": 70000, //本机RPC调用
  66. "skeleton.chanrpclen": 70000, //路由消息
  67. "console.profilepath": "", //监测文件名
  68. "console.consoleport": 0,
  69. "console.consoleprompt": "grave# ", //控制台提示字符
  70. "cluster.listenaddr": "", //:10112
  71. "cluster.connaddrs": []ConnAddr{}, //[]ConnAddr{{Tag: 1, Addr: "127.0.0.1:10112"}, {Tag: 2, Addr: "127.0.0.1:10113"}}
  72. "cluster.pendingwritenum": 70000, //发送数据
  73. "cluster.connectinterval": "3s",
  74. "grave.nats.connectiontimeout": "2s",
  75. "grave.nats.maxreconnectionretries": 15,
  76. "grave.nats.requesttimeout": "5s",
  77. "grave.nats.connect": "nats://localhost:4222",
  78. "grave.nats.messages": 75,
  79. "grave.nats.push": 100,
  80. //"grave.nats.connectiontimeout": "2s",
  81. //"grave.nats.maxreconnectionretries": 15,
  82. }
  83. for param := range defaultsMap {
  84. if c.Get(param) == nil {
  85. c.SetDefault(param, defaultsMap[param])
  86. }
  87. }
  88. }
  89. func (c *Config) GetInt64Slice(key string) {
  90. v := c.Get(key)
  91. fmt.Println(v)
  92. // return cast.ToIntSlice(c.Get(key))
  93. }
  94. // // GetDuration returns a duration from the inner config
  95. // func (c *Config) GetDuration(s string) time.Duration {
  96. // return c.GetDuration(s)
  97. // }
  98. // // GetString returns a string from the inner config
  99. // func (c *Config) GetString(s string) string {
  100. // return c.GetString(s)
  101. // }
  102. // // GetInt returns an int from the inner config
  103. // func (c *Config) GetInt(s string) int {
  104. // return c.config.GetInt(s)
  105. // }
  106. // // GetUint32 returns an int from the inner config
  107. // func (c *Config) GetUint32(s string) uint32 {
  108. // return c.config.GetUint32(s)
  109. // }
  110. // // GetBool returns an boolean from the inner config
  111. // func (c *Config) GetBool(s string) bool {
  112. // return c.config.GetBool(s)
  113. // }
  114. // // GetStringSlice returns a string slice from the inner config
  115. // func (c *Config) GetStringSlice(s string) []string {
  116. // return c.config.GetStringSlice(s)
  117. // }
  118. // // Get returns an interface from the inner config
  119. // func (c *Config) Get(s string) interface{} {
  120. // return c.config.Get(s)
  121. // }
  122. // // GetStringMapString returns a string map string from the inner config
  123. // func (c *Config) GetStringMapString(s string) map[string]string {
  124. // return c.config.GetStringMapString(s)
  125. // }
  126. // // UnmarshalKey unmarshals key into v
  127. // func (c *Config) UnmarshalKey(s string, v interface{}) error {
  128. // return c.config.UnmarshalKey(s, v)
  129. // }
  130. // func (c *Config) Sub(s string) *viper.Viper {
  131. // return c.config.Sub(s)
  132. // }