config.go 8.2 KB

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