config.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package config
  2. import (
  3. "github.com/sirupsen/logrus"
  4. "gopkg.in/yaml.v3"
  5. "io/ioutil"
  6. "os"
  7. )
  8. var Config = initConfig()
  9. func initConfig() config {
  10. var c config
  11. open, err := os.Open("./config.yaml")
  12. if err != nil {
  13. logrus.Fatal("打开文件读取错误", err)
  14. }
  15. data, err := ioutil.ReadAll(open)
  16. if err != nil {
  17. logrus.Fatal("读取配置文件错误", err)
  18. }
  19. if yaml.Unmarshal(data, &c) != nil {
  20. logrus.Fatal("解析配置文件错误", err)
  21. }
  22. logrus.Info("配置文件读取成功:%+v", c)
  23. return c
  24. }
  25. type config struct {
  26. System System `mapstructure:"system" json:"system" yaml:"system"`
  27. JWT JWT `mapstructure:"jwt" json:"jwt" yaml:"jwt"`
  28. Captcha Captcha `mapstructure:"captcha" json:"captcha" yaml:"captcha"`
  29. Cors CORS `mapstructure:"cors" json:"cors" yaml:"cors"`
  30. Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"`
  31. Redis Redis `mapstructure:"redis" json:"redis" yaml:"redis"`
  32. HttpHostNotificationList HttpHostNotificationList `mapstructure:"HttpHostNotificationList" yaml:"HttpHostNotificationList" xml:"HttpHostNotificationList"`
  33. Foreign Foreign `mapstructure:"foreign" yaml:"foreign" json:"foreign"`
  34. Hikvision Hikvision `mapstructure:"hikvision" yaml:"hikvision" json:"hikvision"`
  35. Metering Metering `mapstructure:"metering" yaml:"metering" json:"metering"`
  36. Dump [][]float64 `mapstructure:"dump" yaml:"dump" json:"dump"`
  37. }
  38. type System struct {
  39. Env string `mapstructure:"env" json:"env" yaml:"env"` // 环境值
  40. Addr int `mapstructure:"addr" json:"addr" yaml:"addr"` // 端口值
  41. DbType string `mapstructure:"db-type" json:"db-type" yaml:"db-type"` // 数据库类型:mysql(默认)|sqlite|sqlserver|postgresql
  42. OssType string `mapstructure:"oss-type" json:"oss-type" yaml:"oss-type"` // Oss类型
  43. UseMultipoint bool `mapstructure:"use-multipoint" json:"use-multipoint" yaml:"use-multipoint"` // 多点登录拦截
  44. UseRedis bool `mapstructure:"use-redis" json:"use-redis" yaml:"use-redis"` // 使用redis
  45. LimitCountIP int `mapstructure:"iplimit-count" json:"iplimit-count" yaml:"iplimit-count"`
  46. LimitTimeIP int `mapstructure:"iplimit-time" json:"iplimit-time" yaml:"iplimit-time"`
  47. RouterPrefix string `mapstructure:"router-prefix" json:"router-prefix" yaml:"router-prefix"`
  48. }
  49. type JWT struct {
  50. SigningKey string `mapstructure:"signing-key" json:"signing-key" yaml:"signing-key"` // jwt签名
  51. ExpiresTime string `mapstructure:"expires-time" json:"expires-time" yaml:"expires-time"` // 过期时间
  52. BufferTime string `mapstructure:"buffer-time" json:"buffer-time" yaml:"buffer-time"` // 缓冲时间
  53. Issuer string `mapstructure:"issuer" json:"issuer" yaml:"issuer"` // 签发者
  54. }
  55. type Captcha struct {
  56. KeyLong int `mapstructure:"key-long" json:"key-long" yaml:"key-long"` // 验证码长度
  57. ImgWidth int `mapstructure:"img-width" json:"img-width" yaml:"img-width"` // 验证码宽度
  58. ImgHeight int `mapstructure:"img-height" json:"img-height" yaml:"img-height"` // 验证码高度
  59. OpenCaptcha int `mapstructure:"open-captcha" json:"open-captcha" yaml:"open-captcha"` // 防爆破验证码开启此数,0代表每次登录都需要验证码,其他数字代表错误密码此数,如3代表错误三次后出现验证码
  60. OpenCaptchaTimeOut int `mapstructure:"open-captcha-timeout" json:"open-captcha-timeout" yaml:"open-captcha-timeout"` // 防爆破验证码超时时间,单位:s(秒)
  61. }
  62. type CORS struct {
  63. Mode string `mapstructure:"mode" json:"mode" yaml:"mode"`
  64. Whitelist []CORSWhitelist `mapstructure:"whitelist" json:"whitelist" yaml:"whitelist"`
  65. }
  66. type CORSWhitelist struct {
  67. AllowOrigin string `mapstructure:"allow-origin" json:"allow-origin" yaml:"allow-origin"`
  68. AllowMethods string `mapstructure:"allow-methods" json:"allow-methods" yaml:"allow-methods"`
  69. AllowHeaders string `mapstructure:"allow-headers" json:"allow-headers" yaml:"allow-headers"`
  70. ExposeHeaders string `mapstructure:"expose-headers" json:"expose-headers" yaml:"expose-headers"`
  71. AllowCredentials bool `mapstructure:"allow-credentials" json:"allow-credentials" yaml:"allow-credentials"`
  72. }
  73. type Mysql struct {
  74. GeneralDB `yaml:",inline" mapstructure:",squash"`
  75. }
  76. func (m *Mysql) Dsn() string {
  77. return m.Username + ":" + m.Password + "@tcp(" + m.Path + ":" + m.Port + ")/" + m.Dbname + "?" + m.Config
  78. }
  79. func (m *Mysql) GetLogMode() string {
  80. return m.LogMode
  81. }
  82. type GeneralDB struct {
  83. Path string `mapstructure:"path" json:"path" yaml:"path"` // 服务器地址:端口
  84. Port string `mapstructure:"port" json:"port" yaml:"port"` //:端口
  85. Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置
  86. Dbname string `mapstructure:"db-name" json:"db-name" yaml:"db-name"` // 数据库名
  87. Username string `mapstructure:"username" json:"username" yaml:"username"` // 数据库用户名
  88. Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码
  89. Prefix string `mapstructure:"prefix" json:"prefix" yaml:"prefix"` //全局表前缀,单独定义TableName则不生效
  90. Singular bool `mapstructure:"singular" json:"singular" yaml:"singular"` //是否开启全局禁用复数,true表示开启
  91. Engine string `mapstructure:"engine" json:"engine" yaml:"engine" default:"InnoDB"` //数据库引擎,默认InnoDB
  92. MaxIdleConns int `mapstructure:"max-idle-conns" json:"max-idle-conns" yaml:"max-idle-conns"` // 空闲中的最大连接数
  93. MaxOpenConns int `mapstructure:"max-open-conns" json:"max-open-conns" yaml:"max-open-conns"` // 打开到数据库的最大连接数
  94. LogMode string `mapstructure:"log-mode" json:"log-mode" yaml:"log-mode"` // 是否开启Gorm全局日志
  95. LogZap bool `mapstructure:"log-zap" json:"log-zap" yaml:"log-zap"` // 是否通过zap写入日志文件
  96. }
  97. type Redis struct {
  98. DB int `mapstructure:"db" json:"db" yaml:"db"` // redis的哪个数据库
  99. Addr string `mapstructure:"addr" json:"addr" yaml:"addr"` // 服务器地址:端口
  100. Password string `mapstructure:"password" json:"password" yaml:"password"` // 密码
  101. }
  102. // 事件监听服务器列表
  103. type HttpHostNotificationList struct {
  104. HttpHostNotification []HttpHostNotification `yaml:"HttpHostNotification"`
  105. }
  106. type HttpHostNotification struct {
  107. ID string `xml:"id" yaml:"id"`
  108. URL string `xml:"url" yaml:"url"` //路径
  109. ProtocolType string `xml:"protocolType" yaml:"protocolType"` //协议类型 HTTP
  110. ParameterFormatType string `xml:"parameterFormatType" yaml:"parameterFormatType"` //数据格式 XML
  111. AddressingFormatType string `xml:"addressingFormatType" yaml:"addressingFormatType"` //地址格式 ipaddress
  112. Ipaddress string `xml:"ipAddress" yaml:"ipaddress"` //服务器ip地址
  113. PortNo int `xml:"portNo" yaml:"portNo"` //端口
  114. HttpBroken string `xml:"httpBroken" yaml:"httpBroken"` //true
  115. HttpAuthenticationMethod string `xml:"httpAuthenticationMethod" yaml:"httpAuthenticationMethod"` //none
  116. }
  117. type Foreign struct {
  118. SecurityRewindUrl string `mapstructure:"securityRewindUrl" yaml:"securityRewindUrl"`
  119. GatewayServer string `mapstructure:"getawayServer" yaml:"gatewayServer"`
  120. }
  121. type Hikvision struct {
  122. User string `yaml:"user"`
  123. Password string `yaml:"password"`
  124. StreamBaseUrl string `mapstructure:"streamBaseUrl" yaml:"streamBaseUrl"`
  125. }
  126. type Metering struct {
  127. Camera1 struct {
  128. A []int `yaml:"a"`
  129. B []int `yaml:"b"`
  130. } `yaml:"camera1"`
  131. Camera2 struct {
  132. A []int `yaml:"a"`
  133. } `yaml:"camera2"`
  134. }
  135. type Dump struct {
  136. Dump [][]float64 `yaml:"dump"`
  137. }