Skip to content

Commit

Permalink
switch all other services to struct tag based env config
Browse files Browse the repository at this point in the history
  • Loading branch information
wkloucek committed Jan 3, 2022
1 parent 30656c5 commit ee57288
Show file tree
Hide file tree
Showing 115 changed files with 1,022 additions and 1,988 deletions.
1 change: 1 addition & 0 deletions accounts/pkg/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func Execute(cfg *config.Config) error {
Email: "[email protected]",
},
},

Before: func(c *cli.Context) error {
cfg.Service.Version = version.String
return ParseConfig(c, cfg)
Expand Down
4 changes: 2 additions & 2 deletions accounts/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func Server(cfg *config.Config) *cli.Command {
cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/")
}

cfg.Repo.Backend = strings.ToLower(cfg.Repo.Backend)

if err := ParseConfig(ctx, cfg); err != nil {
return err
}

cfg.Repo.Backend = strings.ToLower(cfg.Repo.Backend)

return nil
},
Action: func(c *cli.Context) error {
Expand Down
43 changes: 26 additions & 17 deletions accounts/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import (
"github.com/owncloud/ocis/ocis-pkg/config/defaults"
)

//TODO: use debug config
// Debug defines the available debug configuration.
type Debug struct {
Addr string `ocisConfig:"addr" env:"ACCOUNTS_DEBUG_ADDR"`
Token string `ocisConfig:"token" env:"ACCOUNTS_DEBUG_TOKEN"`
Pprof bool `ocisConfig:"pprof" env:"ACCOUNTS_DEBUG_PPROF"`
Zpages bool `ocisConfig:"zpages" env:"ACCOUNTS_DEBUG_ZPAGES"`
}

// CORS defines the available cors configuration.
type CORS struct {
AllowedOrigins []string `ocisConfig:"allowed_origins"`
Expand Down Expand Up @@ -112,28 +121,28 @@ type Log struct {
type Config struct {
//*shared.Commons

HTTP HTTP `ocisConfig:"http"`
GRPC GRPC `ocisConfig:"grpc"`
Service Service `ocisConfig:"service"`
Asset Asset `ocisConfig:"asset"`
Log Log `ocisConfig:"log"`
TokenManager TokenManager `ocisConfig:"token_manager"`
Repo Repo `ocisConfig:"repo"`
Index Index `ocisConfig:"index"`
ServiceUser ServiceUser `ocisConfig:"service_user"`
HashDifficulty int `ocisConfig:"hash_difficulty" env:"ACCOUNTS_HASH_DIFFICULTY"`
DemoUsersAndGroups bool `ocisConfig:"demo_users_and_groups" env:"ACCOUNTS_DEMO_USERS_AND_GROUPS"`
Tracing Tracing `ocisConfig:"tracing"`
Service Service `ocisConfig:"service"`

Tracing Tracing `ocisConfig:"tracing"`
Log Log `ocisConfig:"log"`
Debug Debug `ocisConfig:"debug"`

HTTP HTTP `ocisConfig:"http"`
GRPC GRPC `ocisConfig:"grpc"`

TokenManager TokenManager `ocisConfig:"token_manager"`

Asset Asset `ocisConfig:"asset"`
Repo Repo `ocisConfig:"repo"`
Index Index `ocisConfig:"index"`
ServiceUser ServiceUser `ocisConfig:"service_user"`
HashDifficulty int `ocisConfig:"hash_difficulty" env:"ACCOUNTS_HASH_DIFFICULTY"`
DemoUsersAndGroups bool `ocisConfig:"demo_users_and_groups" env:"ACCOUNTS_DEMO_USERS_AND_GROUPS"`

Context context.Context
Supervised bool
}

// New returns a new config.
func New() *Config {
return &Config{}
}

func DefaultConfig() *Config {
return &Config{

Expand Down
2 changes: 1 addition & 1 deletion accounts/pkg/proto/v0/accounts.pb.micro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func init() {
grpc.Address("localhost:9180"),
)

cfg := config.New()
cfg := config.DefaultConfig()
cfg.Repo.Backend = "disk"
cfg.Repo.Disk.Path = dataPath
cfg.DemoUsersAndGroups = true
Expand Down
3 changes: 1 addition & 2 deletions accounts/pkg/service/v0/accounts_permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ var (
)

func init() {
cfg := config.New()
cfg.Service.Name = "accounts"
cfg := config.DefaultConfig()
cfg.Repo.Backend = "disk"
cfg.Repo.Disk.Path = dataPath
logger := olog.NewLogger(olog.Color(true), olog.Pretty(true))
Expand Down
2 changes: 1 addition & 1 deletion glauth/cmd/glauth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func main() {
if err := command.Execute(config.New()); err != nil {
if err := command.Execute(config.DefaultConfig()); err != nil {
os.Exit(1)
}
}
6 changes: 4 additions & 2 deletions glauth/pkg/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ func Execute(cfg *config.Config) error {
Email: "[email protected]",
},
},

Before: func(c *cli.Context) error {
cfg.Service.Version = version.String
return nil
return ParseConfig(c, cfg)
},

Commands: []*cli.Command{
Server(cfg),
Health(cfg),
Expand Down Expand Up @@ -89,7 +91,7 @@ type SutureService struct {

// NewSutureService creates a new glauth.SutureService
func NewSutureService(cfg *ociscfg.Config) suture.Service {
cfg.GLAuth.Commons = cfg.Commons
//cfg.GLAuth.Commons = cfg.Commons
return SutureService{
cfg: cfg.GLAuth,
}
Expand Down
27 changes: 13 additions & 14 deletions glauth/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,24 @@ type FallbackBackend struct {
type Config struct {
*shared.Commons

Log Log `ocisConfig:"log"`
Debug Debug `ocisConfig:"debug"`
Service Service `ocisConfig:"service"`
Tracing Tracing `ocisConfig:"tracing"`
Ldap Ldap `ocisConfig:"ldap"`
Ldaps Ldaps `ocisConfig:"ldaps"`
Backend Backend `ocisConfig:"backend"`
Fallback FallbackBackend `ocisConfig:"fallback"`
RoleBundleUUID string `ocisConfig:"role_bundle_uuid" env:"GLAUTH_ROLE_BUNDLE_ID"`
Service Service `ocisConfig:"service"`

Tracing Tracing `ocisConfig:"tracing"`
Log Log `ocisConfig:"log"`
Debug Debug `ocisConfig:"debug"`

Ldap Ldap `ocisConfig:"ldap"`
Ldaps Ldaps `ocisConfig:"ldaps"`

Backend Backend `ocisConfig:"backend"`
Fallback FallbackBackend `ocisConfig:"fallback"`

RoleBundleUUID string `ocisConfig:"role_bundle_uuid" env:"GLAUTH_ROLE_BUNDLE_ID"`

Context context.Context
Supervised bool
}

// New initializes a new configuration with or without defaults.
func New() *Config {
return &Config{}
}

func DefaultConfig() *Config {
return &Config{
Debug: Debug{
Expand Down
3 changes: 2 additions & 1 deletion graph-explorer/pkg/command/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

"github.com/owncloud/ocis/graph-explorer/pkg/config"
"github.com/owncloud/ocis/graph-explorer/pkg/logging"
"github.com/urfave/cli/v2"
)

Expand All @@ -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(
Expand Down
47 changes: 30 additions & 17 deletions graph-explorer/pkg/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"context"
"os"

"github.com/imdario/mergo"
"github.com/owncloud/ocis/graph-explorer/pkg/config"
ociscfg "github.com/owncloud/ocis/ocis-pkg/config"
"github.com/owncloud/ocis/ocis-pkg/log"
"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 graph-explorer command.
Expand All @@ -25,10 +26,12 @@ func Execute(cfg *config.Config) error {
Email: "[email protected]",
},
},

Before: func(c *cli.Context) error {
cfg.Service.Version = version.String
return ParseConfig(c, cfg)
},

Commands: []*cli.Command{
Server(cfg),
Health(cfg),
Expand All @@ -46,27 +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 log.NewLogger(
log.Name("graph-explorer"),
log.Level(cfg.Log.Level),
log.Pretty(cfg.Log.Pretty),
log.Color(cfg.Log.Color),
log.File(cfg.Log.File),
)
}

// ParseConfig loads graph configuration from known paths.
func ParseConfig(c *cli.Context, cfg *config.Config) error {
conf, err := ociscfg.BindSourcesToStructs("graph-explorer", cfg)
_, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg)
if err != nil {
return err
}

conf.LoadOSEnv(config.GetEnv(), false)
bindings := config.StructMappings(cfg)
return ociscfg.BindEnv(conf, bindings)
// 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{}
//}

// load all env variables relevant to the config in the current context.
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 graph-explorer command to be embedded and supervised by a suture supervisor tree.
Expand All @@ -76,7 +89,7 @@ type SutureService struct {

// NewSutureService creates a new graph-explorer.SutureService
func NewSutureService(cfg *ociscfg.Config) suture.Service {
cfg.GraphExplorer.Log = cfg.Log
//cfg.GraphExplorer.Log = cfg.Log
return SutureService{
cfg: cfg.GraphExplorer,
}
Expand Down
9 changes: 6 additions & 3 deletions graph-explorer/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/oklog/run"
"github.com/owncloud/ocis/graph-explorer/pkg/config"
"github.com/owncloud/ocis/graph-explorer/pkg/logging"
"github.com/owncloud/ocis/graph-explorer/pkg/metrics"
"github.com/owncloud/ocis/graph-explorer/pkg/server/debug"
"github.com/owncloud/ocis/graph-explorer/pkg/server/http"
Expand All @@ -26,9 +27,11 @@ func Server(cfg *config.Config) *cli.Command {
return ParseConfig(ctx, cfg)
},
Action: func(c *cli.Context) error {
logger := NewLogger(cfg)
tracing.Configure(cfg)

logger := logging.Configure(cfg.Service.Name, cfg.Log)
err := tracing.Configure(cfg)
if err != nil {
return err
}
var (
gr = run.Group{}
ctx, cancel = func() (context.Context, context.CancelFunc) {
Expand Down
Loading

0 comments on commit ee57288

Please sign in to comment.