-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
133 lines (115 loc) · 3.23 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package config
import (
"fmt"
"os"
"time"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
)
// Build information -ldflags .
var (
Version string = "dev"
CommitHash string = "-"
)
// Project contains all parameters project information.
type Project struct {
Debug bool `yaml:"debug"`
Name string `yaml:"name"`
Environment string `yaml:"environment"`
SwaggerDir string `yaml:"swaggerDir"`
Version string
CommitHash string
}
// Database contains all parameters database connection.
type Database struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
User string `yaml:"user"`
Password string `yaml:"password"`
Name string `yaml:"name"`
SslMode string `yaml:"sslmode"`
MigrationsDir string `yaml:"migrationsDir"`
Driver string `yaml:"driver"`
}
// GRPC contains all parameters of gRPC.
type GRPC struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
MaxConnectionIdle time.Duration `yaml:"maxConnectionIdle"`
Timeout time.Duration `yaml:"timeout"`
MaxConnectionAge time.Duration `yaml:"maxConnectionAge"`
}
// REST contains all parameters of REST.
type REST struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
}
// Jaeger contains all parameters tracer information.
type Jaeger struct {
URL string `yaml:"url"`
Service string `yaml:"service"`
}
// Metrics contains all parameters metrics information.
type Metrics struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Path string `yaml:"path"`
}
// Status contains all parameters status information.
type Status struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
LivenessPath string `yaml:"livenessPath"`
ReadinessPath string `yaml:"readinessPath"`
VersionPath string `yaml:"versionPath"`
LoggerPath string `yaml:"loggerPath"`
}
// The Kafka producer contains all parameters Producer information.
type Producer struct {
Capacity int `yaml:"capacity"`
Topic string `yaml:"topic"`
Brokers []string `yaml:"brokers"`
}
// The Kafka consume contains all parameters Consume information.
type Consume struct {
Topic string `yaml:"topic"`
GroupID string `yaml:"groupId"`
Brokers []string `yaml:"brokers"`
}
// Config contains all configuration parameters in the config package.
type Config struct {
Project Project `yaml:"project"`
Database Database `yaml:"database"`
Logger zap.Config `yaml:"logger"`
GRPC GRPC `yaml:"grpc"`
REST REST `yaml:"rest"`
Jaeger Jaeger `yaml:"jaeger"`
Metrics Metrics `yaml:"metrics"`
Status Status `yaml:"status"`
Producer Producer `yaml:"producer"`
}
// String returns a data source name.
func (db *Database) String() string {
dsn := fmt.Sprintf("host=%v port=%v user=%v password=%v dbname=%v sslmode=%v",
db.Host,
db.Port,
db.User,
db.Password,
db.Name,
db.SslMode,
)
return dsn
}
// ReadConfigYML reads the config file into the config struct.
func ReadConfigYML(filename string, cfg interface{}) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
decoder := yaml.NewDecoder(file)
if err := decoder.Decode(cfg); err != nil {
return err
}
return nil
}