forked from SkySoft-ATM/gorillaz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
87 lines (77 loc) · 2.87 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
package gorillaz
import (
"flag"
"log"
"os"
"path"
"strings"
"github.com/spf13/pflag"
)
const defaultConfigPath = "configs"
//Define flags supported by gorillaz
func init() {
flag.String("env", "dev", "Environment")
flag.String("conf", defaultConfigPath, "config folder. default: configs")
flag.String("log.level", "", "Log level")
flag.String("service.name", "", "Service name")
flag.String("service.address", "", "Service address")
flag.Bool("tracing.enabled", false, "Tracing enabled")
flag.String("tracing.collector.url", "", "URL of the tracing service")
flag.Bool("healthcheck.enabled", true, "Healthcheck enabled")
flag.Bool("pprof.enabled", false, "Pprof enabled")
flag.Int("pprof.port", 0, "pprof port")
flag.String("prometheus.endpoint", "/metrics", "Prometheus endpoint")
flag.Bool("prometheus.enabled", true, "Prometheus enabled")
flag.Int("http.port", 0, "http port")
flag.String("https.crt", "", "path to http tls server certificate")
flag.String("https.key", "", "path to http tls server private key")
flag.Int("grpc.port", 0, "grpc port")
flag.Int("metrics.publication.interval.ms", 400, "interval of prometheus metrics publication over gRPC stream")
flag.String("nats.addr", "", "nats broker address")
flag.Bool("nats.add.env.prefix", true, "configure whether or not the nats subjects should be prefixed by the gorillaz env")
flag.Uint64("nats.connect_timeout_ms", 5000, "nats connection timeout ms")
flag.Uint64("nats.ping_interval_ms", 5000, "nats ping interval ms")
flag.Int("nats.max_ping_outstanding", 3, "nats max outstanding ping before disconnection")
flag.String("nats.ca", "", "nats root ca file list")
flag.String("nats.crt", "", "nats client TLS certificate")
flag.String("nats.key", "", "nats client TLS certificate")
}
func parseConfiguration(g *Gaz, configPath string) {
conf := GetConfigPath(configPath)
const configFilePrefix = "application"
g.Viper.SetConfigName(configFilePrefix) //the suffix ".properties" will be added by viper
g.Viper.AddConfigPath(conf)
g.Viper.SetConfigType("properties")
err := g.Viper.ReadInConfig()
if err != nil {
Sugar.Warnf("unable to read config in path %s with file prefix %s %v", conf, configFilePrefix, err)
}
if g.bindConfigKeysAsFlag {
for _, k := range g.Viper.AllKeys() {
if flag.Lookup(k) == nil {
flag.String(k, g.Viper.GetString(k), "flag generated by gorillaz from config file")
}
}
}
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
err = g.Viper.BindPFlags(pflag.CommandLine)
if err != nil {
log.Fatalf("unable to bind flags: %v", err)
}
}
func GetConfigPath(configPath string) string {
if configPath != "" {
return configPath
}
args := os.Args[1:]
for i, a := range args {
if strings.HasPrefix(a, "--conf=") {
return path.Clean(a[7:])
}
if strings.HasPrefix(a, "--conf") && len(args) > (i+1) {
return path.Clean(args[i+1])
}
}
return defaultConfigPath
}