config.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. }
  15. type TracingConfig struct {
  16. Enable bool
  17. Jaeger JaegerConfig
  18. }
  19. type JaegerConfig struct {
  20. URL string
  21. }
  22. type RegistryConfig struct {
  23. Enable bool
  24. Consul ConsulConfig
  25. }
  26. type ConsulConfig struct {
  27. URL string
  28. }
  29. type DatabaseConfig struct {
  30. Enable bool
  31. Mysql MysqlConfig
  32. }
  33. type RedisConfig struct {
  34. Enable bool
  35. URL string
  36. Password string
  37. }
  38. type MysqlConfig struct {
  39. User string
  40. Password string
  41. Host string
  42. Port int
  43. DataBase string
  44. }
  45. var cfg *Config = &Config{
  46. Port: 50053,
  47. }
  48. func Address() string {
  49. return fmt.Sprintf(":%d", cfg.Port)
  50. }
  51. func Tracing() TracingConfig {
  52. return cfg.Tracing
  53. }
  54. func Registry() RegistryConfig {
  55. return cfg.Registry
  56. }
  57. func DataBase() DatabaseConfig {
  58. return cfg.Database
  59. }
  60. func RedisAddress() RedisConfig {
  61. return cfg.Redis
  62. }
  63. func Load() error {
  64. configor, err := config.NewConfig(config.WithSource(env.NewSource()))
  65. if err != nil {
  66. return errors.Wrap(err, "configor.New")
  67. }
  68. if err := configor.Load(); err != nil {
  69. return errors.Wrap(err, "configor.Load")
  70. }
  71. if err := configor.Scan(cfg); err != nil {
  72. return errors.Wrap(err, "configor.Scan")
  73. }
  74. return nil
  75. }