-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
119 lines (102 loc) · 3.9 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package config
import (
"flag"
"fmt"
"os"
"github.com/ashkan-maleki/go-es-cqrs/pkg/constants"
"github.com/ashkan-maleki/go-es-cqrs/pkg/elasticsearch"
"github.com/ashkan-maleki/go-es-cqrs/pkg/eventstroredb"
"github.com/ashkan-maleki/go-es-cqrs/pkg/logger"
"github.com/ashkan-maleki/go-es-cqrs/pkg/mongodb"
"github.com/ashkan-maleki/go-es-cqrs/pkg/probes"
"github.com/ashkan-maleki/go-es-cqrs/pkg/tracing"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
var configPath string
func init() {
flag.StringVar(&configPath, "config", "", "ES microservice config path")
}
type Config struct {
ServiceName string `mapstructure:"serviceName"`
Logger *logger.Config `mapstructure:"logger"`
GRPC GRPC `mapstructure:"grpc"`
Mongo *mongodb.Config `mapstructure:"mongo"`
MongoCollections MongoCollections `mapstructure:"mongoCollections"`
Probes probes.Config `mapstructure:"probes"`
Jaeger *tracing.Config `mapstructure:"jaeger"`
EventStoreConfig eventstroredb.EventStoreConfig `mapstructure:"eventStoreConfig"`
Subscriptions Subscriptions `mapstructure:"subscriptions"`
Elastic elasticsearch.Config `mapstructure:"elastic"`
ElasticIndexes ElasticIndexes `mapstructure:"elasticIndexes"`
Http Http `mapstructure:"http"`
}
type GRPC struct {
Port string `mapstructure:"port"`
Development bool `mapstructure:"development"`
}
type MongoCollections struct {
Orders string `mapstructure:"orders" validate:"required"`
}
type Subscriptions struct {
PoolSize int `mapstructure:"poolSize" validate:"required,gte=0"`
OrderPrefix string `mapstructure:"orderPrefix" validate:"required,gte=0"`
MongoProjectionGroupName string `mapstructure:"mongoProjectionGroupName" validate:"required,gte=0"`
ElasticProjectionGroupName string `mapstructure:"elasticProjectionGroupName" validate:"required,gte=0"`
}
type ElasticIndexes struct {
Orders string `mapstructure:"orders" validate:"required"`
}
type Http struct {
Port string `mapstructure:"port" validate:"required"`
Development bool `mapstructure:"development"`
BasePath string `mapstructure:"basePath" validate:"required"`
OrdersPath string `mapstructure:"ordersPath" validate:"required"`
DebugErrorsResponse bool `mapstructure:"debugErrorsResponse"`
IgnoreLogUrls []string `mapstructure:"ignoreLogUrls"`
}
func InitConfig() (*Config, error) {
if configPath == "" {
configPathFromEnv := os.Getenv(constants.ConfigPath)
if configPathFromEnv != "" {
configPath = configPathFromEnv
} else {
getwd, err := os.Getwd()
if err != nil {
return nil, errors.Wrap(err, "os.Getwd")
}
configPath = fmt.Sprintf("%s/config/config.yaml", getwd)
}
}
cfg := &Config{}
viper.SetConfigType(constants.Yaml)
viper.SetConfigFile(configPath)
if err := viper.ReadInConfig(); err != nil {
return nil, errors.Wrap(err, "viper.ReadInConfig")
}
if err := viper.Unmarshal(cfg); err != nil {
return nil, errors.Wrap(err, "viper.Unmarshal")
}
grpcPort := os.Getenv(constants.GrpcPort)
if grpcPort != "" {
cfg.GRPC.Port = grpcPort
}
mongoURI := os.Getenv(constants.MongoDbURI)
if mongoURI != "" {
//cfg.Mongo.URI = "mongodb://host.docker.internal:27017"
cfg.Mongo.URI = mongoURI
}
jaegerAddr := os.Getenv(constants.JaegerHostPort)
if jaegerAddr != "" {
cfg.Jaeger.HostPort = jaegerAddr
}
eventStoreConnectionString := os.Getenv(constants.EventStoreConnectionString)
if eventStoreConnectionString != "" {
cfg.EventStoreConfig.ConnectionString = eventStoreConnectionString
}
elasticUrl := os.Getenv(constants.ElasticUrl)
if elasticUrl != "" {
cfg.Elastic.URL = elasticUrl
}
return cfg, nil
}