switch all other services to struct tag based env config
This commit is contained in:
committed by
Jörn Friedrich Dreyer
parent
288d6c469e
commit
6990e7d660
@@ -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"
|
||||
)
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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.
|
||||
@@ -25,10 +26,12 @@ func Execute(cfg *config.Config) error {
|
||||
Email: "support@owncloud.com",
|
||||
},
|
||||
},
|
||||
|
||||
Before: func(c *cli.Context) error {
|
||||
cfg.Service.Version = version.String
|
||||
return ParseConfig(c, cfg)
|
||||
},
|
||||
|
||||
Commands: []*cli.Command{
|
||||
Server(cfg),
|
||||
Health(cfg),
|
||||
@@ -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.
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
@@ -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) {
|
||||
|
||||
@@ -2,70 +2,73 @@ package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/owncloud/ocis/ocis-pkg/shared"
|
||||
)
|
||||
|
||||
// Debug defines the available debug configuration.
|
||||
type Debug struct {
|
||||
Addr string `ocisConfig:"addr"`
|
||||
Token string `ocisConfig:"token"`
|
||||
Pprof bool `ocisConfig:"pprof"`
|
||||
Zpages bool `ocisConfig:"zpages"`
|
||||
Addr string `ocisConfig:"addr" env:"GRAPH_EXPLORER_DEBUG_ADDR"`
|
||||
Token string `ocisConfig:"token" env:"GRAPH_EXPLORER_DEBUG_TOKEN"`
|
||||
Pprof bool `ocisConfig:"pprof" env:"GRAPH_EXPLORER_DEBUG_PPROF"`
|
||||
Zpages bool `ocisConfig:"zpages" env:"GRAPH_EXPLORER_DEBUG_ZPAGES"`
|
||||
}
|
||||
|
||||
// HTTP defines the available http configuration.
|
||||
type HTTP struct {
|
||||
Addr string `ocisConfig:"addr"`
|
||||
Root string `ocisConfig:"root"`
|
||||
Addr string `ocisConfig:"addr" env:"GRAPH_EXPLORER_HTTP_ADDR"`
|
||||
Root string `ocisConfig:"root" env:"GRAPH_EXPLORER_HTTP_ROOT"`
|
||||
Namespace string
|
||||
}
|
||||
|
||||
// Service defines the available service configuration.
|
||||
type Service struct {
|
||||
Name string `ocisConfig:"name"`
|
||||
Version string `ocisConfig:"version"`
|
||||
Name string
|
||||
Version string
|
||||
}
|
||||
|
||||
// Tracing defines the available tracing configuration.
|
||||
type Tracing struct {
|
||||
Enabled bool `ocisConfig:"enabled"`
|
||||
Type string `ocisConfig:"type"`
|
||||
Endpoint string `ocisConfig:"endpoint"`
|
||||
Collector string `ocisConfig:"collector"`
|
||||
Service string `ocisConfig:"service"`
|
||||
Enabled bool `ocisConfig:"enabled" env:"OCIS_TRACING_ENABLED;GRAPH_EXPLORER_TRACING_ENABLED"`
|
||||
Type string `ocisConfig:"type" env:"OCIS_TRACING_TYPE;GRAPH_EXPLORER_TRACING_TYPE"`
|
||||
Endpoint string `ocisConfig:"endpoint" env:"OCIS_TRACING_ENDPOINT;GRAPH_EXPLORER_TRACING_ENDPOINT"`
|
||||
Collector string `ocisConfig:"collector" env:"OCIS_TRACING_COLLECTOR;GRAPH_EXPLORER_TRACING_COLLECTOR"`
|
||||
Service string `ocisConfig:"service" env:"GRAPH_EXPLORER_TRACING_SERVICE"` //TODO: should this be an ID? or the same as Service.Name?
|
||||
}
|
||||
|
||||
// Log defines the available log configuration.
|
||||
type Log struct {
|
||||
Level string `mapstructure:"level" env:"OCIS_LOG_LEVEL;GRAPH_EXPLORER_LOG_LEVEL"`
|
||||
Pretty bool `mapstructure:"pretty" env:"OCIS_LOG_PRETTY;GRAPH_EXPLORER_LOG_PRETTY"`
|
||||
Color bool `mapstructure:"color" env:"OCIS_LOG_COLOR;GRAPH_EXPLORER_LOG_COLOR"`
|
||||
File string `mapstructure:"file" env:"OCIS_LOG_FILE;GRAPH_EXPLORER_LOG_FILE"`
|
||||
}
|
||||
|
||||
// GraphExplorer defines the available graph-explorer configuration.
|
||||
type GraphExplorer struct {
|
||||
ClientID string `ocisConfig:"client_id"`
|
||||
Issuer string `ocisConfig:"issuer"`
|
||||
GraphURLBase string `ocisConfig:"graph_url_base"`
|
||||
GraphURLPath string `ocisConfig:"graph_url_path"`
|
||||
ClientID string `ocisConfig:"client_id" env:"GRAPH_EXPLORER_CLIENT_ID"`
|
||||
Issuer string `ocisConfig:"issuer" env:"OCIS_URL;GRAPH_EXPLORER_ISSUER"`
|
||||
GraphURLBase string `ocisConfig:"graph_url_base" env:"OCIS_URL;GRAPH_EXPLORER_GRAPH_URL_BASE"`
|
||||
GraphURLPath string `ocisConfig:"graph_url_path" env:"GRAPH_EXPLORER_GRAPH_URL_PATH"`
|
||||
}
|
||||
|
||||
// Config combines all available configuration parts.
|
||||
type Config struct {
|
||||
Log shared.Log `ocisConfig:"log"`
|
||||
Debug Debug `ocisConfig:"debug"`
|
||||
HTTP HTTP `ocisConfig:"http"`
|
||||
Service Service `ocisConfig:"service"`
|
||||
Tracing Tracing `ocisConfig:"tracing"`
|
||||
Service Service `ocisConfig:"service"`
|
||||
|
||||
Tracing Tracing `ocisConfig:"tracing"`
|
||||
Log Log `ocisConfig:"log"`
|
||||
Debug Debug `ocisConfig:"debug"`
|
||||
|
||||
HTTP HTTP `ocisConfig:"http"`
|
||||
|
||||
GraphExplorer GraphExplorer `ocisConfig:"graph_explorer"`
|
||||
|
||||
Context context.Context
|
||||
Supervised bool
|
||||
}
|
||||
|
||||
// New initializes a new configuration with or without defaults.
|
||||
func New() *Config {
|
||||
return &Config{}
|
||||
}
|
||||
|
||||
// DefaultConfig provides with a working version of a config.
|
||||
func DefaultConfig() *Config {
|
||||
return &Config{
|
||||
Log: shared.Log{},
|
||||
Debug: Debug{
|
||||
Addr: "127.0.0.1:9136",
|
||||
Token: "",
|
||||
@@ -78,7 +81,7 @@ func DefaultConfig() *Config {
|
||||
Namespace: "com.owncloud.web",
|
||||
},
|
||||
Service: Service{
|
||||
Name: "graph",
|
||||
Name: "graph-explorer",
|
||||
},
|
||||
Tracing: Tracing{
|
||||
Type: "jaeger",
|
||||
@@ -94,14 +97,3 @@ func DefaultConfig() *Config {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// GetEnv fetches a list of known env variables for this extension. It is to be used by gookit, as it provides a list
|
||||
// with all the environment variables an extension supports.
|
||||
func GetEnv() []string {
|
||||
var r = make([]string, len(structMappings(&Config{})))
|
||||
for i := range structMappings(&Config{}) {
|
||||
r = append(r, structMappings(&Config{})[i].EnvVars...)
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -1,96 +0,0 @@
|
||||
package config
|
||||
|
||||
import "github.com/owncloud/ocis/ocis-pkg/shared"
|
||||
|
||||
// StructMappings binds a set of environment variables to a destination on cfg. Iterating over this set and editing the
|
||||
// Destination value of a binding will alter the original value, as it is a pointer to its memory address. This lets
|
||||
// us propagate changes easier.
|
||||
func StructMappings(cfg *Config) []shared.EnvBinding {
|
||||
return structMappings(cfg)
|
||||
}
|
||||
|
||||
// structMappings binds a set of environment variables to a destination on cfg.
|
||||
func structMappings(cfg *Config) []shared.EnvBinding {
|
||||
return []shared.EnvBinding{
|
||||
{
|
||||
EnvVars: []string{"OCIS_LOG_LEVEL", "GRAPH_EXPLORER_LOG_LEVEL"},
|
||||
Destination: &cfg.Log.Level,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCIS_LOG_PRETTY", "GRAPH_EXPLORER_LOG_PRETTY"},
|
||||
Destination: &cfg.Log.Pretty,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCIS_LOG_COLOR", "GRAPH_EXPLORER_LOG_COLOR"},
|
||||
Destination: &cfg.Log.Color,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCIS_LOG_FILE", "GRAPH_EXPLORER_LOG_FILE"},
|
||||
Destination: &cfg.Log.File,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCIS_TRACING_ENABLED", "GRAPH_EXPLORER_TRACING_ENABLED"},
|
||||
Destination: &cfg.Tracing.Enabled,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCIS_TRACING_TYPE", "GRAPH_EXPLORER_TRACING_TYPE"},
|
||||
Destination: &cfg.Tracing.Type,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCIS_TRACING_ENDPOINT", "GRAPH_EXPLORER_TRACING_ENDPOINT"},
|
||||
Destination: &cfg.Tracing.Endpoint,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCIS_TRACING_COLLECTOR", "GRAPH_EXPLORER_TRACING_COLLECTOR"},
|
||||
Destination: &cfg.Tracing.Collector,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"GRAPH_EXPLORER_TRACING_SERVICE"},
|
||||
Destination: &cfg.Tracing.Service,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"GRAPH_EXPLORER_DEBUG_ADDR"},
|
||||
Destination: &cfg.Debug.Addr,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"GRAPH_EXPLORER_DEBUG_TOKEN"},
|
||||
Destination: &cfg.Debug.Token,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"GRAPH_EXPLORER_DEBUG_PPROF"},
|
||||
Destination: &cfg.Debug.Pprof,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"GRAPH_EXPLORER_DEBUG_ZPAGES"},
|
||||
Destination: &cfg.Debug.Zpages,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"GRAPH_EXPLORER_HTTP_ADDR"},
|
||||
Destination: &cfg.HTTP.Addr,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"GRAPH_EXPLORER_HTTP_ROOT"},
|
||||
Destination: &cfg.HTTP.Root,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"GRAPH_EXPLORER_NAMESPACE"},
|
||||
Destination: &cfg.HTTP.Namespace,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCIS_URL", "GRAPH_EXPLORER_ISSUER"},
|
||||
Destination: &cfg.GraphExplorer.Issuer,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"GRAPH_EXPLORER_CLIENT_ID"},
|
||||
Destination: &cfg.GraphExplorer.ClientID,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCIS_URL", "GRAPH_EXPLORER_GRAPH_URL_BASE"},
|
||||
Destination: &cfg.GraphExplorer.GraphURLBase,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"GRAPH_EXPLORER_GRAPH_URL_PATH"},
|
||||
Destination: &cfg.GraphExplorer.GraphURLPath,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"github.com/owncloud/ocis/graph-explorer/pkg/config"
|
||||
"github.com/owncloud/ocis/ocis-pkg/log"
|
||||
)
|
||||
|
||||
// LoggerFromConfig initializes a service-specific logger instance.
|
||||
func Configure(name string, cfg config.Log) log.Logger {
|
||||
return log.NewLogger(
|
||||
log.Name(name),
|
||||
log.Level(cfg.Level),
|
||||
log.Pretty(cfg.Pretty),
|
||||
log.Color(cfg.Color),
|
||||
log.File(cfg.File),
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user