config.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. }
  32. var cfg *Config = &Config{
  33. Address: ":8080",
  34. AuthorizationService: "authorizationservice",
  35. }
  36. func Get() Config {
  37. return *cfg
  38. }
  39. func Address() string {
  40. return cfg.Address
  41. }
  42. func Tracing() TracingConfig {
  43. return cfg.Tracing
  44. }
  45. func Registry() RegistryConfig {
  46. return cfg.Registry
  47. }
  48. func RedisAddress() RedisConfig {
  49. return cfg.Redis
  50. }
  51. func Load() error {
  52. configor, err := config.NewConfig(config.WithSource(env.NewSource()))
  53. if err != nil {
  54. return errors.Wrap(err, "configor.New")
  55. }
  56. if err = configor.Load(); err != nil {
  57. return errors.Wrap(err, "configor.Load")
  58. }
  59. if err = configor.Scan(cfg); err != nil {
  60. return errors.Wrap(err, "configor.Scan")
  61. }
  62. return nil
  63. }