mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-03-23 03:21:50 +00:00
持久化Token
This commit is contained in:
@@ -2,8 +2,8 @@ package model
|
||||
|
||||
import "time"
|
||||
|
||||
// CtxKeyIsUserLogin ..
|
||||
const CtxKeyIsUserLogin = "ckiul"
|
||||
// CtxKeyAuthorizedUser ..
|
||||
const CtxKeyAuthorizedUser = "ckau"
|
||||
|
||||
// CtxKeyOauth2State ..
|
||||
const CtxKeyOauth2State = "cko2s"
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
@@ -18,26 +20,29 @@ type Config struct {
|
||||
ClientID string
|
||||
ClientSecret string
|
||||
}
|
||||
|
||||
v *viper.Viper
|
||||
}
|
||||
|
||||
// ReadInConfig ..
|
||||
func ReadInConfig(path string) (*Config, error) {
|
||||
viper.SetConfigFile(path)
|
||||
err := viper.ReadInConfig()
|
||||
func (c *Config) Read(path string) error {
|
||||
c.v = viper.New()
|
||||
c.v.SetConfigFile(path)
|
||||
err := c.v.ReadInConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var c Config
|
||||
|
||||
err = viper.Unmarshal(&c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
viper.OnConfigChange(func(in fsnotify.Event) {
|
||||
viper.Unmarshal(&c)
|
||||
err = c.v.Unmarshal(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.v.OnConfigChange(func(in fsnotify.Event) {
|
||||
fmt.Println("配置文件更新,重载配置")
|
||||
c.v.Unmarshal(c)
|
||||
})
|
||||
|
||||
go viper.WatchConfig()
|
||||
return &c, nil
|
||||
go c.v.WatchConfig()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -48,5 +48,5 @@ func NewUserFromGitHub(gu *github.User) User {
|
||||
// IssueNewToken ...
|
||||
func (u *User) IssueNewToken() {
|
||||
u.Token = com.MD5(fmt.Sprintf("%d%d%s", time.Now().UnixNano(), u.ID, u.Login))
|
||||
u.TokenExpired = time.Now().AddDate(0, 0, 14)
|
||||
u.TokenExpired = time.Now().AddDate(0, 2, 0)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user