mirror of
https://github.com/nezhahq/nezha.git
synced 2025-03-11 08:18:13 -04:00
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:
parent
d972d331e2
commit
f1e3613daf
@ -32,8 +32,10 @@ func listConfig(c *gin.Context) (*model.SettingResponse, error) {
|
|||||||
|
|
||||||
conf := model.SettingResponse{
|
conf := model.SettingResponse{
|
||||||
Config: model.Setting{
|
Config: model.Setting{
|
||||||
ConfigForGuests: config.ConfigForGuests,
|
ConfigForGuests: config.ConfigForGuests,
|
||||||
ConfigDashboard: config.ConfigDashboard,
|
ConfigDashboard: config.ConfigDashboard,
|
||||||
|
IgnoredIPNotificationServerIDs: config.IgnoredIPNotificationServerIDs,
|
||||||
|
Oauth2Providers: config.Oauth2Providers,
|
||||||
},
|
},
|
||||||
Version: singleton.Version,
|
Version: singleton.Version,
|
||||||
FrontendTemplates: singleton.FrontendTemplates,
|
FrontendTemplates: singleton.FrontendTemplates,
|
||||||
@ -48,6 +50,7 @@ func listConfig(c *gin.Context) (*model.SettingResponse, error) {
|
|||||||
conf = model.SettingResponse{
|
conf = model.SettingResponse{
|
||||||
Config: model.Setting{
|
Config: model.Setting{
|
||||||
ConfigForGuests: configForGuests,
|
ConfigForGuests: configForGuests,
|
||||||
|
Oauth2Providers: config.Oauth2Providers,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
6
go.mod
6
go.mod
@ -13,7 +13,7 @@ require (
|
|||||||
github.com/gorilla/websocket v1.5.3
|
github.com/gorilla/websocket v1.5.3
|
||||||
github.com/hashicorp/go-uuid v1.0.3
|
github.com/hashicorp/go-uuid v1.0.3
|
||||||
github.com/jinzhu/copier v0.4.0
|
github.com/jinzhu/copier v0.4.0
|
||||||
github.com/knadh/koanf/parsers/yaml v0.1.0
|
github.com/knadh/koanf/maps v0.1.1
|
||||||
github.com/knadh/koanf/providers/env v1.0.0
|
github.com/knadh/koanf/providers/env v1.0.0
|
||||||
github.com/knadh/koanf/providers/file v1.1.2
|
github.com/knadh/koanf/providers/file v1.1.2
|
||||||
github.com/knadh/koanf/v2 v2.1.2
|
github.com/knadh/koanf/v2 v2.1.2
|
||||||
@ -36,9 +36,9 @@ require (
|
|||||||
golang.org/x/sync v0.11.0
|
golang.org/x/sync v0.11.0
|
||||||
google.golang.org/grpc v1.70.0
|
google.golang.org/grpc v1.70.0
|
||||||
google.golang.org/protobuf v1.36.5
|
google.golang.org/protobuf v1.36.5
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
|
||||||
gorm.io/driver/sqlite v1.5.7
|
gorm.io/driver/sqlite v1.5.7
|
||||||
gorm.io/gorm v1.25.12
|
gorm.io/gorm v1.25.12
|
||||||
|
sigs.k8s.io/yaml v1.4.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@ -63,7 +63,6 @@ require (
|
|||||||
github.com/josharian/intern v1.0.0 // indirect
|
github.com/josharian/intern v1.0.0 // indirect
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
|
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
|
||||||
github.com/knadh/koanf/maps v0.1.1 // indirect
|
|
||||||
github.com/leodido/go-urn v1.4.0 // indirect
|
github.com/leodido/go-urn v1.4.0 // indirect
|
||||||
github.com/mailru/easyjson v0.9.0 // indirect
|
github.com/mailru/easyjson v0.9.0 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
@ -86,4 +85,5 @@ require (
|
|||||||
golang.org/x/text v0.22.0 // indirect
|
golang.org/x/text v0.22.0 // indirect
|
||||||
golang.org/x/tools v0.30.0 // indirect
|
golang.org/x/tools v0.30.0 // indirect
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250219182151-9fdb1cabc7b2 // indirect
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20250219182151-9fdb1cabc7b2 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
5
go.sum
5
go.sum
@ -60,6 +60,7 @@ github.com/golang-jwt/jwt/v4 v4.5.1 h1:JdqV9zKUdtaa9gdPlywC3aeoEsR681PlKC+4F5gQg
|
|||||||
github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
||||||
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
||||||
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
||||||
|
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
@ -84,8 +85,6 @@ github.com/klauspost/cpuid/v2 v2.2.9 h1:66ze0taIn2H33fBvCkXuv9BmCwDfafmiIVpKV9kK
|
|||||||
github.com/klauspost/cpuid/v2 v2.2.9/go.mod h1:rqkxqrZ1EhYM9G+hXH7YdowN5R5RGN6NK4QwQ3WMXF8=
|
github.com/klauspost/cpuid/v2 v2.2.9/go.mod h1:rqkxqrZ1EhYM9G+hXH7YdowN5R5RGN6NK4QwQ3WMXF8=
|
||||||
github.com/knadh/koanf/maps v0.1.1 h1:G5TjmUh2D7G2YWf5SQQqSiHRJEjaicvU0KpypqB3NIs=
|
github.com/knadh/koanf/maps v0.1.1 h1:G5TjmUh2D7G2YWf5SQQqSiHRJEjaicvU0KpypqB3NIs=
|
||||||
github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI=
|
github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI=
|
||||||
github.com/knadh/koanf/parsers/yaml v0.1.0 h1:ZZ8/iGfRLvKSaMEECEBPM1HQslrZADk8fP1XFUxVI5w=
|
|
||||||
github.com/knadh/koanf/parsers/yaml v0.1.0/go.mod h1:cvbUDC7AL23pImuQP0oRw/hPuccrNBS2bps8asS0CwY=
|
|
||||||
github.com/knadh/koanf/providers/env v1.0.0 h1:ufePaI9BnWH+ajuxGGiJ8pdTG0uLEUWC7/HDDPGLah0=
|
github.com/knadh/koanf/providers/env v1.0.0 h1:ufePaI9BnWH+ajuxGGiJ8pdTG0uLEUWC7/HDDPGLah0=
|
||||||
github.com/knadh/koanf/providers/env v1.0.0/go.mod h1:mzFyRZueYhb37oPmC1HAv/oGEEuyvJDA98r3XAa8Gak=
|
github.com/knadh/koanf/providers/env v1.0.0/go.mod h1:mzFyRZueYhb37oPmC1HAv/oGEEuyvJDA98r3XAa8Gak=
|
||||||
github.com/knadh/koanf/providers/file v1.1.2 h1:aCC36YGOgV5lTtAFz2qkgtWdeQsgfxUkxDOe+2nQY3w=
|
github.com/knadh/koanf/providers/file v1.1.2 h1:aCC36YGOgV5lTtAFz2qkgtWdeQsgfxUkxDOe+2nQY3w=
|
||||||
@ -248,3 +247,5 @@ gorm.io/driver/sqlite v1.5.7/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDa
|
|||||||
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
|
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
|
||||||
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
|
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
|
||||||
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||||
|
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
|
||||||
|
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
|
||||||
|
@ -3,14 +3,14 @@ package model
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/go-viper/mapstructure/v2"
|
"github.com/go-viper/mapstructure/v2"
|
||||||
kyaml "github.com/knadh/koanf/parsers/yaml"
|
kmaps "github.com/knadh/koanf/maps"
|
||||||
"github.com/knadh/koanf/providers/env"
|
"github.com/knadh/koanf/providers/env"
|
||||||
"github.com/knadh/koanf/providers/file"
|
"github.com/knadh/koanf/providers/file"
|
||||||
"github.com/knadh/koanf/v2"
|
"github.com/knadh/koanf/v2"
|
||||||
|
"sigs.k8s.io/yaml"
|
||||||
|
|
||||||
"github.com/nezhahq/nezha/pkg/utils"
|
"github.com/nezhahq/nezha/pkg/utils"
|
||||||
)
|
)
|
||||||
@ -22,25 +22,19 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ConfigForGuests struct {
|
type ConfigForGuests struct {
|
||||||
Language string `koanf:"language" json:"language"` // 系统语言,默认 zh_CN
|
Language string `koanf:"language" json:"language"` // 系统语言,默认 zh_CN
|
||||||
SiteName string `koanf:"site_name" json:"site_name"`
|
SiteName string `koanf:"site_name" json:"site_name"`
|
||||||
CustomCode string `koanf:"custom_code" json:"custom_code,omitempty"`
|
CustomCode string `koanf:"custom_code" json:"custom_code,omitempty"`
|
||||||
CustomCodeDashboard string `koanf:"custom_code_dashboard" json:"custom_code_dashboard,omitempty"`
|
CustomCodeDashboard string `koanf:"custom_code_dashboard" json:"custom_code_dashboard,omitempty"`
|
||||||
Oauth2Providers []string `koanf:"-" json:"oauth2_providers,omitempty"` // oauth2 供应商列表,无需配置,自动生成
|
|
||||||
|
|
||||||
InstallHost string `koanf:"install_host" json:"install_host,omitempty"`
|
InstallHost string `koanf:"install_host" json:"install_host,omitempty"`
|
||||||
AgentTLS bool `koanf:"tls" json:"tls,omitempty"` // 用于前端判断生成的安装命令是否启用 TLS
|
AgentTLS bool `koanf:"tls" json:"tls,omitempty"` // 用于前端判断生成的安装命令是否启用 TLS
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConfigDashboard struct {
|
type ConfigDashboard struct {
|
||||||
Debug bool `koanf:"debug" json:"debug,omitempty"` // debug模式开关
|
RealIPHeader string `koanf:"real_ip_header" json:"real_ip_header,omitempty"` // 真实IP
|
||||||
RealIPHeader string `koanf:"real_ip_header" json:"real_ip_header,omitempty"` // 真实IP
|
UserTemplate string `koanf:"user_template" json:"user_template,omitempty"`
|
||||||
UserTemplate string `koanf:"user_template" json:"user_template,omitempty"`
|
AdminTemplate string `koanf:"admin_template" json:"admin_template,omitempty"`
|
||||||
AdminTemplate string `koanf:"admin_template" json:"admin_template,omitempty"`
|
|
||||||
Location string `koanf:"location" json:"location,omitempty"` // 时区,默认为 Asia/Shanghai
|
|
||||||
ForceAuth bool `koanf:"force_auth" json:"force_auth,omitempty"` // 强制要求认证
|
|
||||||
AgentSecretKey string `koanf:"agent_secret_key" json:"agent_secret_key,omitempty"`
|
|
||||||
JWTTimeout int `mapstructure:"jwt_timeout" json:"jwt_timeout,omitempty"` // JWT token过期时间(小时)
|
|
||||||
|
|
||||||
EnablePlainIPInNotification bool `koanf:"enable_plain_ip_in_notification" json:"enable_plain_ip_in_notification,omitempty"` // 通知信息IP不打码
|
EnablePlainIPInNotification bool `koanf:"enable_plain_ip_in_notification" json:"enable_plain_ip_in_notification,omitempty"` // 通知信息IP不打码
|
||||||
|
|
||||||
@ -50,15 +44,21 @@ type ConfigDashboard struct {
|
|||||||
Cover uint8 `koanf:"cover" json:"cover"` // 覆盖范围(0:提醒未被 IgnoredIPNotification 包含的所有服务器; 1:仅提醒被 IgnoredIPNotification 包含的服务器;)
|
Cover uint8 `koanf:"cover" json:"cover"` // 覆盖范围(0:提醒未被 IgnoredIPNotification 包含的所有服务器; 1:仅提醒被 IgnoredIPNotification 包含的服务器;)
|
||||||
IgnoredIPNotification string `koanf:"ignored_ip_notification" json:"ignored_ip_notification,omitempty"` // 特定服务器IP(多个服务器用逗号分隔)
|
IgnoredIPNotification string `koanf:"ignored_ip_notification" json:"ignored_ip_notification,omitempty"` // 特定服务器IP(多个服务器用逗号分隔)
|
||||||
|
|
||||||
IgnoredIPNotificationServerIDs map[uint64]bool `koanf:"ignored_ip_notification_server_ids" json:"ignored_ip_notification_server_ids,omitempty"` // [ServerID] -> bool(值为true代表当前ServerID在特定服务器列表内)
|
DNSServers string `koanf:"dns_servers" json:"dns_servers,omitempty"`
|
||||||
AvgPingCount int `koanf:"avg_ping_count" json:"avg_ping_count,omitempty"`
|
|
||||||
DNSServers string `koanf:"dns_servers" json:"dns_servers,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
ConfigForGuests
|
ConfigForGuests
|
||||||
ConfigDashboard
|
ConfigDashboard
|
||||||
|
|
||||||
|
AvgPingCount int `koanf:"avg_ping_count" json:"avg_ping_count,omitempty"`
|
||||||
|
|
||||||
|
Debug bool `koanf:"debug" json:"debug,omitempty"` // debug模式开关
|
||||||
|
Location string `koanf:"location" json:"location,omitempty"` // 时区,默认为 Asia/Shanghai
|
||||||
|
ForceAuth bool `koanf:"force_auth" json:"force_auth,omitempty"` // 强制要求认证
|
||||||
|
AgentSecretKey string `koanf:"agent_secret_key" json:"agent_secret_key,omitempty"`
|
||||||
|
JWTTimeout int `koanf:"jwt_timeout" json:"jwt_timeout,omitempty"` // JWT token过期时间(小时)
|
||||||
|
|
||||||
JWTSecretKey string `koanf:"jwt_secret_key" json:"jwt_secret_key,omitempty"`
|
JWTSecretKey string `koanf:"jwt_secret_key" json:"jwt_secret_key,omitempty"`
|
||||||
ListenPort uint16 `koanf:"listen_port" json:"listen_port,omitempty"`
|
ListenPort uint16 `koanf:"listen_port" json:"listen_port,omitempty"`
|
||||||
ListenHost string `koanf:"listen_host" json:"listen_host,omitempty"`
|
ListenHost string `koanf:"listen_host" json:"listen_host,omitempty"`
|
||||||
@ -67,17 +67,19 @@ type Config struct {
|
|||||||
Oauth2 map[string]*Oauth2Config `koanf:"oauth2" json:"oauth2,omitempty"`
|
Oauth2 map[string]*Oauth2Config `koanf:"oauth2" json:"oauth2,omitempty"`
|
||||||
|
|
||||||
// HTTPS 配置
|
// HTTPS 配置
|
||||||
HTTPS struct {
|
HTTPS HTTPSConf `koanf:"https" json:"https"`
|
||||||
ListenPort uint16 `koanf:"listen_port" json:"listen_port,omitempty"`
|
|
||||||
TLSCertPath string `koanf:"tls_cert_path" json:"tls_cert_path,omitempty"`
|
|
||||||
TLSKeyPath string `koanf:"tls_key_path" json:"tls_key_path,omitempty"`
|
|
||||||
InsecureTLS bool `koanf:"insecure_tls" json:"insecure_tls,omitempty"`
|
|
||||||
} `koanf:"https" json:"https"`
|
|
||||||
|
|
||||||
k *koanf.Koanf `json:"-"`
|
k *koanf.Koanf `json:"-"`
|
||||||
filePath string `json:"-"`
|
filePath string `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type HTTPSConf struct {
|
||||||
|
InsecureTLS bool `koanf:"insecure_tls" json:"insecure_tls,omitempty"`
|
||||||
|
ListenPort uint16 `koanf:"listen_port" json:"listen_port,omitempty"`
|
||||||
|
TLSCertPath string `koanf:"tls_cert_path" json:"tls_cert_path,omitempty"`
|
||||||
|
TLSKeyPath string `koanf:"tls_key_path" json:"tls_key_path,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
// Read 读取配置文件并应用
|
// Read 读取配置文件并应用
|
||||||
func (c *Config) Read(path string, frontendTemplates []FrontendTemplate) error {
|
func (c *Config) Read(path string, frontendTemplates []FrontendTemplate) error {
|
||||||
c.k = koanf.New(".")
|
c.k = koanf.New(".")
|
||||||
@ -91,7 +93,7 @@ func (c *Config) Read(path string, frontendTemplates []FrontendTemplate) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(path); err == nil {
|
if _, err := os.Stat(path); err == nil {
|
||||||
err = c.k.Load(file.Provider(path), kyaml.Parser())
|
err = c.k.Load(file.Provider(path), new(utils.KubeYAML), koanf.WithMergeFunc(mergeDedup))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -160,31 +162,24 @@ func (c *Config) Read(path string, frontendTemplates []FrontendTemplate) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Oauth2Providers = utils.MapKeysToSlice(c.Oauth2)
|
|
||||||
|
|
||||||
c.updateIgnoredIPNotificationID()
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateIgnoredIPNotificationID 更新用于判断服务器ID是否属于特定服务器的map
|
|
||||||
func (c *Config) updateIgnoredIPNotificationID() {
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save 保存配置文件
|
// Save 保存配置文件
|
||||||
func (c *Config) Save() error {
|
func (c *Config) Save() error {
|
||||||
c.updateIgnoredIPNotificationID()
|
return c.save()
|
||||||
data, err := c.k.Marshal(kyaml.Parser())
|
}
|
||||||
|
|
||||||
|
func (c *Config) save() error {
|
||||||
|
data, err := yaml.Marshal(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return c.write(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) write(data []byte) error {
|
||||||
dir := filepath.Dir(c.filePath)
|
dir := filepath.Dir(c.filePath)
|
||||||
if err := os.MkdirAll(dir, 0750); err != nil {
|
if err := os.MkdirAll(dir, 0750); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -210,3 +205,20 @@ func koanfConf(c any) koanf.UnmarshalConf {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mergeDedup(src, dst map[string]any) error {
|
||||||
|
for key := range src {
|
||||||
|
if strings.IndexByte(key, '_') == -1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
oldKey := strings.ReplaceAll(key, "_", "")
|
||||||
|
if _, ok := dst[oldKey]; ok {
|
||||||
|
src[oldKey] = src[key]
|
||||||
|
delete(src, key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
kmaps.Merge(src, dst)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -21,6 +21,9 @@ type SettingForm struct {
|
|||||||
type Setting struct {
|
type Setting struct {
|
||||||
ConfigForGuests
|
ConfigForGuests
|
||||||
ConfigDashboard
|
ConfigDashboard
|
||||||
|
|
||||||
|
IgnoredIPNotificationServerIDs map[uint64]bool `json:"ignored_ip_notification_server_ids,omitempty"`
|
||||||
|
Oauth2Providers []string `json:"oauth2_providers,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type FrontendTemplate struct {
|
type FrontendTemplate struct {
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/go-viper/mapstructure/v2"
|
"github.com/go-viper/mapstructure/v2"
|
||||||
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TextUnmarshalerHookFunc is a fixed version of mapstructure.TextUnmarshallerHookFunc.
|
// TextUnmarshalerHookFunc is a fixed version of mapstructure.TextUnmarshallerHookFunc.
|
||||||
@ -69,3 +70,21 @@ func TextUnmarshalerHookFunc() mapstructure.DecodeHookFuncType {
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// KubeYAML implements a YAML parser.
|
||||||
|
type KubeYAML struct{}
|
||||||
|
|
||||||
|
// Unmarshal parses the given YAML bytes.
|
||||||
|
func (p *KubeYAML) Unmarshal(b []byte) (map[string]any, error) {
|
||||||
|
var out map[string]any
|
||||||
|
if err := yaml.Unmarshal(b, &out); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Marshal marshals the given config map to YAML bytes.
|
||||||
|
func (p *KubeYAML) Marshal(o map[string]any) ([]byte, error) {
|
||||||
|
return yaml.Marshal(o)
|
||||||
|
}
|
||||||
|
53
service/singleton/config.go
Normal file
53
service/singleton/config.go
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,14 +3,14 @@
|
|||||||
repository: "https://github.com/nezhahq/admin-frontend"
|
repository: "https://github.com/nezhahq/admin-frontend"
|
||||||
author: "nezhahq"
|
author: "nezhahq"
|
||||||
version: "v1.8.0"
|
version: "v1.8.0"
|
||||||
isadmin: true
|
is_admin: true
|
||||||
isofficial: true
|
is_official: true
|
||||||
- path: "user-dist"
|
- path: "user-dist"
|
||||||
name: "Official"
|
name: "Official"
|
||||||
repository: "https://github.com/hamster1963/nezha-dash-v1"
|
repository: "https://github.com/hamster1963/nezha-dash-v1"
|
||||||
author: "hamster1963"
|
author: "hamster1963"
|
||||||
version: "v1.23.0"
|
version: "v1.23.0"
|
||||||
isofficial: true
|
is_official: true
|
||||||
- path: "nezha-ascii-dist"
|
- path: "nezha-ascii-dist"
|
||||||
name: "Nezha-ASCII"
|
name: "Nezha-ASCII"
|
||||||
repository: "https://github.com/hamster1963/nezha-ascii"
|
repository: "https://github.com/hamster1963/nezha-ascii"
|
||||||
|
@ -11,9 +11,9 @@ import (
|
|||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/patrickmn/go-cache"
|
"github.com/patrickmn/go-cache"
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
"gorm.io/driver/sqlite"
|
"gorm.io/driver/sqlite"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
"sigs.k8s.io/yaml"
|
||||||
|
|
||||||
"github.com/nezhahq/nezha/model"
|
"github.com/nezhahq/nezha/model"
|
||||||
"github.com/nezhahq/nezha/pkg/utils"
|
"github.com/nezhahq/nezha/pkg/utils"
|
||||||
@ -22,7 +22,6 @@ import (
|
|||||||
var Version = "debug"
|
var Version = "debug"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Conf *model.Config
|
|
||||||
Cache *cache.Cache
|
Cache *cache.Cache
|
||||||
DB *gorm.DB
|
DB *gorm.DB
|
||||||
Loc *time.Location
|
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 从给出的文件路径中加载数据库
|
// InitDBFromPath 从给出的文件路径中加载数据库
|
||||||
func InitDBFromPath(path string) {
|
func InitDBFromPath(path string) {
|
||||||
var err error
|
var err error
|
||||||
|
Loading…
Reference in New Issue
Block a user