answer/internal/base/conf/conf.go

87 lines
2.6 KiB
Go

package conf
import (
"bytes"
"path/filepath"
"github.com/answerdev/answer/configs"
"github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/data"
"github.com/answerdev/answer/internal/base/server"
"github.com/answerdev/answer/internal/base/translator"
"github.com/answerdev/answer/internal/cli"
"github.com/answerdev/answer/internal/router"
"github.com/answerdev/answer/internal/service/service_config"
"github.com/answerdev/answer/pkg/writer"
"github.com/segmentfault/pacman/contrib/conf/viper"
"github.com/segmentfault/pacman/log"
"gopkg.in/yaml.v3"
)
// AllConfig all config
type AllConfig struct {
Debug bool `json:"debug" mapstructure:"debug" yaml:"debug"`
Server *Server `json:"server" mapstructure:"server" yaml:"server"`
Data *Data `json:"data" mapstructure:"data" yaml:"data"`
I18n *translator.I18n `json:"i18n" mapstructure:"i18n" yaml:"i18n"`
ServiceConfig *service_config.ServiceConfig `json:"service_config" mapstructure:"service_config" yaml:"service_config"`
Swaggerui *router.SwaggerConfig `json:"swaggerui" mapstructure:"swaggerui" yaml:"swaggerui"`
}
type PathIgnore struct {
Users []string `yaml:"users"`
}
// Server server config
type Server struct {
HTTP *server.HTTP `json:"http" mapstructure:"http" yaml:"http"`
}
// Data data config
type Data struct {
Database *data.Database `json:"database" mapstructure:"database" yaml:"database"`
Cache *data.CacheConf `json:"cache" mapstructure:"cache" yaml:"cache"`
}
// ReadConfig read config
func ReadConfig(configFilePath string) (c *AllConfig, err error) {
if len(configFilePath) == 0 {
configFilePath = filepath.Join(cli.ConfigFileDir, cli.DefaultConfigFileName)
}
c = &AllConfig{}
config, err := viper.NewWithPath(configFilePath)
if err != nil {
return nil, err
}
if err = config.Parse(&c); err != nil {
return nil, err
}
return c, nil
}
// RewriteConfig rewrite config file path
func RewriteConfig(configFilePath string, allConfig *AllConfig) error {
buf := bytes.Buffer{}
enc := yaml.NewEncoder(&buf)
enc.SetIndent(2)
if err := enc.Encode(allConfig); err != nil {
return err
}
return writer.ReplaceFile(configFilePath, buf.String())
}
func GetPathIgnoreList() map[string]bool {
list := make(map[string]bool, 0)
data := &PathIgnore{}
err := yaml.Unmarshal(configs.PathIgnore, data)
if err != nil {
log.Error(err)
return list
}
for _, item := range data.Users {
list[item] = true
}
constant.PathIgnoreMap = list
return list
}