12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package config
- import (
- "fmt"
- "github.com/pkg/errors"
- "go-micro.dev/v4/config"
- "go-micro.dev/v4/config/source/env"
- )
- type Config struct {
- Port int
- Tracing TracingConfig
- Registry RegistryConfig
- Redis RedisConfig
- Database DatabaseConfig
- Service string
- }
- type TracingConfig struct {
- Enable bool
- Jaeger JaegerConfig
- }
- type JaegerConfig struct {
- URL string
- }
- type RegistryConfig struct {
- Enable bool
- Consul ConsulConfig
- }
- type ConsulConfig struct {
- URL string
- }
- type DatabaseConfig struct {
- Enable bool
- Mysql MysqlConfig
- }
- type RedisConfig struct {
- Enable bool
- URL string
- Password string
- }
- type MysqlConfig struct {
- User string
- Password string
- Host string
- Port int
- DataBase string
- }
- var cfg *Config = &Config{
- Port: 50052,
- Service: "shoppingservice",
- }
- func Address() string {
- return fmt.Sprintf(":%d", cfg.Port)
- }
- func ShoppingServiceName() string {
- return cfg.Service
- }
- func Tracing() TracingConfig {
- return cfg.Tracing
- }
- func Registry() RegistryConfig {
- return cfg.Registry
- }
- func DataBase() DatabaseConfig {
- return cfg.Database
- }
- func RedisAddress() RedisConfig {
- return cfg.Redis
- }
- func Load() error {
- configor, err := config.NewConfig(config.WithSource(env.NewSource()))
- if err != nil {
- return errors.Wrap(err, "configor.New")
- }
- if err = configor.Load(); err != nil {
- return errors.Wrap(err, "configor.Load")
- }
- if err = configor.Scan(cfg); err != nil {
- return errors.Wrap(err, "configor.Scan")
- }
- return nil
- }
|