mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-09-19 09:40:12 +00:00
62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package singleton
|
|
|
|
import (
|
|
"log"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/nezhahq/nezha/model"
|
|
"github.com/nezhahq/nezha/pkg/utils"
|
|
)
|
|
|
|
var Conf *ConfigClass
|
|
|
|
type ConfigClass struct {
|
|
*model.Config
|
|
|
|
IgnoredIPNotificationServerIDs map[uint64]bool `json:"ignored_ip_notification_server_ids,omitempty"`
|
|
Oauth2Providers []string `json:"oauth2_providers,omitempty"`
|
|
}
|
|
|
|
// InitConfigFromPath 从给出的文件路径中加载配置
|
|
func InitConfigFromPath(path string) error {
|
|
Conf = &ConfigClass{
|
|
Config: &model.Config{},
|
|
}
|
|
err := Conf.Read(path, FrontendTemplates)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rotated, err := Conf.RotateJWTSecretKeyIfNeeded(Version)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if rotated {
|
|
log.Printf("NEZHA>> Rotated jwt_secret_key for dashboard version %s", Version)
|
|
}
|
|
|
|
Conf.updateIgnoredIPNotificationID()
|
|
Conf.Oauth2Providers = utils.MapKeysToSlice(Conf.Oauth2)
|
|
return nil
|
|
}
|
|
|
|
func (c *ConfigClass) Save() error {
|
|
c.updateIgnoredIPNotificationID()
|
|
return c.Config.Save()
|
|
}
|
|
|
|
// updateIgnoredIPNotificationID 更新用于判断服务器ID是否属于特定服务器的map
|
|
func (c *ConfigClass) updateIgnoredIPNotificationID() {
|
|
if c.IgnoredIPNotification == "" {
|
|
return
|
|
}
|
|
|
|
c.IgnoredIPNotificationServerIDs = make(map[uint64]bool)
|
|
for splitedID := range strings.SplitSeq(c.IgnoredIPNotification, ",") {
|
|
id, _ := strconv.ParseUint(splitedID, 10, 64)
|
|
if id > 0 {
|
|
c.IgnoredIPNotificationServerIDs[id] = true
|
|
}
|
|
}
|
|
}
|