config.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. Logrus Logrus `mapstructure:"logrus" json:"logrus" yaml:"logrus"`
  33. }
  34. type System struct {
  35. Env string `mapstructure:"env" json:"env" yaml:"env"` // 环境值
  36. Addr int `mapstructure:"addr" json:"addr" yaml:"addr"` // 端口值
  37. DbType string `mapstructure:"db-type" json:"db-type" yaml:"db-type"` // 数据库类型:mysql(默认)|sqlite|sqlserver|postgresql
  38. OssType string `mapstructure:"oss-type" json:"oss-type" yaml:"oss-type"` // Oss类型
  39. UseMultipoint bool `mapstructure:"use-multipoint" json:"use-multipoint" yaml:"use-multipoint"` // 多点登录拦截
  40. UseRedis bool `mapstructure:"use-redis" json:"use-redis" yaml:"use-redis"` // 使用redis
  41. LimitCountIP int `mapstructure:"iplimit-count" json:"iplimit-count" yaml:"iplimit-count"`
  42. LimitTimeIP int `mapstructure:"iplimit-time" json:"iplimit-time" yaml:"iplimit-time"`
  43. RouterPrefix string `mapstructure:"router-prefix" json:"router-prefix" yaml:"router-prefix"`
  44. }
  45. type JWT struct {
  46. SigningKey string `mapstructure:"signing-key" json:"signing-key" yaml:"signing-key"` // jwt签名
  47. ExpiresTime string `mapstructure:"expires-time" json:"expires-time" yaml:"expires-time"` // 过期时间
  48. BufferTime string `mapstructure:"buffer-time" json:"buffer-time" yaml:"buffer-time"` // 缓冲时间
  49. Issuer string `mapstructure:"issuer" json:"issuer" yaml:"issuer"` // 签发者
  50. }
  51. type Captcha struct {
  52. KeyLong int `mapstructure:"key-long" json:"key-long" yaml:"key-long"` // 验证码长度
  53. ImgWidth int `mapstructure:"img-width" json:"img-width" yaml:"img-width"` // 验证码宽度
  54. ImgHeight int `mapstructure:"img-height" json:"img-height" yaml:"img-height"` // 验证码高度
  55. OpenCaptcha int `mapstructure:"open-captcha" json:"open-captcha" yaml:"open-captcha"` // 防爆破验证码开启此数,0代表每次登录都需要验证码,其他数字代表错误密码此数,如3代表错误三次后出现验证码
  56. OpenCaptchaTimeOut int `mapstructure:"open-captcha-timeout" json:"open-captcha-timeout" yaml:"open-captcha-timeout"` // 防爆破验证码超时时间,单位:s(秒)
  57. }
  58. type CORS struct {
  59. Mode string `mapstructure:"mode" json:"mode" yaml:"mode"`
  60. Whitelist []CORSWhitelist `mapstructure:"whitelist" json:"whitelist" yaml:"whitelist"`
  61. }
  62. type CORSWhitelist struct {
  63. AllowOrigin string `mapstructure:"allow-origin" json:"allow-origin" yaml:"allow-origin"`
  64. AllowMethods string `mapstructure:"allow-methods" json:"allow-methods" yaml:"allow-methods"`
  65. AllowHeaders string `mapstructure:"allow-headers" json:"allow-headers" yaml:"allow-headers"`
  66. ExposeHeaders string `mapstructure:"expose-headers" json:"expose-headers" yaml:"expose-headers"`
  67. AllowCredentials bool `mapstructure:"allow-credentials" json:"allow-credentials" yaml:"allow-credentials"`
  68. }
  69. type Mysql struct {
  70. GeneralDB `yaml:",inline" mapstructure:",squash"`
  71. }
  72. func (m *Mysql) Dsn() string {
  73. return m.Username + ":" + m.Password + "@tcp(" + m.Path + ":" + m.Port + ")/" + m.Dbname + "?" + m.Config
  74. }
  75. func (m *Mysql) GetLogMode() string {
  76. return m.LogMode
  77. }
  78. type GeneralDB struct {
  79. Path string `mapstructure:"path" json:"path" yaml:"path"` // 服务器地址:端口
  80. Port string `mapstructure:"port" json:"port" yaml:"port"` //:端口
  81. Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置
  82. Dbname string `mapstructure:"db-name" json:"db-name" yaml:"db-name"` // 数据库名
  83. Username string `mapstructure:"username" json:"username" yaml:"username"` // 数据库用户名
  84. Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码
  85. Prefix string `mapstructure:"prefix" json:"prefix" yaml:"prefix"` //全局表前缀,单独定义TableName则不生效
  86. Singular bool `mapstructure:"singular" json:"singular" yaml:"singular"` //是否开启全局禁用复数,true表示开启
  87. Engine string `mapstructure:"engine" json:"engine" yaml:"engine" default:"InnoDB"` //数据库引擎,默认InnoDB
  88. MaxIdleConns int `mapstructure:"max-idle-conns" json:"max-idle-conns" yaml:"max-idle-conns"` // 空闲中的最大连接数
  89. MaxOpenConns int `mapstructure:"max-open-conns" json:"max-open-conns" yaml:"max-open-conns"` // 打开到数据库的最大连接数
  90. LogMode string `mapstructure:"log-mode" json:"log-mode" yaml:"log-mode"` // 是否开启Gorm全局日志
  91. LogZap bool `mapstructure:"log-zap" json:"log-zap" yaml:"log-zap"` // 是否通过zap写入日志文件
  92. }
  93. type Redis struct {
  94. DB int `mapstructure:"db" json:"db" yaml:"db"` // redis的哪个数据库
  95. Addr string `mapstructure:"addr" json:"addr" yaml:"addr"` // 服务器地址:端口
  96. Password string `mapstructure:"password" json:"password" yaml:"password"` // 密码
  97. }
  98. type Logrus struct {
  99. StdOut bool `ymal:"stdout"`
  100. MaxAge int64 `yaml:"max-age"`
  101. RotationTime int `yaml:"rotation-time"`
  102. }