/
config_service.go
83 lines (63 loc) · 2.37 KB
/
config_service.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
// Copyright 2019 Axel Etcheverry. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package container
import (
"flag"
"os"
"strings"
"time"
service "github.com/euskadi31/go-service"
"github.com/hyperscale/hypercloud/cmd/hypercloud-installer/app/config"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)
// Services keys
const (
ConfigKey = "service.config"
)
const name = "hypercloud-installer"
func init() {
service.Set(ConfigKey, func(c service.Container) interface{} {
cmd := c.Get(FlagsKey).(*flag.FlagSet)
var cfgFile string
cmd.StringVar(&cfgFile, "config", "", "config file (default is $HOME/config.yaml)")
// Ignore errors; cmd is set for ExitOnError.
// nolint:gosec
_ = cmd.Parse(os.Args[1:])
cfg := config.NewConfiguration()
options := viper.New()
options.SetDefault("logger.level", "info")
options.SetDefault("logger.prefix", name)
options.SetDefault("server.http.host", "")
options.SetDefault("server.http.port", 8080)
options.SetDefault("server.profiling", true)
options.SetDefault("server.metrics", true)
options.SetDefault("server.healthcheck", true)
options.SetDefault("server.shutdown_timeout", 10*time.Second)
options.SetDefault("server.write_timeout", 0)
options.SetDefault("server.read_timeout", 10*time.Second)
options.SetDefault("server.read_header_timeout", 10*time.Millisecond)
options.SetConfigName("config") // name of config file (without extension)
options.AddConfigPath("/etc/" + name + "/") // path to look for the config file in
options.AddConfigPath("$HOME/." + name + "/") // call multiple times to add many search paths
options.AddConfigPath(".")
if cfgFile != "" { // enable ability to specify config file via flag
options.SetConfigFile(cfgFile)
}
if port := os.Getenv("PORT"); port != "" {
os.Setenv("HYPERCLOUD_INSTALLER_SERVER_HTTP_PORT", port)
}
options.SetEnvPrefix("HYPERCLOUD_INSTALLER")
options.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
options.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := options.ReadInConfig(); err == nil {
log.Info().Msgf("Using config file: %s", options.ConfigFileUsed())
}
if err := options.Unmarshal(cfg); err != nil {
log.Fatal().Err(err).Msg(ConfigKey)
}
return cfg // *config.Configuration
})
}