config.go 8.6 KB

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