switch glauth to struct tag based env config

This commit is contained in:
Willy Kloucek
2021-12-16 19:24:07 +01:00
parent d0030ab555
commit 4067ae9493
16 changed files with 120 additions and 247 deletions

View File

@@ -5,6 +5,7 @@ import (
"net/http"
"github.com/owncloud/ocis/glauth/pkg/config"
"github.com/owncloud/ocis/glauth/pkg/logging"
"github.com/urfave/cli/v2"
)
@@ -17,7 +18,7 @@ func Health(cfg *config.Config) *cli.Command {
return ParseConfig(c, cfg)
},
Action: func(c *cli.Context) error {
logger := NewLogger(cfg)
logger := logging.Configure(cfg.Service.Name, cfg.Log)
resp, err := http.Get(
fmt.Sprintf(

View File

@@ -4,14 +4,13 @@ import (
"context"
"os"
"github.com/imdario/mergo"
"github.com/owncloud/ocis/glauth/pkg/config"
ociscfg "github.com/owncloud/ocis/ocis-pkg/config"
"github.com/owncloud/ocis/ocis-pkg/log"
oclog "github.com/owncloud/ocis/ocis-pkg/log"
"github.com/owncloud/ocis/ocis-pkg/shared"
"github.com/owncloud/ocis/ocis-pkg/version"
"github.com/thejerf/suture/v4"
"github.com/urfave/cli/v2"
"github.com/wkloucek/envdecode"
)
// Execute is the entry point for the ocis-glauth command.
@@ -50,34 +49,37 @@ func Execute(cfg *config.Config) error {
return app.Run(os.Args)
}
// NewLogger initializes a service-specific logger instance.
func NewLogger(cfg *config.Config) log.Logger {
return oclog.LoggerFromConfig("glauth", *cfg.Log)
}
// ParseConfig loads glauth configuration from known paths.
func ParseConfig(c *cli.Context, cfg *config.Config) error {
conf, err := ociscfg.BindSourcesToStructs("glauth", cfg)
_, err := ociscfg.BindSourcesToStructs("accounts", cfg)
if err != nil {
return err
}
// provide with defaults for shared logging, since we need a valid destination address for BindEnv.
if cfg.Log == nil && cfg.Commons != nil && cfg.Commons.Log != nil {
cfg.Log = &shared.Log{
Level: cfg.Commons.Log.Level,
Pretty: cfg.Commons.Log.Pretty,
Color: cfg.Commons.Log.Color,
File: cfg.Commons.Log.File,
}
} else if cfg.Log == nil && cfg.Commons == nil {
cfg.Log = &shared.Log{}
}
//if cfg.Log == nil && cfg.Commons != nil && cfg.Commons.Log != nil {
// cfg.Log = &shared.Log{
// Level: cfg.Commons.Log.Level,
// Pretty: cfg.Commons.Log.Pretty,
// Color: cfg.Commons.Log.Color,
// File: cfg.Commons.Log.File,
// }
//} else if cfg.Log == nil && cfg.Commons == nil {
// cfg.Log = &shared.Log{}
//}
// load all env variables relevant to the config in the current context.
conf.LoadOSEnv(config.GetEnv(cfg), false)
bindings := config.StructMappings(cfg)
return ociscfg.BindEnv(conf, bindings)
envCfg := config.Config{}
if err := envdecode.Decode(&envCfg); err != nil && err.Error() != "none of the target fields were set from environment variables" {
return err
}
// merge environment variable config on top of the current config
if err := mergo.Merge(cfg, envCfg, mergo.WithOverride); err != nil {
return err
}
return nil
}
// SutureService allows for the glauth command to be embedded and supervised by a suture supervisor tree.

View File

@@ -7,6 +7,7 @@ import (
"github.com/oklog/run"
accounts "github.com/owncloud/ocis/accounts/pkg/proto/v0"
"github.com/owncloud/ocis/glauth/pkg/config"
"github.com/owncloud/ocis/glauth/pkg/logging"
"github.com/owncloud/ocis/glauth/pkg/metrics"
"github.com/owncloud/ocis/glauth/pkg/server/debug"
"github.com/owncloud/ocis/glauth/pkg/server/glauth"
@@ -30,9 +31,9 @@ func Server(cfg *config.Config) *cli.Command {
return nil
},
Action: func(c *cli.Context) error {
logger := NewLogger(cfg)
if err := tracing.Configure(cfg); err != nil {
logger := logging.Configure(cfg.Service.Name, cfg.Log)
err := tracing.Configure(cfg)
if err != nil {
return err
}
@@ -47,7 +48,7 @@ func Server(cfg *config.Config) *cli.Command {
defer cancel()
metrics.BuildInfo.WithLabelValues(cfg.Version).Set(1)
metrics.BuildInfo.WithLabelValues(cfg.Service.Version).Set(1)
{