package config import ( "encoding/json" "intent-system/pkg/itypes" "io/ioutil" "os" "path" "github.com/civet148/log" ) const ( strConfName = "config.json" RunConfigName = "system-backend" ) type Config struct { Version string `json:"version"` //版本号 DSN string `json:"dsn" toml:"dsn" db:"dsn" cli:"dsn"` //数据库连接字符串 Debug bool `json:"debug" toml:"debug" db:"debug" cli:"debug"` //开启debug模式 HttpAddr string `json:"http_addr" toml:"http_addr" db:"http_addr" cli:"http-addr"` //监听地址 Domain string `json:"domain" toml:"domain" db:"domain" cli:"domain"` //域名URL配置 Static string `json:"static" toml:"static" db:"static" cli:"static"` //静态页面文件路径 ImagePrefix string `json:"image_prefix" toml:"image_prefix" db:"image_prefix" cli:"image-prefix"` //用户端图片访问域名 ImagePath string `json:"image_path" toml:"image_path" db:"image_path" cli:"image-path"` //图片存储路径 GatewayUrl string `json:"gateway_url" toml:"gateway_url" db:"gateway_url" cli:"gateway-url"` //网关地址URL GatewayKey string `json:"gateway_key" toml:"gateway_key" db:"gateway_key" cli:"gateway-key"` //网关访问Key GatewaySecret string `json:"gateway_secret" toml:"gateway_secret" db:"gateway_secret" cli:"gateway-secret"` //网关访问密码 Postgresql string `json:"postgresql" toml:"postgresql" db:"postgresql" cli:"pg"` //Postgresql数据库连接字符串 SubCron string `json:"sub_cron" toml:"sub_cron" db:"sub_cron" cli:"sub-cron"` //订阅邮件定时任务 } var strFilePath = path.Join(itypes.DefaultConfigHome, strConfName) func (c *Config) Save() (err error) { var data []byte strConfigPath := itypes.DefaultConfigHome if err = os.MkdirAll(strConfigPath, os.ModePerm); err != nil { return log.Errorf("make dir [%s] error [%s]", strConfigPath, err.Error()) } data, err = json.Marshal(c) if err != nil { return log.Errorf("marshal json error: %s", err.Error()) } err = ioutil.WriteFile(strFilePath, data, os.ModePerm) if err != nil { return log.Errorf("write to file %s error: %s", strFilePath, err.Error()) } return nil } func (c *Config) Load() (err error) { var data []byte data, err = ioutil.ReadFile(strFilePath) if err != nil { return log.Errorf("read file %s error: %s", strFilePath, err.Error()) } err = json.Unmarshal(data, c) if err != nil { return log.Errorf("unmarshal error: %s", err.Error()) } return nil }