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 // 站点名称
|
2019-12-10 04:57:57 -05:00
|
|
|
Domain string // 站点域名
|
2019-12-08 03:59:58 -05:00
|
|
|
CookieName string // 浏览器 Cookie 名称
|
|
|
|
}
|
|
|
|
GitHub struct {
|
|
|
|
Admin string // 管理员登录名
|
|
|
|
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
|
|
|
}
|