configx.go 566 B

123456789101112131415161718192021222324252627282930
  1. package configx
  2. import (
  3. "github.com/rotisserie/eris"
  4. "github.com/spf13/viper"
  5. )
  6. type Config struct {
  7. Name string `toml:"name"`
  8. Port int `toml:"port"`
  9. Environment string `toml:"environment"`
  10. }
  11. type LoadOption func() error
  12. func LoadConfig(file string, opts ...LoadOption) error {
  13. viper.SetConfigFile(file)
  14. viper.SetConfigType("toml")
  15. if err := viper.ReadInConfig(); err != nil {
  16. panic(eris.Wrap(err, "无法加载配置"))
  17. }
  18. var err error
  19. for _, opt := range opts {
  20. if err = opt(); err != nil {
  21. return err
  22. }
  23. }
  24. return nil
  25. }