@@ -12,40 +12,48 @@ import (
12
12
13
13
// Config содержит настройки приложения, включая параметры сервера, базы данных и файлового хранилища.
14
14
type Config struct {
15
- ServerAddress string // Адрес и порт, на котором запускается сервер.
16
- BaseURL string // Базовый URL для сокращённых ссылок.
17
- FileStoragePath string // Путь к файлу для хранения сокращённых URL.
18
- DatabaseDSN string // Строка подключения к базе данных.
19
- EnableHTTPS bool // Включить HTTPS.
20
- CertFile string // Путь к файлу с сертификатом.
21
- KeyFile string // путь к файлу с ключом.
22
- DefaultServerAddress string // Значение по умолчанию для ServerAddress.
23
- DefaultBaseURL string // Значение по умолчанию для BaseURL.
24
- DefaultFileStoragePath string // Значение по умолчанию для FileStoragePath.
25
- DefaultDatabaseDSN string // Значение по умолчанию для DatabaseDSN.
26
- DefaultEnableHTTPS bool // Значение по умолчанию для EnableHTTPS.
27
- DefaultCertFile string // Значение по умолчанию для CertFile.
28
- DefaultKeyFile string // Значение по умолчанию для KeyFile.
15
+ ServerAddress string // Адрес и порт, на котором запускается сервер.
16
+ BaseURL string // Базовый URL для сокращённых ссылок.
17
+ FileStoragePath string // Путь к файлу для хранения сокращённых URL.
18
+ DatabaseDSN string // Строка подключения к базе данных.
19
+ EnableHTTPS bool // Включить HTTPS.
20
+ CertFile string // Путь к файлу с сертификатом.
21
+ KeyFile string // путь к файлу с ключом.
22
+ TrustedSubnet string // Доверенная подсеть в CIDR-формате.
23
+ GRPCServerAddress string // Адрес GRPC
24
+ DefaultServerAddress string // Значение по умолчанию для ServerAddress.
25
+ DefaultBaseURL string // Значение по умолчанию для BaseURL.
26
+ DefaultFileStoragePath string // Значение по умолчанию для FileStoragePath.
27
+ DefaultDatabaseDSN string // Значение по умолчанию для DatabaseDSN.
28
+ DefaultEnableHTTPS bool // Значение по умолчанию для EnableHTTPS.
29
+ DefaultCertFile string // Значение по умолчанию для CertFile.
30
+ DefaultKeyFile string // Значение по умолчанию для KeyFile.
31
+ DefaultTrustedSubnet string // Значение по умолчанию для TrustedSubnet.
32
+ DefaultGRPCServerAddress string // Значение по умолчанию для GRPCServerAddress.
29
33
}
30
34
31
35
type envConfig struct {
32
- ServerAddress string `env:"SERVER_ADDRESS"`
33
- BaseURL string `env:"BASE_URL"`
34
- FileStoragePath string `env:"FILE_STORAGE_PATH"`
35
- DatabaseDSN string `env:"DATABASE_DSN"`
36
- EnableHTTPS string `env:"ENABLE_HTTPS"`
37
- CertFile string `env:"TLS_CERT"`
38
- KeyFile string `env:"TLS_KEY"`
36
+ ServerAddress string `env:"SERVER_ADDRESS"`
37
+ BaseURL string `env:"BASE_URL"`
38
+ FileStoragePath string `env:"FILE_STORAGE_PATH"`
39
+ DatabaseDSN string `env:"DATABASE_DSN"`
40
+ EnableHTTPS string `env:"ENABLE_HTTPS"`
41
+ CertFile string `env:"TLS_CERT"`
42
+ KeyFile string `env:"TLS_KEY"`
43
+ TrustedSubnet string `env:"TRUSTED_SUBNET"`
44
+ GRPCServerAddress string `env:"GRPC_SERVER_ADDRESS"`
39
45
}
40
46
41
47
type jsonConfig struct {
42
- ServerAddress string `json:"server_address"`
43
- BaseURL string `json:"base_url"`
44
- FileStoragePath string `json:"file_storage_path"`
45
- DatabaseDSN string `json:"database_dsn"`
46
- EnableHTTPS bool `json:"enable_https"`
47
- CertFile string `json:"cert_file"`
48
- KeyFile string `json:"key_file"`
48
+ ServerAddress string `json:"server_address"`
49
+ BaseURL string `json:"base_url"`
50
+ FileStoragePath string `json:"file_storage_path"`
51
+ DatabaseDSN string `json:"database_dsn"`
52
+ EnableHTTPS bool `json:"enable_https"`
53
+ CertFile string `json:"cert_file"`
54
+ KeyFile string `json:"key_file"`
55
+ TrustedSubnet string `json:"trusted_subnet"`
56
+ GRPCServerAddress string `json:"grpc_server_address"`
49
57
}
50
58
51
59
// GetConfig возвращает экземпляр конфига
@@ -58,6 +66,8 @@ func GetConfig() Config {
58
66
cfg .DefaultEnableHTTPS = false
59
67
cfg .DefaultCertFile = ""
60
68
cfg .DefaultKeyFile = ""
69
+ cfg .DefaultTrustedSubnet = ""
70
+ cfg .DefaultGRPCServerAddress = "localhost:50051"
61
71
parseFlags (& cfg )
62
72
parsEnv (& cfg )
63
73
return cfg
@@ -106,7 +116,16 @@ func parseFlags(cfg *Config) {
106
116
} else {
107
117
cfg .KeyFile = cfg .DefaultKeyFile
108
118
}
109
-
119
+ if f := flag .Lookup ("t" ); f == nil {
120
+ flag .StringVar (& cfg .TrustedSubnet , "t" , cfg .DefaultTrustedSubnet , "trusted subnet CIDR" )
121
+ } else {
122
+ cfg .TrustedSubnet = cfg .DefaultTrustedSubnet
123
+ }
124
+ if f := flag .Lookup ("grpc" ); f == nil {
125
+ flag .StringVar (& cfg .GRPCServerAddress , "grpc" , cfg .DefaultGRPCServerAddress , "address and port for gRPC server" )
126
+ } else {
127
+ cfg .GRPCServerAddress = cfg .DefaultGRPCServerAddress
128
+ }
110
129
flag .Parse ()
111
130
parseJSON (configPath , cfg )
112
131
parsEnv (cfg )
@@ -140,6 +159,12 @@ func parsEnv(cfg *Config) {
140
159
if len (envCfg .KeyFile ) > 0 {
141
160
cfg .KeyFile = envCfg .KeyFile
142
161
}
162
+ if len (envCfg .TrustedSubnet ) > 0 {
163
+ cfg .TrustedSubnet = envCfg .TrustedSubnet
164
+ }
165
+ if len (envCfg .GRPCServerAddress ) > 0 {
166
+ cfg .GRPCServerAddress = envCfg .GRPCServerAddress
167
+ }
143
168
}
144
169
145
170
func parseJSON (path string , cfg * Config ) {
@@ -184,4 +209,10 @@ func parseJSON(path string, cfg *Config) {
184
209
if cfg .KeyFile == cfg .DefaultKeyFile && jCfg .KeyFile != "" {
185
210
cfg .KeyFile = jCfg .KeyFile
186
211
}
212
+ if cfg .TrustedSubnet == cfg .DefaultTrustedSubnet && jCfg .TrustedSubnet != "" {
213
+ cfg .TrustedSubnet = jCfg .TrustedSubnet
214
+ }
215
+ if cfg .GRPCServerAddress == cfg .DefaultGRPCServerAddress && jCfg .GRPCServerAddress != "" {
216
+ cfg .GRPCServerAddress = jCfg .GRPCServerAddress
217
+ }
187
218
}
0 commit comments