config.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package config
  2. import (
  3. "github.com/pkg/errors"
  4. "go-micro.dev/v4/config"
  5. "go-micro.dev/v4/config/source/env"
  6. )
  7. type Config struct {
  8. Address string
  9. Tracing TracingConfig
  10. Registry RegistryConfig
  11. Redis RedisConfig
  12. AuthorizationService string
  13. }
  14. type TracingConfig struct {
  15. Enable bool
  16. Jaeger JaegerConfig
  17. }
  18. type JaegerConfig struct {
  19. URL string
  20. }
  21. type RegistryConfig struct {
  22. Enable bool
  23. Consul ConsulConfig
  24. }
  25. type ConsulConfig struct {
  26. URL string
  27. }
  28. type RedisConfig struct {
  29. Enable bool
  30. URL string
  31. Password string
  32. }
  33. var cfg *Config = &Config{
  34. Address: ":8080",
  35. AuthorizationService: "authorizationservice",
  36. }
  37. func Get() Config {
  38. return *cfg
  39. }
  40. func Address() string {
  41. return cfg.Address
  42. }
  43. func Tracing() TracingConfig {
  44. return cfg.Tracing
  45. }
  46. func Registry() RegistryConfig {
  47. return cfg.Registry
  48. }
  49. func RedisAddress() RedisConfig {
  50. return cfg.Redis
  51. }
  52. func Load() error {
  53. configor, err := config.NewConfig(config.WithSource(env.NewSource()))
  54. if err != nil {
  55. return errors.Wrap(err, "configor.New")
  56. }
  57. if err = configor.Load(); err != nil {
  58. return errors.Wrap(err, "configor.Load")
  59. }
  60. if err = configor.Scan(cfg); err != nil {
  61. return errors.Wrap(err, "configor.Scan")
  62. }
  63. return nil
  64. }