-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
206 lines (167 loc) · 5.95 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Package config - Configuration for Cloud-Barista's GRPC and provides the required process
package config
import (
"fmt"
"log"
"os"
"strings"
"time"
)
// ===== [ Constants and Variables ] =====
const (
// ConfigVersion - 설정 구조에 대한 버전
ConfigVersion = 1
)
// ===== [ Types ] =====
// GrpcConfig - CB-GRPC 서비스 설정 구조
type GrpcConfig struct {
Version int `mapstructure:"version"`
GSL GrpcServiceList `mapstructure:"grpc"`
}
// GrpcServiceList - CB-GRPC 서비스 목록
type GrpcServiceList struct {
LadybugSrv *GrpcServerConfig `mapstructure:"ladybugsrv"`
LadybugCli *GrpcClientConfig `mapstructure:"ladybugcli"`
}
// GrpcServerConfig - CB-GRPC 서버 설정 구조
type GrpcServerConfig struct {
Addr string `mapstructure:"addr"`
Reflection string `mapstructure:"reflection"`
TLS *TLSConfig `mapstructure:"tls"`
Interceptors *InterceptorsConfig `mapstructure:"interceptors"`
}
// GrpcClientConfig - CB-GRPC 클라이언트 설정 구조
type GrpcClientConfig struct {
ServerAddr string `mapstructure:"server_addr"`
Timeout time.Duration `mapstructure:"timeout"`
TLS *TLSConfig `mapstructure:"tls"`
Interceptors *InterceptorsConfig `mapstructure:"interceptors"`
}
// TLSConfig - TLS 설정 구조
type TLSConfig struct {
TLSCert string `mapstructure:"tls_cert"`
TLSKey string `mapstructure:"tls_key"`
TLSCA string `mapstructure:"tls_ca"`
}
// InterceptorsConfig - GRPC 인터셉터 설정 구조
type InterceptorsConfig struct {
AuthJWT *AuthJWTConfig `mapstructure:"auth_jwt"`
PrometheusMetrics *PrometheusMetricsConfig `mapstructure:"prometheus_metrics"`
Opentracing *OpentracingConfig `mapstructure:"opentracing"`
}
// AuthJWTConfig - AuthJWT 설정 구조
type AuthJWTConfig struct {
JWTKey string `mapstructure:"jwt_key"`
JWTToken string `mapstructure:"jwt_token"`
}
// PrometheusMetricsConfig - Prometheus Metrics 설정 구조
type PrometheusMetricsConfig struct {
ListenPort int `mapstructure:"listen_port"`
}
// OpentracingConfig - Opentracing 설정 구조
type OpentracingConfig struct {
Jaeger *JaegerClientConfig `mapstructure:"jaeger"`
}
// JaegerClientConfig - Jaeger Client 설정 구조
type JaegerClientConfig struct {
Endpoint string `mapstructure:"endpoint"`
ServiceName string `mapstructure:"service_name"`
SampleRate float64 `mapstructure:"sample_rate"`
}
// UnsupportedVersionError - 설정 초기화 과정에서 버전 검증을 통해 반환할 오류 구조
type UnsupportedVersionError struct {
Have int
Want int
}
// ===== [ Implementations ] =====
// Init - 설정에 대한 검사 및 초기화
func (gConf *GrpcConfig) Init() error {
// 설정 파일 버전 검증
if gConf.Version != ConfigVersion {
return &UnsupportedVersionError{
Have: gConf.Version,
Want: ConfigVersion,
}
}
// 전역변수 초기화
gConf.initGlobalParams()
return nil
}
// initGlobalParams - 전역 설정 초기화
func (gConf *GrpcConfig) initGlobalParams() {
if gConf.GSL.LadybugSrv != nil {
if gConf.GSL.LadybugSrv.TLS != nil {
if gConf.GSL.LadybugSrv.TLS.TLSCert != "" {
gConf.GSL.LadybugSrv.TLS.TLSCert = ReplaceEnvPath(gConf.GSL.LadybugSrv.TLS.TLSCert)
}
if gConf.GSL.LadybugSrv.TLS.TLSKey != "" {
gConf.GSL.LadybugSrv.TLS.TLSKey = ReplaceEnvPath(gConf.GSL.LadybugSrv.TLS.TLSKey)
}
}
if gConf.GSL.LadybugSrv.Interceptors != nil {
if gConf.GSL.LadybugSrv.Interceptors.Opentracing != nil {
if gConf.GSL.LadybugSrv.Interceptors.Opentracing.Jaeger != nil {
if gConf.GSL.LadybugSrv.Interceptors.Opentracing.Jaeger.ServiceName == "" {
gConf.GSL.LadybugSrv.Interceptors.Opentracing.Jaeger.ServiceName = "grpc ladybug server"
}
if gConf.GSL.LadybugSrv.Interceptors.Opentracing.Jaeger.SampleRate == 0 {
gConf.GSL.LadybugSrv.Interceptors.Opentracing.Jaeger.SampleRate = 1
}
}
}
}
}
if gConf.GSL.LadybugCli != nil {
if gConf.GSL.LadybugCli.Timeout == 0 {
gConf.GSL.LadybugCli.Timeout = 90 * time.Second
}
if gConf.GSL.LadybugCli.TLS != nil {
if gConf.GSL.LadybugCli.TLS.TLSCA != "" {
gConf.GSL.LadybugCli.TLS.TLSCA = ReplaceEnvPath(gConf.GSL.LadybugCli.TLS.TLSCA)
}
}
if gConf.GSL.LadybugCli.Interceptors != nil {
if gConf.GSL.LadybugCli.Interceptors.Opentracing != nil {
if gConf.GSL.LadybugCli.Interceptors.Opentracing.Jaeger != nil {
if gConf.GSL.LadybugCli.Interceptors.Opentracing.Jaeger.ServiceName == "" {
gConf.GSL.LadybugCli.Interceptors.Opentracing.Jaeger.ServiceName = "grpc dragonfly client"
}
if gConf.GSL.LadybugCli.Interceptors.Opentracing.Jaeger.SampleRate == 0 {
gConf.GSL.LadybugCli.Interceptors.Opentracing.Jaeger.SampleRate = 1
}
}
}
}
}
}
// Error - 비 호환 버전에 대한 오류 문자열 반환
func (u *UnsupportedVersionError) Error() string {
return fmt.Sprintf("Unsupported version: %d (wanted: %d)", u.Have, u.Want)
}
// ===== [ Private Functions ] =====
// ===== [ Public Functions ] =====
// ReplaceEnvPath - $ABC/def ==> /abc/def
func ReplaceEnvPath(str string) string {
if strings.Index(str, "$") == -1 {
return str
}
// ex) input "$CBSTORE_ROOT/meta_db/dat"
strList := strings.Split(str, "/")
for n, one := range strList {
if strings.Index(one, "$") != -1 {
cbstoreRootPath := os.Getenv(strings.Trim(one, "$"))
if cbstoreRootPath == "" {
log.Fatal(one + " is not set!")
}
strList[n] = cbstoreRootPath
}
}
var resultStr string
for _, one := range strList {
resultStr = resultStr + one + "/"
}
// ex) "/root/go/src/github.com/cloud-barista/cb-spider/meta_db/dat/"
resultStr = strings.TrimRight(resultStr, "/")
resultStr = strings.ReplaceAll(resultStr, "//", "/")
return resultStr
}