Files
go-site-clone/config/index.go
2025-10-14 18:15:01 +08:00

43 lines
747 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package config
import (
"os"
"gopkg.in/yaml.v3"
)
type AppConfig struct {
Port int `yaml:"port"`
WsUrl string `yaml:"wsUrl"`
StunUrl string `yaml:"stunUrl"`
ApiUrl string `yaml:"apiUrl"`
AppName string `yaml:"appName"`
}
func LoadConfig() (*AppConfig, error) {
// 读取文件内容
data, err := os.ReadFile("config.yaml")
if err != nil {
return nil, err
}
// 解析 YAML
var cfg AppConfig
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, err
}
return &cfg, nil
}
// 更新配置(写回文件)
func SaveConfig(cfg *AppConfig) error {
data, err := yaml.Marshal(cfg)
if err != nil {
return err
}
// 写回文件0644 表示文件权限
return os.WriteFile("config.yaml", data, 0644)
}