Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 16 additions & 47 deletions kernel/model/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"github.com/88250/lute"
"github.com/88250/lute/ast"
"github.com/Xuanwo/go-locale"
"github.com/jinzhu/copier"
"github.com/siyuan-note/eventbus"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
Expand Down Expand Up @@ -98,22 +97,6 @@ func NewAppConf() *AppConf {
}
}

// mergeConfWithDefaults 将部分解析失败的配置合并到完整默认配置中。
// 利用 copier 的 IgnoreEmpty 跳过零值/nil 字段,保留已成功解析的配置项,
// 未成功解析的字段回退到 NewAppConf() 的默认值。
func mergeConfWithDefaults() {
fresh := NewAppConf()
if err := copier.CopyWithOption(fresh, Conf, copier.Option{IgnoreEmpty: true, DeepCopy: true}); err != nil {
logging.LogErrorf("merge conf with defaults failed: %s", err)
return
}
// copier 会通过 unsafe 复制未导出字段,导致 fresh 的锁(json.Unmarshal 后为 nil)覆盖默认值,
// 必须在合并后重新初始化锁,否则后续 Lock/Unlock 会 panic。
fresh.m = &sync.RWMutex{}
fresh.userLock = &sync.RWMutex{}
Conf = fresh
}

func (conf *AppConf) GetUILayout() *conf.UILayout {
conf.m.Lock()
defer conf.m.Unlock()
Expand Down Expand Up @@ -147,38 +130,17 @@ func InitConf() {
if data, err := os.ReadFile(confPath); err != nil {
logging.LogErrorf("load conf [%s] failed: %s", confPath, err)
} else {
if conf.NeedsAIMigration(data) {
if err = gulu.JSON.UnmarshalJSON(data, Conf); err != nil {
logging.LogWarnf("parse conf failed, merge available config with defaults: %s", err)
mergeConfWithDefaults()
} else {
Conf.AI = conf.MigrateAI(data)
if nil != Conf.Search && Conf.Search.HanSensitive == nil {
Conf.Search.SetHanSensitive(true)
}
if nil != Conf.AI {
Conf.AI.DecryptAPIKeys()
}
if nil != Conf.Secrets {
Conf.Secrets.Decrypt()
}
Conf.Save()
logging.LogInfof("migrated AI config [%s]", confPath)
}
} else if err = gulu.JSON.UnmarshalJSON(data, Conf); err != nil {
logging.LogWarnf("parse conf failed, merge available config with defaults: %s", err)
mergeConfWithDefaults()
// 解析失败时保留已成功写入的字段;未导出字段(m、userLock)与未触及的导出字段保持 NewAppConf() 初值。
if err = gulu.JSON.UnmarshalJSON(data, Conf); err != nil {
logging.LogWarnf("parse conf failed, parsed fields retained: %s", err)
} else {
logging.LogInfof("loaded conf [%s]", confPath)
if nil != Conf.Search && Conf.Search.HanSensitive == nil {
Conf.Search.SetHanSensitive(true)
}
if nil != Conf.AI {
Conf.AI.DecryptAPIKeys()
}
if nil != Conf.Secrets {
Conf.Secrets.Decrypt()
}
}

if conf.NeedsAIMigration(data) {
Conf.AI = conf.MigrateAI(data)
Conf.Save()
logging.LogInfof("migrated AI config [%s]", confPath)
}
}
}
Expand Down Expand Up @@ -575,6 +537,9 @@ func InitConf() {
if 1 > Conf.Search.BacklinkMentionKeywordsLimit {
Conf.Search.BacklinkMentionKeywordsLimit = 512
}
if nil == Conf.Search.HanSensitive {
Conf.Search.SetHanSensitive(true)
}
sql.SetHanSensitive(Conf.Search.HanSensitiveVal())

if nil == Conf.Stat {
Expand Down Expand Up @@ -629,11 +594,15 @@ func InitConf() {

if nil == Conf.AI {
Conf.AI = conf.NewAI()
} else {
Conf.AI.DecryptAPIKeys()
}
Conf.AI.Normalize()

if nil == Conf.Secrets {
Conf.Secrets = conf.NewSecrets()
} else {
Conf.Secrets.Decrypt()
}

if nil == Conf.Variables {
Expand Down
Loading