Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Commit

Permalink
Load config from Viper on Before hook (#51)
Browse files Browse the repository at this point in the history
* abstract ParseConfig to its own scope

* remove hardcoded values

* parse config from root if present

* update example.json

* fix linter

* code style
  • Loading branch information
refs authored Mar 17, 2020
1 parent 439cc7d commit c16003d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 46 deletions.
3 changes: 2 additions & 1 deletion config/example.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
},
"http": {
"addr": "0.0.0.0:9130",
"root": "/"
"root": "/",
"tls": true
},
"tracing": {
"enabled": false,
Expand Down
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ github.com/longsleep/go-metrics v0.0.0-20191013204616-cddea569b0ea/go.mod h1:w6Q
github.com/lucas-clemente/quic-go v0.12.1/go.mod h1:UXJJPE4RfFef/xPO5wQm0tITK8gNfqwTxjbE7s3Vb8s=
github.com/lucas-clemente/quic-go v0.13.1 h1:CxtJTXQIh2aboCPk0M6vf530XOov6DZjVBiSE3nSj8s=
github.com/lucas-clemente/quic-go v0.13.1/go.mod h1:Vn3/Fb0/77b02SGhQk36KzOUmXgVpFfizUfW5WMaqyU=
github.com/lucas-clemente/quic-go v0.14.1 h1:c1aKoBZKOPA+49q96B1wGkibyPP0AxYh45WuAoq+87E=
github.com/lucas-clemente/quic-go v0.14.1/go.mod h1:Vn3/Fb0/77b02SGhQk36KzOUmXgVpFfizUfW5WMaqyU=
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
Expand Down Expand Up @@ -481,8 +482,6 @@ github.com/oracle/oci-go-sdk v7.0.0+incompatible/go.mod h1:VQb79nF8Z2cwLkLS35ukw
github.com/orcaman/concurrent-map v0.0.0-20190826125027-8c72a8bb44f6 h1:lNCW6THrCKBiJBpz8kbVGjC7MgdCGKwuvBgc7LoD6sw=
github.com/orcaman/concurrent-map v0.0.0-20190826125027-8c72a8bb44f6/go.mod h1:Lu3tH6HLW3feq74c2GC+jIMS/K2CFcDWnWD9XkenwhI=
github.com/ovh/go-ovh v0.0.0-20181109152953-ba5adb4cf014/go.mod h1:joRatxRJaZBsY3JAOEMcoOp05CnZzsx4scTxi95DHyQ=
github.com/owncloud/ocis-pkg/v2 v2.0.3-0.20200309135548-7e0d3cab3467 h1:7uOmEVi7tiwth1A34F3vk5eaGLDGnJyxgnptpVj8Vbo=
github.com/owncloud/ocis-pkg/v2 v2.0.3-0.20200309135548-7e0d3cab3467/go.mod h1:TrBRa+D8mUTsl+qvQiIksJbUvxdE/Qq9jEHUcERPQ60=
github.com/owncloud/ocis-pkg/v2 v2.0.3-0.20200309150924-5c659fd4b0ad h1:R6JGg68GP8bzzl3L5xso8n9ifay5KUfz2XMQ1KS0pbI=
github.com/owncloud/ocis-pkg/v2 v2.0.3-0.20200309150924-5c659fd4b0ad/go.mod h1:TrBRa+D8mUTsl+qvQiIksJbUvxdE/Qq9jEHUcERPQ60=
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw=
Expand Down
83 changes: 44 additions & 39 deletions pkg/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,45 +32,7 @@ func Execute() error {
Flags: flagset.RootWithConfig(cfg),

Before: func(c *cli.Context) error {
logger := NewLogger(cfg)

viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("KONNECTD")
viper.AutomaticEnv()

if c.IsSet("config-file") {
viper.SetConfigFile(c.String("config-file"))
} else {
viper.SetConfigName("konnectd")

viper.AddConfigPath("/etc/ocis")
viper.AddConfigPath("$HOME/.ocis")
viper.AddConfigPath("./config")
}

if err := viper.ReadInConfig(); err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
logger.Info().
Msg("Continue without config")
case viper.UnsupportedConfigError:
logger.Fatal().
Err(err).
Msg("Unsupported config type")
default:
logger.Fatal().
Err(err).
Msg("Failed to read config")
}
}

if err := viper.Unmarshal(&cfg); err != nil {
logger.Fatal().
Err(err).
Msg("Failed to parse config")
}

return nil
return ParseConfig(c, cfg)
},

Commands: []*cli.Command{
Expand Down Expand Up @@ -101,3 +63,46 @@ func NewLogger(cfg *config.Config) log.Logger {
log.Color(cfg.Log.Color),
)
}

// ParseConfig load configuration for every extension
func ParseConfig(c *cli.Context, cfg *config.Config) error {
logger := NewLogger(cfg)

viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("KONNECTD")
viper.AutomaticEnv()

if c.IsSet("config-file") {
viper.SetConfigFile(c.String("config-file"))
} else {
viper.SetConfigName("konnectd")

viper.AddConfigPath("/etc/ocis")
viper.AddConfigPath("$HOME/.ocis")
viper.AddConfigPath("./config")
}

if err := viper.ReadInConfig(); err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
logger.Info().
Msg("Continue without config")
case viper.UnsupportedConfigError:
logger.Fatal().
Err(err).
Msg("Unsupported config type")
default:
logger.Fatal().
Err(err).
Msg("Failed to read config")
}
}

if err := viper.Unmarshal(&cfg); err != nil {
logger.Fatal().
Err(err).
Msg("Failed to parse config")
}

return nil
}
7 changes: 4 additions & 3 deletions pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func Server(cfg *config.Config) *cli.Command {
Usage: "Start integrated server",
Flags: flagset.ServerWithConfig(cfg),
Before: func(c *cli.Context) error {

if cfg.HTTP.Root != "/" {
cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/")
}
Expand All @@ -48,7 +49,7 @@ func Server(cfg *config.Config) *cli.Command {
cfg.Konnectd.SigningPrivateKeyFiles = c.StringSlice("signing-private-key")
}

return nil
return ParseConfig(c, cfg)
},
Action: func(c *cli.Context) error {
logger := NewLogger(cfg)
Expand Down Expand Up @@ -155,8 +156,8 @@ func Server(cfg *config.Config) *cli.Command {
http.Context(ctx),
http.Config(cfg),
http.Metrics(metrics),
http.Flags(flagset.RootWithConfig(cfg)),
http.Flags(flagset.ServerWithConfig(cfg)),
http.Flags(flagset.RootWithConfig(config.New())),
http.Flags(flagset.ServerWithConfig(config.New())),
)

if err != nil {
Expand Down
1 change: 0 additions & 1 deletion pkg/server/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func Server(opts ...Option) (http.Service, error) {
http.TLSConfig(tlsConfig),
)

options.Config.Konnectd.Listen = options.Config.HTTP.Addr
handle := svc.NewService(
svc.Logger(options.Logger),
svc.Config(options.Config),
Expand Down

0 comments on commit c16003d

Please sign in to comment.