99 lines
2.7 KiB
Go
99 lines
2.7 KiB
Go
package configs
|
||
|
||
import (
|
||
"github.com/iwind/TeaGo/Tea"
|
||
"gopkg.in/yaml.v3"
|
||
"os"
|
||
"sync"
|
||
)
|
||
|
||
var (
|
||
brandConfig *BrandConfig
|
||
brandConfigOnce sync.Once
|
||
)
|
||
|
||
// BrandConfig 品牌配置
|
||
type BrandConfig struct {
|
||
OfficialSite string `yaml:"officialSite" json:"officialSite"`
|
||
DocsSite string `yaml:"docsSite" json:"docsSite"`
|
||
DocsPathPrefix string `yaml:"docsPathPrefix" json:"docsPathPrefix"`
|
||
DefaultInstallPath string `yaml:"defaultInstallPath" json:"defaultInstallPath"`
|
||
ProductName string `yaml:"productName" json:"productName"`
|
||
}
|
||
|
||
// GetBrandConfig 获取品牌配置
|
||
func GetBrandConfig() *BrandConfig {
|
||
brandConfigOnce.Do(func() {
|
||
brandConfig = loadBrandConfig()
|
||
})
|
||
return brandConfig
|
||
}
|
||
|
||
func loadBrandConfig() *BrandConfig {
|
||
config := &BrandConfig{
|
||
OfficialSite: getEnvOrDefault("BRAND_OFFICIAL_SITE", "https://goedge.cn"),
|
||
DocsSite: getEnvOrDefault("BRAND_DOCS_SITE", "https://goedge.cn"),
|
||
DocsPathPrefix: getEnvOrDefault("BRAND_DOCS_PREFIX", "/docs"),
|
||
DefaultInstallPath: getEnvOrDefault("BRAND_INSTALL_PATH", "/usr/local/goedge"),
|
||
ProductName: getEnvOrDefault("BRAND_PRODUCT_NAME", "GoEdge"),
|
||
}
|
||
|
||
// 从配置文件加载
|
||
configFile := Tea.ConfigFile("brand.yaml")
|
||
if data, err := os.ReadFile(configFile); err == nil {
|
||
var fileConfig struct {
|
||
Brand BrandConfig `yaml:"brand"`
|
||
}
|
||
if err := yaml.Unmarshal(data, &fileConfig); err == nil {
|
||
if fileConfig.Brand.OfficialSite != "" {
|
||
config.OfficialSite = fileConfig.Brand.OfficialSite
|
||
}
|
||
if fileConfig.Brand.DocsSite != "" {
|
||
config.DocsSite = fileConfig.Brand.DocsSite
|
||
}
|
||
if fileConfig.Brand.DocsPathPrefix != "" {
|
||
config.DocsPathPrefix = fileConfig.Brand.DocsPathPrefix
|
||
}
|
||
if fileConfig.Brand.DefaultInstallPath != "" {
|
||
config.DefaultInstallPath = fileConfig.Brand.DefaultInstallPath
|
||
}
|
||
if fileConfig.Brand.ProductName != "" {
|
||
config.ProductName = fileConfig.Brand.ProductName
|
||
}
|
||
}
|
||
}
|
||
|
||
return config
|
||
}
|
||
|
||
// GetDocsURL 获取文档 URL
|
||
func (c *BrandConfig) GetDocsURL(path string) string {
|
||
if len(path) > 0 && path[0] != '/' {
|
||
path = "/" + path
|
||
}
|
||
return c.DocsSite + c.DocsPathPrefix + path
|
||
}
|
||
|
||
// GetFullDocsURL 获取完整文档 URL
|
||
func (c *BrandConfig) GetFullDocsURL(path string) string {
|
||
return c.GetDocsURL(path)
|
||
}
|
||
|
||
// ToMap 转换为 map(用于前端)
|
||
func (c *BrandConfig) ToMap() map[string]interface{} {
|
||
return map[string]interface{}{
|
||
"officialSite": c.OfficialSite,
|
||
"docsSite": c.DocsSite,
|
||
"docsPathPrefix": c.DocsPathPrefix,
|
||
"defaultInstallPath": c.DefaultInstallPath,
|
||
"productName": c.ProductName,
|
||
}
|
||
}
|
||
|
||
func getEnvOrDefault(key, defaultValue string) string {
|
||
if value := os.Getenv(key); value != "" {
|
||
return value
|
||
}
|
||
return defaultValue
|
||
}
|