Add listen-domains

- add listen-domains config key
- add method to fetch domains list to listen

Signed-off-by: Evgeniy Kulikov <kim@nspcc.ru>
This commit is contained in:
Evgeniy Kulikov 2020-12-10 18:11:45 +03:00
parent 4cf1a207ef
commit 793925497a
2 changed files with 30 additions and 1 deletions

View file

@ -75,6 +75,7 @@ const ( // settings
cfgEnableMetrics = "metrics" cfgEnableMetrics = "metrics"
cfgEnableProfiler = "pprof" cfgEnableProfiler = "pprof"
cfgListenAddress = "listen_address" cfgListenAddress = "listen_address"
cfgListenDomains = "listen_domains"
// Peers // Peers
cfgPeers = "peers" cfgPeers = "peers"
@ -127,6 +128,21 @@ func fetchPeers(l *zap.Logger, v *viper.Viper) map[string]float64 {
return peers return peers
} }
func fetchDomains(v *viper.Viper) []string {
cnt := v.GetInt(cfgListenDomains + ".count")
res := make([]string, 0, cnt)
for i := 0; ; i++ {
domain := v.GetString(cfgListenDomains + "." + strconv.Itoa(i))
if domain == "" {
break
}
res = append(res, domain)
}
return res
}
func newSettings() *viper.Viper { func newSettings() *viper.Viper {
v := viper.New() v := viper.New()
@ -162,6 +178,8 @@ func newSettings() *viper.Viper {
flags.String(cfgListenAddress, "0.0.0.0:8080", "set address to listen") flags.String(cfgListenAddress, "0.0.0.0:8080", "set address to listen")
peers := flags.StringArrayP(cfgPeers, "p", nil, "set NeoFS nodes") peers := flags.StringArrayP(cfgPeers, "p", nil, "set NeoFS nodes")
domains := flags.StringArrayP(cfgListenDomains, "d", nil, "set domains to be listened")
// set prefers: // set prefers:
v.Set(cfgApplicationName, misc.ApplicationName) v.Set(cfgApplicationName, misc.ApplicationName)
v.Set(cfgApplicationVersion, misc.Version) v.Set(cfgApplicationVersion, misc.Version)
@ -203,6 +221,14 @@ func newSettings() *viper.Viper {
} }
} }
if domains != nil && len(*domains) > 0 {
for i := range *domains {
v.SetDefault(cfgListenDomains+"."+strconv.Itoa(i), (*domains)[i])
}
v.SetDefault(cfgListenDomains+".count", len(*domains))
}
switch { switch {
case help != nil && *help: case help != nil && *help:
fmt.Printf("NeoFS S3 Gateway %s (%s)\n", misc.Version, misc.Build) fmt.Printf("NeoFS S3 Gateway %s (%s)\n", misc.Version, misc.Build)

View file

@ -214,7 +214,10 @@ func (a *App) Server(ctx context.Context) {
attachProfiler(router, a.cfg, a.log) attachProfiler(router, a.cfg, a.log)
// Attach S3 API: // Attach S3 API:
api.Attach(router, a.maxClients, a.api, a.ctr, a.log) domains := fetchDomains(a.cfg)
a.log.Info("fetch domains, prepare to use API",
zap.Strings("domains", domains))
api.Attach(router, domains, a.maxClients, a.api, a.ctr, a.log)
// Use mux.Router as http.Handler // Use mux.Router as http.Handler
srv.Handler = router srv.Handler = router