config.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package config
  2. import (
  3. "fmt"
  4. "github.com/spf13/viper"
  5. "strings"
  6. "time"
  7. )
  8. const (
  9. DebugMode = "debug"
  10. ReleaseMode = "release"
  11. DefaultConfig = "conf/config.yaml"
  12. )
  13. type App struct {
  14. WebClient int `mapstructure:"web-client"`
  15. Register bool `mapstructure:"register"`
  16. RegisterStatus int `mapstructure:"register-status"`
  17. ShowSwagger int `mapstructure:"show-swagger"`
  18. TokenExpire time.Duration `mapstructure:"token-expire"`
  19. WebSso bool `mapstructure:"web-sso"`
  20. DisablePwdLogin bool `mapstructure:"disable-pwd-login"`
  21. CaptchaThreshold int `mapstructure:"captcha-threshold"`
  22. BanThreshold int `mapstructure:"ban-threshold"`
  23. }
  24. type Admin struct {
  25. Title string `mapstructure:"title"`
  26. Hello string `mapstructure:"hello"`
  27. HelloFile string `mapstructure:"hello-file"`
  28. IdServerPort int `mapstructure:"id-server-port"`
  29. RelayServerPort int `mapstructure:"relay-server-port"`
  30. }
  31. type Config struct {
  32. Lang string `mapstructure:"lang"`
  33. App App
  34. Admin Admin
  35. Gorm Gorm
  36. Mysql Mysql
  37. Postgresql Postgresql
  38. Gin Gin
  39. Logger Logger
  40. Redis Redis
  41. Cache Cache
  42. Oss Oss
  43. Jwt Jwt
  44. Rustdesk Rustdesk
  45. Proxy Proxy
  46. Ldap Ldap
  47. }
  48. func (a *Admin) Init() {
  49. if a.IdServerPort == 0 {
  50. a.IdServerPort = DefaultIdServerPort
  51. }
  52. if a.RelayServerPort == 0 {
  53. a.RelayServerPort = DefaultRelayServerPort
  54. }
  55. }
  56. // Init 初始化配置
  57. func Init(rowVal *Config, path string) *viper.Viper {
  58. if path == "" {
  59. path = DefaultConfig
  60. }
  61. v := viper.GetViper()
  62. v.AutomaticEnv()
  63. v.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
  64. v.SetEnvPrefix("RUSTDESK_API")
  65. v.SetConfigFile(path)
  66. v.SetConfigType("yaml")
  67. err := v.ReadInConfig()
  68. if err != nil {
  69. panic(fmt.Errorf("Fatal error config file: %s \n", err))
  70. }
  71. /*
  72. v.WatchConfig()
  73. //监听配置修改没什么必要
  74. v.OnConfigChange(func(e fsnotify.Event) {
  75. //配置文件修改监听
  76. fmt.Println("config file changed:", e.Name)
  77. if err2 := v.Unmarshal(rowVal); err2 != nil {
  78. fmt.Println(err2)
  79. }
  80. rowVal.Rustdesk.LoadKeyFile()
  81. rowVal.Rustdesk.ParsePort()
  82. })
  83. */
  84. if err := v.Unmarshal(rowVal); err != nil {
  85. panic(fmt.Errorf("Fatal error config: %s \n", err))
  86. }
  87. rowVal.Rustdesk.LoadKeyFile()
  88. rowVal.Admin.Init()
  89. return v
  90. }
  91. // ReadEnv 读取环境变量
  92. func ReadEnv(rowVal interface{}) *viper.Viper {
  93. v := viper.New()
  94. v.AutomaticEnv()
  95. if err := v.Unmarshal(rowVal); err != nil {
  96. fmt.Println(err)
  97. }
  98. return v
  99. }