package common import ( "fmt" "log" "os" "regexp" "strings" "gitea.bywaystudios.com/pet_home/nacos" "gopkg.in/yaml.v2" ) type SshConfig struct { Name string `yaml:"name"` Host string `yaml:"host"` Port int `yaml:"port"` Username string `yaml:"username"` Password string `yaml:"password"` PrivateKeyPath string } type MysqlConfig struct { Name string `yaml:"name"` Host string `yaml:"host"` Port int `yaml:"port"` Username string `yaml:"user"` Password string `yaml:"password"` // Database string `yaml:"database"` } type SystemConfig struct { NMap bool `yaml:"nmap"` // 是否开启端口扫描 FeishUrl string `yaml:"feishu_url"` // 飞书机器人url NoticeUrl string `yaml:"notice_url"` // 通知url OperationUrl string `yaml:"operation_url"` // 运营url OperationChatId string `yaml:"operation_chat_id"` // 运营群id USOperationChatId string `yaml:"us_operation_chat_id"` // US运营群id ClientChatId string `yaml:"client_chat_id"` // 客户端群id Ssh bool `yaml:"ssh"` // 是否开启SSH连接 Env string `yaml:"env"` // 环境变量 RpcSwitch bool `yaml:"rpc_switch"` // 是否开启RPC调用 } type Config struct { Servers []SshConfig `yaml:"servers"` Mysqls []MysqlConfig `yaml:"mysqls"` System SystemConfig `yaml:"system"` } type TranlaterUser struct { Account string `yaml:"account"` Keys string `yaml:"keys"` KeysType string `yaml:"keysType"` Colunm string `yaml:"colunm"` } type TranlaterConfig struct { User []TranlaterUser `yaml:"user"` } type LanguageLimit struct { Key string `json:"key"` Zh_CN int `json:"zh_CN"` Latin int `json:"latin"` } var config *Config var tranlaterConfig *TranlaterConfig var launchEnvRegexp = regexp.MustCompile(`"ENV"\s*:\s*"([^"]+)"`) func currentRuntimeEnv() string { if env, ok := os.LookupEnv("ENV"); ok { env = strings.TrimSpace(env) if env != "" { return env } } content, err := os.ReadFile(".vscode/launch.json") if err != nil { return "" } matches := launchEnvRegexp.FindStringSubmatch(string(content)) if len(matches) != 2 { return "" } return strings.TrimSpace(matches[1]) } func Init() error { err := loadConfig() if err != nil { return fmt.Errorf("failed to load config: %w", err) } return nil } func InitTranlater() error { data, err := os.ReadFile("conf/translater.yml") if err != nil { return err } err = yaml.Unmarshal(data, &tranlaterConfig) if err != nil { return err } return nil } func loadConfig() error { nacosClient, err := nacos.GetNacosClient() if err != nil { return fmt.Errorf("get nacos client: %w", err) } configfile := "web-backend-env-conf" if currentRuntimeEnv() == "dev" { configfile = "web-backend-env-conf-dev" } err = nacosClient.GetYAML(configfile, "web", &config) if err != nil { return fmt.Errorf("get nacos config: %w", err) } nacosClient.Listen(configfile, "web", func(event nacos.ChangeEvent) { var newConfig Config if err := yaml.Unmarshal([]byte(event.Content), &newConfig); err != nil { log.Printf("解析 Nacos 配置变更失败: %v", err) return } config = &newConfig log.Printf("Nacos 配置已更新: %v", config) }) return nil } func GetServerConfig(name string) (*SshConfig, error) { for _, server := range config.Servers { if server.Name == name { return &server, nil } } return nil, fmt.Errorf("server configuration not found: %s", name) } func GetMysqlConfig(name string) (*MysqlConfig, error) { for _, mysql := range config.Mysqls { if mysql.Name == name { return &mysql, nil } } return nil, fmt.Errorf("mysql configuration not found: %s", name) } func GetWsConfig(name string) (*SshConfig, error) { return GetServerConfig("ws") } func GetFeishuUrl() string { return config.System.FeishUrl } func GetNoticeUrl() string { return config.System.NoticeUrl } func GetOperationUrl() string { return config.System.OperationUrl } func GetNMap() bool { return config.System.NMap } func GetOperationChatId() string { return config.System.OperationChatId } func GetUSOperationChatId() string { return config.System.USOperationChatId } func GetClientChatId() string { return config.System.ClientChatId } func GetSsh() bool { return config.System.Ssh } func GetEnv() string { if config == nil || strings.TrimSpace(config.System.Env) == "" { return currentRuntimeEnv() } return config.System.Env } func GetRpcSwitch() bool { return config.System.RpcSwitch } func GetTranlaterConfig(account string) *TranlaterUser { for _, user := range tranlaterConfig.User { if user.Account == account { return &user } } return nil }