config.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package config
  2. import (
  3. "fmt"
  4. "github.com/pkg/errors"
  5. "go-micro.dev/v4/config"
  6. "go-micro.dev/v4/config/source/env"
  7. )
  8. type Config struct {
  9. Port int
  10. Tracing TracingConfig
  11. Registry RegistryConfig
  12. Redis RedisConfig
  13. Database DatabaseConfig
  14. Service string
  15. }
  16. type TracingConfig struct {
  17. Enable bool
  18. Jaeger JaegerConfig
  19. }
  20. type JaegerConfig struct {
  21. URL string
  22. }
  23. type RegistryConfig struct {
  24. Enable bool
  25. Consul ConsulConfig
  26. }
  27. type ConsulConfig struct {
  28. URL string
  29. }
  30. type DatabaseConfig struct {
  31. Enable bool
  32. Mysql MysqlConfig
  33. }
  34. type RedisConfig struct {
  35. Enable bool
  36. URL string
  37. Password string
  38. }
  39. type MysqlConfig struct {
  40. User string
  41. Password string
  42. Host string
  43. Port int
  44. DataBase string
  45. }
  46. var cfg *Config = &Config{
  47. Port: 50052,
  48. Service: "shoppingservice",
  49. }
  50. func Address() string {
  51. return fmt.Sprintf(":%d", cfg.Port)
  52. }
  53. func ShoppingServiceName() string {
  54. return cfg.Service
  55. }
  56. func Tracing() TracingConfig {
  57. return cfg.Tracing
  58. }
  59. func Registry() RegistryConfig {
  60. return cfg.Registry
  61. }
  62. func DataBase() DatabaseConfig {
  63. return cfg.Database
  64. }
  65. func RedisAddress() RedisConfig {
  66. return cfg.Redis
  67. }
  68. func Load() error {
  69. configor, err := config.NewConfig(config.WithSource(env.NewSource()))
  70. if err != nil {
  71. return errors.Wrap(err, "configor.New")
  72. }
  73. if err = configor.Load(); err != nil {
  74. return errors.Wrap(err, "configor.Load")
  75. }
  76. if err = configor.Scan(cfg); err != nil {
  77. return errors.Wrap(err, "configor.Scan")
  78. }
  79. return nil
  80. }