fix: config fields not generated on first startup (#1016)

* fix: config fields not generated on first startup

* cleanup

* fix reference

* replace yaml module

* remove duplicated fields

* remove yaml.v3 as a direct dependency

* update dependency

* chore
This commit is contained in:
UUBulb
2025-03-03 19:02:25 +08:00
committed by GitHub
parent d972d331e2
commit f1e3613daf
9 changed files with 143 additions and 62 deletions

View File

@@ -0,0 +1,53 @@
package singleton
import (
"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
}
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
}
}
}

View File

@@ -3,14 +3,14 @@
repository: "https://github.com/nezhahq/admin-frontend"
author: "nezhahq"
version: "v1.8.0"
isadmin: true
isofficial: true
is_admin: true
is_official: true
- path: "user-dist"
name: "Official"
repository: "https://github.com/hamster1963/nezha-dash-v1"
author: "hamster1963"
version: "v1.23.0"
isofficial: true
is_official: true
- path: "nezha-ascii-dist"
name: "Nezha-ASCII"
repository: "https://github.com/hamster1963/nezha-ascii"

View File

@@ -11,9 +11,9 @@ import (
"github.com/gin-gonic/gin"
"github.com/patrickmn/go-cache"
"gopkg.in/yaml.v3"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"sigs.k8s.io/yaml"
"github.com/nezhahq/nezha/model"
"github.com/nezhahq/nezha/pkg/utils"
@@ -22,7 +22,6 @@ import (
var Version = "debug"
var (
Conf *model.Config
Cache *cache.Cache
DB *gorm.DB
Loc *time.Location
@@ -69,15 +68,6 @@ func InitFrontendTemplates() {
}
}
// InitConfigFromPath 从给出的文件路径中加载配置
func InitConfigFromPath(path string) {
Conf = &model.Config{}
err := Conf.Read(path, FrontendTemplates)
if err != nil {
panic(err)
}
}
// InitDBFromPath 从给出的文件路径中加载数据库
func InitDBFromPath(path string) {
var err error