nezha/model/config.go

48 lines
751 B
Go
Raw Normal View History

2019-12-08 03:59:58 -05:00
package model
import (
2019-12-20 10:58:09 -05:00
"fmt"
2019-12-08 03:59:58 -05:00
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
// Config ..
type Config struct {
Debug bool
Site struct {
Brand string // 站点名称
CookieName string // 浏览器 Cookie 名称
}
GitHub struct {
2020-03-22 10:28:25 -04:00
Admin []int64 // 管理员ID列表
2019-12-08 03:59:58 -05:00
ClientID string
ClientSecret string
}
2019-12-20 10:58:09 -05:00
v *viper.Viper
2019-12-08 03:59:58 -05:00
}
// ReadInConfig ..
2019-12-20 10:58:09 -05:00
func (c *Config) Read(path string) error {
c.v = viper.New()
c.v.SetConfigFile(path)
err := c.v.ReadInConfig()
2019-12-08 03:59:58 -05:00
if err != nil {
2019-12-20 10:58:09 -05:00
return err
2019-12-08 03:59:58 -05:00
}
2019-12-20 10:58:09 -05:00
err = c.v.Unmarshal(c)
2019-12-08 03:59:58 -05:00
if err != nil {
2019-12-20 10:58:09 -05:00
return err
2019-12-08 03:59:58 -05:00
}
2019-12-20 10:58:09 -05:00
c.v.OnConfigChange(func(in fsnotify.Event) {
fmt.Println("配置文件更新,重载配置")
c.v.Unmarshal(c)
2019-12-08 03:59:58 -05:00
})
2019-12-20 10:58:09 -05:00
go c.v.WatchConfig()
return nil
2019-12-08 03:59:58 -05:00
}