config.go 4.3 KB

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