Files
rustdesk-api/config/config.go
T

75 lines
1.4 KiB
Go
Raw Permalink Normal View History

2024-09-13 15:57:29 +08:00
package config
import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
2024-09-13 22:09:46 +08:00
"strings"
2024-09-13 15:57:29 +08:00
)
const (
DebugMode = "debug"
ReleaseMode = "release"
DefaultConfig = "conf/config.yaml"
)
2024-10-14 10:43:29 +08:00
type App struct {
2024-10-31 15:14:30 +08:00
WebClient int `mapstructure:"web-client"`
Register bool `mapstructure:"register"`
2024-10-14 10:43:29 +08:00
}
2024-09-13 15:57:29 +08:00
type Config struct {
2024-09-25 22:41:57 +08:00
Lang string `mapstructure:"lang"`
2024-10-14 10:43:29 +08:00
App App
2024-09-13 15:57:29 +08:00
Gorm Gorm
Mysql Mysql
Gin Gin
Logger Logger
Redis Redis
Cache Cache
Oss Oss
Jwt Jwt
Rustdesk Rustdesk
2024-10-19 15:46:45 +08:00
Proxy Proxy
2024-09-13 15:57:29 +08:00
}
// Init 初始化配置
2024-11-08 15:54:49 +08:00
func Init(rowVal interface{}, path string) *viper.Viper {
if path == "" {
path = DefaultConfig
2024-09-13 15:57:29 +08:00
}
2024-11-08 15:54:49 +08:00
v := viper.GetViper()
2024-09-13 22:09:46 +08:00
v.AutomaticEnv()
2024-09-13 22:39:00 +08:00
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
2024-09-13 22:09:46 +08:00
v.SetEnvPrefix("RUSTDESK_API")
2024-11-08 15:54:49 +08:00
v.SetConfigFile(path)
2024-09-13 15:57:29 +08:00
v.SetConfigType("yaml")
err := v.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
v.WatchConfig()
v.OnConfigChange(func(e fsnotify.Event) {
//配置文件修改监听
fmt.Println("config file changed:", e.Name)
if err2 := v.Unmarshal(rowVal); err2 != nil {
fmt.Println(err2)
}
})
if err := v.Unmarshal(rowVal); err != nil {
fmt.Println(err)
}
2024-11-08 15:54:49 +08:00
2024-09-13 15:57:29 +08:00
return v
}
2024-09-13 22:09:46 +08:00
// ReadEnv 读取环境变量
func ReadEnv(rowVal interface{}) *viper.Viper {
v := viper.New()
v.AutomaticEnv()
if err := v.Unmarshal(rowVal); err != nil {
fmt.Println(err)
}
return v
}