config.go 8.4 KB

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