123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- package config
- import (
- "github.com/sirupsen/logrus"
- "gopkg.in/yaml.v3"
- "io/ioutil"
- "os"
- )
- var Config = initConfig()
- func initConfig() config {
- var c config
- open, err := os.Open("./config.yaml")
- if err != nil {
- logrus.Fatal("打开文件读取错误", err)
- }
- data, err := ioutil.ReadAll(open)
- if err != nil {
- logrus.Fatal("读取配置文件错误", err)
- }
- if yaml.Unmarshal(data, &c) != nil {
- logrus.Fatal("解析配置文件错误", err)
- }
- logrus.Info("配置文件读取成功:%+v", c)
- return c
- }
- type config struct {
- System System `mapstructure:"system" json:"system" yaml:"system"`
- JWT JWT `mapstructure:"jwt" json:"jwt" yaml:"jwt"`
- Captcha Captcha `mapstructure:"captcha" json:"captcha" yaml:"captcha"`
- Cors CORS `mapstructure:"cors" json:"cors" yaml:"cors"`
- Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"`
- Redis Redis `mapstructure:"redis" json:"redis" yaml:"redis"`
- HttpHostNotificationList HttpHostNotificationList `mapstructure:"HttpHostNotificationList" yaml:"HttpHostNotificationList" xml:"HttpHostNotificationList"`
- Foreign Foreign `mapstructure:"foreign" yaml:"foreign" json:"foreign"`
- Hikvision Hikvision `mapstructure:"hikvision" yaml:"hikvision" json:"hikvision"`
- Metering Metering `mapstructure:"metering" yaml:"metering" json:"metering"`
- Dump Dump `mapstructure:"dump" yaml:"dump" json:"dump"`
- }
- type System struct {
- Env string `mapstructure:"env" json:"env" yaml:"env"` // 环境值
- Addr int `mapstructure:"addr" json:"addr" yaml:"addr"` // 端口值
- DbType string `mapstructure:"db-type" json:"db-type" yaml:"db-type"` // 数据库类型:mysql(默认)|sqlite|sqlserver|postgresql
- OssType string `mapstructure:"oss-type" json:"oss-type" yaml:"oss-type"` // Oss类型
- UseMultipoint bool `mapstructure:"use-multipoint" json:"use-multipoint" yaml:"use-multipoint"` // 多点登录拦截
- UseRedis bool `mapstructure:"use-redis" json:"use-redis" yaml:"use-redis"` // 使用redis
- LimitCountIP int `mapstructure:"iplimit-count" json:"iplimit-count" yaml:"iplimit-count"`
- LimitTimeIP int `mapstructure:"iplimit-time" json:"iplimit-time" yaml:"iplimit-time"`
- RouterPrefix string `mapstructure:"router-prefix" json:"router-prefix" yaml:"router-prefix"`
- }
- type JWT struct {
- SigningKey string `mapstructure:"signing-key" json:"signing-key" yaml:"signing-key"` // jwt签名
- ExpiresTime string `mapstructure:"expires-time" json:"expires-time" yaml:"expires-time"` // 过期时间
- BufferTime string `mapstructure:"buffer-time" json:"buffer-time" yaml:"buffer-time"` // 缓冲时间
- Issuer string `mapstructure:"issuer" json:"issuer" yaml:"issuer"` // 签发者
- }
- type Captcha struct {
- KeyLong int `mapstructure:"key-long" json:"key-long" yaml:"key-long"` // 验证码长度
- ImgWidth int `mapstructure:"img-width" json:"img-width" yaml:"img-width"` // 验证码宽度
- ImgHeight int `mapstructure:"img-height" json:"img-height" yaml:"img-height"` // 验证码高度
- OpenCaptcha int `mapstructure:"open-captcha" json:"open-captcha" yaml:"open-captcha"` // 防爆破验证码开启此数,0代表每次登录都需要验证码,其他数字代表错误密码此数,如3代表错误三次后出现验证码
- OpenCaptchaTimeOut int `mapstructure:"open-captcha-timeout" json:"open-captcha-timeout" yaml:"open-captcha-timeout"` // 防爆破验证码超时时间,单位:s(秒)
- }
- type CORS struct {
- Mode string `mapstructure:"mode" json:"mode" yaml:"mode"`
- Whitelist []CORSWhitelist `mapstructure:"whitelist" json:"whitelist" yaml:"whitelist"`
- }
- type CORSWhitelist struct {
- AllowOrigin string `mapstructure:"allow-origin" json:"allow-origin" yaml:"allow-origin"`
- AllowMethods string `mapstructure:"allow-methods" json:"allow-methods" yaml:"allow-methods"`
- AllowHeaders string `mapstructure:"allow-headers" json:"allow-headers" yaml:"allow-headers"`
- ExposeHeaders string `mapstructure:"expose-headers" json:"expose-headers" yaml:"expose-headers"`
- AllowCredentials bool `mapstructure:"allow-credentials" json:"allow-credentials" yaml:"allow-credentials"`
- }
- type Mysql struct {
- GeneralDB `yaml:",inline" mapstructure:",squash"`
- }
- func (m *Mysql) Dsn() string {
- return m.Username + ":" + m.Password + "@tcp(" + m.Path + ":" + m.Port + ")/" + m.Dbname + "?" + m.Config
- }
- func (m *Mysql) GetLogMode() string {
- return m.LogMode
- }
- type GeneralDB struct {
- Path string `mapstructure:"path" json:"path" yaml:"path"` // 服务器地址:端口
- Port string `mapstructure:"port" json:"port" yaml:"port"` //:端口
- Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置
- Dbname string `mapstructure:"db-name" json:"db-name" yaml:"db-name"` // 数据库名
- Username string `mapstructure:"username" json:"username" yaml:"username"` // 数据库用户名
- Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码
- Prefix string `mapstructure:"prefix" json:"prefix" yaml:"prefix"` //全局表前缀,单独定义TableName则不生效
- Singular bool `mapstructure:"singular" json:"singular" yaml:"singular"` //是否开启全局禁用复数,true表示开启
- Engine string `mapstructure:"engine" json:"engine" yaml:"engine" default:"InnoDB"` //数据库引擎,默认InnoDB
- MaxIdleConns int `mapstructure:"max-idle-conns" json:"max-idle-conns" yaml:"max-idle-conns"` // 空闲中的最大连接数
- MaxOpenConns int `mapstructure:"max-open-conns" json:"max-open-conns" yaml:"max-open-conns"` // 打开到数据库的最大连接数
- LogMode string `mapstructure:"log-mode" json:"log-mode" yaml:"log-mode"` // 是否开启Gorm全局日志
- LogZap bool `mapstructure:"log-zap" json:"log-zap" yaml:"log-zap"` // 是否通过zap写入日志文件
- }
- type Redis struct {
- DB int `mapstructure:"db" json:"db" yaml:"db"` // redis的哪个数据库
- Addr string `mapstructure:"addr" json:"addr" yaml:"addr"` // 服务器地址:端口
- Password string `mapstructure:"password" json:"password" yaml:"password"` // 密码
- }
- // 事件监听服务器列表
- type HttpHostNotificationList struct {
- HttpHostNotification []HttpHostNotification `yaml:"HttpHostNotification"`
- }
- type HttpHostNotification struct {
- ID string `xml:"id" yaml:"id"`
- URL string `xml:"url" yaml:"url"` //路径
- ProtocolType string `xml:"protocolType" yaml:"protocolType"` //协议类型 HTTP
- ParameterFormatType string `xml:"parameterFormatType" yaml:"parameterFormatType"` //数据格式 XML
- AddressingFormatType string `xml:"addressingFormatType" yaml:"addressingFormatType"` //地址格式 ipaddress
- Ipaddress string `xml:"ipAddress" yaml:"ipaddress"` //服务器ip地址
- PortNo int `xml:"portNo" yaml:"portNo"` //端口
- HttpBroken string `xml:"httpBroken" yaml:"httpBroken"` //true
- HttpAuthenticationMethod string `xml:"httpAuthenticationMethod" yaml:"httpAuthenticationMethod"` //none
- }
- type Foreign struct {
- SecurityRewindUrl string `mapstructure:"securityRewindUrl" yaml:"securityRewindUrl"`
- GatewayServer string `mapstructure:"getawayServer" yaml:"gatewayServer"`
- }
- type Hikvision struct {
- User string `yaml:"user"`
- Password string `yaml:"password"`
- StreamBaseUrl string `mapstructure:"streamBaseUrl" yaml:"streamBaseUrl"`
- }
- type Metering struct {
- Camera1 struct {
- A []int `yaml:"a"`
- B []int `yaml:"b"`
- } `yaml:"camera1"`
- Camera2 struct {
- A []int `yaml:"a"`
- } `yaml:"camera2"`
- }
- type Dump struct {
- A []float64 `yaml:"a"`
- B []float64 `yaml:"b"`
- C []float64 `yaml:"c"`
- D []float64 `yaml:"d"`
- E []float64 `yaml:"e"`
- F []float64 `yaml:"f"`
- G []float64 `yaml:"g"`
- H []float64 `yaml:"h"`
- }
|