config.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. Gin Gin
  38. Logger Logger
  39. Redis Redis
  40. Cache Cache
  41. Oss Oss
  42. Jwt Jwt
  43. Rustdesk Rustdesk
  44. Proxy Proxy
  45. Ldap Ldap
  46. }
  47. func (a *Admin) Init() {
  48. if a.IdServerPort == 0 {
  49. a.IdServerPort = DefaultIdServerPort
  50. }
  51. if a.RelayServerPort == 0 {
  52. a.RelayServerPort = DefaultRelayServerPort
  53. }
  54. }
  55. // Init 初始化配置
  56. func Init(rowVal *Config, path string) *viper.Viper {
  57. if path == "" {
  58. path = DefaultConfig
  59. }
  60. v := viper.GetViper()
  61. v.AutomaticEnv()
  62. v.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
  63. v.SetEnvPrefix("RUSTDESK_API")
  64. v.SetConfigFile(path)
  65. v.SetConfigType("yaml")
  66. err := v.ReadInConfig()
  67. if err != nil {
  68. panic(fmt.Errorf("Fatal error config file: %s \n", err))
  69. }
  70. /*
  71. v.WatchConfig()
  72. //监听配置修改没什么必要
  73. v.OnConfigChange(func(e fsnotify.Event) {
  74. //配置文件修改监听
  75. fmt.Println("config file changed:", e.Name)
  76. if err2 := v.Unmarshal(rowVal); err2 != nil {
  77. fmt.Println(err2)
  78. }
  79. rowVal.Rustdesk.LoadKeyFile()
  80. rowVal.Rustdesk.ParsePort()
  81. })
  82. */
  83. if err := v.Unmarshal(rowVal); err != nil {
  84. panic(fmt.Errorf("Fatal error config: %s \n", err))
  85. }
  86. rowVal.Rustdesk.LoadKeyFile()
  87. rowVal.Admin.Init()
  88. return v
  89. }
  90. // ReadEnv 读取环境变量
  91. func ReadEnv(rowVal interface{}) *viper.Viper {
  92. v := viper.New()
  93. v.AutomaticEnv()
  94. if err := v.Unmarshal(rowVal); err != nil {
  95. fmt.Println(err)
  96. }
  97. return v
  98. }