diff --git a/cmd/apimain.go b/cmd/apimain.go index 2ba29f2..21cc313 100644 --- a/cmd/apimain.go +++ b/cmd/apimain.go @@ -33,9 +33,7 @@ import ( // @name Authorization func main() { //配置解析 - global.Viper = config.Init(&global.Config, func() { - fmt.Println(global.Config) - }) + global.Viper = config.Init(&global.Config) //日志 global.Logger = logger.New(&logger.Config{ diff --git a/conf/config.yaml b/conf/config.yaml index 2c52163..976bee4 100644 --- a/conf/config.yaml +++ b/conf/config.yaml @@ -7,15 +7,15 @@ gorm: max-idle-conns: 10 max-open-conns: 100 mysql: - username: "root" - password: "111111" - addr: "192.168.1.66:3308" - dbname: "rustdesk2" + username: "" + password: "" + addr: "" + dbname: "" rustdesk: - id-server: "124.220.134.240:21116" - relay-server: "124.220.134.240:21117" - api-server: "http://127.0.0.1:21114" - key: "ljw19891989" + id-server: "192.168.1.66:21116" + relay-server: "192.168.1.66:21117" + api-server: "http://192.168.1.66:21114" + key: "123456789" redis: addr: "127.0.0.1:6379" password: "" @@ -28,7 +28,7 @@ cache: type: "file" file-dir: "./runtime/cache" redis-addr: "127.0.0.1:6379" - redis-pwd: "ljw19891989" + redis-pwd: "" redis-db: 0 oss: access-key-id: "" diff --git a/config/config.go b/config/config.go index 7c789ff..af86a85 100644 --- a/config/config.go +++ b/config/config.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/fsnotify/fsnotify" "github.com/spf13/viper" + "strings" ) const ( @@ -26,7 +27,7 @@ type Config struct { } // Init 初始化配置 -func Init(rowVal interface{}, cb func()) *viper.Viper { +func Init(rowVal interface{}) *viper.Viper { var config string flag.StringVar(&config, "c", "", "choose config file.") flag.Parse() @@ -34,6 +35,9 @@ func Init(rowVal interface{}, cb func()) *viper.Viper { config = DefaultConfig } v := viper.New() + v.AutomaticEnv() + v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + v.SetEnvPrefix("RUSTDESK_API") v.SetConfigFile(config) v.SetConfigType("yaml") err := v.ReadInConfig() @@ -47,10 +51,19 @@ func Init(rowVal interface{}, cb func()) *viper.Viper { if err2 := v.Unmarshal(rowVal); err2 != nil { fmt.Println(err2) } - cb() }) if err := v.Unmarshal(rowVal); err != nil { fmt.Println(err) } return v } + +// ReadEnv 读取环境变量 +func ReadEnv(rowVal interface{}) *viper.Viper { + v := viper.New() + v.AutomaticEnv() + if err := v.Unmarshal(rowVal); err != nil { + fmt.Println(err) + } + return v +}