migrate graph-explorer to the new config scheme

This commit is contained in:
A.Unger
2021-11-05 11:31:35 +01:00
parent ba1635165b
commit 91c766210c
8 changed files with 177 additions and 180 deletions
+1 -1
View File
@@ -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)
}
}
+8 -42
View File
@@ -3,14 +3,11 @@ package command
import (
"context"
"os"
"strings"
"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/sync"
"github.com/owncloud/ocis/ocis-pkg/version"
"github.com/spf13/viper"
"github.com/thejerf/suture/v4"
"github.com/urfave/cli/v2"
)
@@ -29,9 +26,6 @@ func Execute(cfg *config.Config) error {
Email: "support@owncloud.com",
},
},
//Flags: flagset.RootWithConfig(cfg),
Before: func(c *cli.Context) error {
cfg.Server.Version = version.String
return ParseConfig(c, cfg)
@@ -67,46 +61,18 @@ func NewLogger(cfg *config.Config) log.Logger {
)
}
// ParseConfig loads accounts configuration from Viper known paths.
// ParseConfig loads graph configuration from known paths.
func ParseConfig(c *cli.Context, cfg *config.Config) error {
sync.ParsingViperConfig.Lock()
defer sync.ParsingViperConfig.Unlock()
logger := NewLogger(cfg)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("GRAPH_EXPLORER")
viper.AutomaticEnv()
if c.IsSet("config-file") {
viper.SetConfigFile(c.String("config-file"))
} else {
viper.SetConfigName("graph-explorer")
viper.AddConfigPath("/etc/ocis")
viper.AddConfigPath("$HOME/.ocis")
viper.AddConfigPath("./config")
conf, err := ociscfg.BindSourcesToStructs("graph-explorer", cfg)
if err != nil {
return err
}
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")
}
}
// load all env variables relevant to the config in the current context.
conf.LoadOSEnv(config.GetEnv(), false)
if err := viper.Unmarshal(&cfg); err != nil {
logger.Fatal().
Err(err).
Msg("Failed to parse config")
if err = cfg.UnmapEnv(conf); err != nil {
return err
}
return nil
+3 -8
View File
@@ -6,7 +6,6 @@ import (
"github.com/oklog/run"
"github.com/owncloud/ocis/graph-explorer/pkg/config"
"github.com/owncloud/ocis/graph-explorer/pkg/flagset"
"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"
@@ -20,19 +19,15 @@ func Server(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "server",
Usage: "Start integrated server",
Flags: flagset.ServerWithConfig(cfg),
Before: func(ctx *cli.Context) error {
logger := NewLogger(cfg)
if cfg.HTTP.Root != "/" {
cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/")
}
// When running on single binary mode the before hook from the root command won't get called. We manually
// call this before hook from ocis command, so the configuration can be loaded.
if !cfg.Supervised {
return ParseConfig(ctx, cfg)
if err := ParseConfig(ctx, cfg); err != nil {
return err
}
logger.Debug().Str("service", "graph-explorer").Msg("ignoring config file parsing when running supervised")
return nil
},
Action: func(c *cli.Context) error {
+84 -1
View File
@@ -1,6 +1,12 @@
package config
import "context"
import (
"context"
"fmt"
"reflect"
gofig "github.com/gookit/config/v2"
)
// Log defines the available logging configuration.
type Log struct {
@@ -66,3 +72,80 @@ type Config struct {
func New() *Config {
return &Config{}
}
// DefaultConfig provides with a working version of a config.
func DefaultConfig() *Config {
return &Config{
Log: Log{},
Debug: Debug{
Addr: "127.0.0.1:9136",
Token: "",
Pprof: false,
Zpages: false,
},
HTTP: HTTP{
Addr: "127.0.0.1:9135",
Root: "/graph-explorer",
Namespace: "com.owncloud.web",
},
Server: Server{},
Tracing: Tracing{
Type: "jaeger",
Endpoint: "",
Collector: "",
Service: "graph-explorer",
},
GraphExplorer: GraphExplorer{
ClientID: "ocis-explorer.js",
Issuer: "https://localhost:9200",
GraphURLBase: "https://localhost:9200",
GraphURLPath: "/graph",
},
}
}
// 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
}
// UnmapEnv loads values from the gooconf.Config argument and sets them in the expected destination.
func (c *Config) UnmapEnv(gooconf *gofig.Config) error {
vals := structMappings(c)
for i := range vals {
for j := range vals[i].EnvVars {
// we need to guard against v != "" because this is the condition that checks that the value is set from the environment.
// the `ok` guard is not enough, apparently.
if v, ok := gooconf.GetValue(vals[i].EnvVars[j]); ok && v != "" {
// get the destination type from destination
switch reflect.ValueOf(vals[i].Destination).Type().String() {
case "*bool":
r := gooconf.Bool(vals[i].EnvVars[j])
*vals[i].Destination.(*bool) = r
case "*string":
r := gooconf.String(vals[i].EnvVars[j])
*vals[i].Destination.(*string) = r
case "*int":
r := gooconf.Int(vals[i].EnvVars[j])
*vals[i].Destination.(*int) = r
case "*float64":
// defaults to float64
r := gooconf.Float(vals[i].EnvVars[j])
*vals[i].Destination.(*float64) = r
default:
// it is unlikely we will ever get here. Let this serve more as a runtime check for when debugging.
return fmt.Errorf("invalid type for env var: `%v`", vals[i].EnvVars[j])
}
}
}
}
return nil
}
+80
View File
@@ -0,0 +1,80 @@
package config
type mapping struct {
EnvVars []string // name of the EnvVars var.
Destination interface{} // memory address of the original config value to modify.
}
// structMappings binds a set of environment variables to a destination on cfg.
func structMappings(cfg *Config) []mapping {
return []mapping{
{
EnvVars: []string{"GRAPH_EXPLORER_LOG_FILE", "OCIS_LOG_FILE"},
Destination: &cfg.Log.File,
},
{
EnvVars: []string{"GRAPH_EXPLORER_TRACING_ENABLED", "OCIS_TRACING_ENABLED"},
Destination: &cfg.Tracing.Enabled,
},
{
EnvVars: []string{"GRAPH_EXPLORER_TRACING_TYPE", "OCIS_TRACING_TYPE"},
Destination: &cfg.Tracing.Type,
},
{
EnvVars: []string{"GRAPH_EXPLORER_TRACING_ENDPOINT", "OCIS_TRACING_ENDPOINT"},
Destination: &cfg.Tracing.Endpoint,
},
{
EnvVars: []string{"GRAPH_EXPLORER_TRACING_COLLECTOR", "OCIS_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{"GRAPH_EXPLORER_ISSUER", "OCIS_URL"},
Destination: &cfg.GraphExplorer.Issuer,
},
{
EnvVars: []string{"GRAPH_EXPLORER_CLIENT_ID"},
Destination: &cfg.GraphExplorer.ClientID,
},
{
EnvVars: []string{"GRAPH_EXPLORER_GRAPH_URL_BASE", "OCIS_URL"},
Destination: &cfg.GraphExplorer.GraphURLBase,
},
{
EnvVars: []string{"GRAPH_EXPLORER_GRAPH_URL_PATH"},
Destination: &cfg.GraphExplorer.GraphURLPath,
},
}
}
-125
View File
@@ -42,128 +42,3 @@ func HealthWithConfig(cfg *config.Config) []cli.Flag {
},
}
}
// ServerWithConfig applies cfg to the root flagset
func ServerWithConfig(cfg *config.Config) []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "log-file",
Usage: "Enable log to file",
EnvVars: []string{"GRAPH_EXPLORER_LOG_FILE", "OCIS_LOG_FILE"},
Destination: &cfg.Log.File,
},
&cli.BoolFlag{
Name: "tracing-enabled",
Usage: "Enable sending traces",
EnvVars: []string{"GRAPH_EXPLORER_TRACING_ENABLED", "OCIS_TRACING_ENABLED"},
Destination: &cfg.Tracing.Enabled,
},
&cli.StringFlag{
Name: "tracing-type",
Value: flags.OverrideDefaultString(cfg.Tracing.Type, "jaeger"),
Usage: "Tracing backend type",
EnvVars: []string{"GRAPH_EXPLORER_TRACING_TYPE", "OCIS_TRACING_TYPE"},
Destination: &cfg.Tracing.Type,
},
&cli.StringFlag{
Name: "tracing-endpoint",
Value: flags.OverrideDefaultString(cfg.Tracing.Endpoint, ""),
Usage: "Endpoint for the agent",
EnvVars: []string{"GRAPH_EXPLORER_TRACING_ENDPOINT", "OCIS_TRACING_ENDPOINT"},
Destination: &cfg.Tracing.Endpoint,
},
&cli.StringFlag{
Name: "tracing-collector",
Value: flags.OverrideDefaultString(cfg.Tracing.Collector, ""),
Usage: "Endpoint for the collector",
EnvVars: []string{"GRAPH_EXPLORER_TRACING_COLLECTOR", "OCIS_TRACING_COLLECTOR"},
Destination: &cfg.Tracing.Collector,
},
&cli.StringFlag{
Name: "tracing-service",
Value: flags.OverrideDefaultString(cfg.Tracing.Service, "graph-explorer"),
Usage: "Service name for tracing",
EnvVars: []string{"GRAPH_EXPLORER_TRACING_SERVICE"},
Destination: &cfg.Tracing.Service,
},
&cli.StringFlag{
Name: "debug-addr",
Value: flags.OverrideDefaultString(cfg.Debug.Addr, "127.0.0.1:9136"),
Usage: "Address to bind debug server",
EnvVars: []string{"GRAPH_EXPLORER_DEBUG_ADDR"},
Destination: &cfg.Debug.Addr,
},
&cli.StringFlag{
Name: "debug-token",
Value: flags.OverrideDefaultString(cfg.Debug.Token, ""),
Usage: "Token to grant metrics access",
EnvVars: []string{"GRAPH_EXPLORER_DEBUG_TOKEN"},
Destination: &cfg.Debug.Token,
},
&cli.BoolFlag{
Name: "debug-pprof",
Usage: "Enable pprof debugging",
EnvVars: []string{"GRAPH_EXPLORER_DEBUG_PPROF"},
Destination: &cfg.Debug.Pprof,
},
&cli.BoolFlag{
Name: "debug-zpages",
Usage: "Enable zpages debugging",
EnvVars: []string{"GRAPH_EXPLORER_DEBUG_ZPAGES"},
Destination: &cfg.Debug.Zpages,
},
&cli.StringFlag{
Name: "http-addr",
Value: flags.OverrideDefaultString(cfg.HTTP.Addr, "127.0.0.1:9135"),
Usage: "Address to bind http server",
EnvVars: []string{"GRAPH_EXPLORER_HTTP_ADDR"},
Destination: &cfg.HTTP.Addr,
},
&cli.StringFlag{
Name: "http-root",
Value: flags.OverrideDefaultString(cfg.HTTP.Root, "/graph-explorer"),
Usage: "Root path of http server",
EnvVars: []string{"GRAPH_EXPLORER_HTTP_ROOT"},
Destination: &cfg.HTTP.Root,
},
&cli.StringFlag{
Name: "http-namespace",
Value: flags.OverrideDefaultString(cfg.HTTP.Namespace, "com.owncloud.web"),
Usage: "Set the base namespace for the http namespace",
EnvVars: []string{"GRAPH_EXPLORER_NAMESPACE"},
Destination: &cfg.HTTP.Namespace,
},
&cli.StringFlag{
Name: "issuer",
Value: flags.OverrideDefaultString(cfg.GraphExplorer.Issuer, "https://localhost:9200"),
Usage: "Set the OpenID Connect Provider",
EnvVars: []string{"GRAPH_EXPLORER_ISSUER", "OCIS_URL"},
Destination: &cfg.GraphExplorer.Issuer,
},
&cli.StringFlag{
Name: "client-id",
Value: flags.OverrideDefaultString(cfg.GraphExplorer.ClientID, "ocis-explorer.js"),
Usage: "Set the OpenID Client ID to send to the issuer",
EnvVars: []string{"GRAPH_EXPLORER_CLIENT_ID"},
Destination: &cfg.GraphExplorer.ClientID,
},
&cli.StringFlag{
Name: "graph-url-base",
Value: flags.OverrideDefaultString(cfg.GraphExplorer.GraphURLBase, "https://localhost:9200"),
Usage: "Set the base url to the graph api service",
EnvVars: []string{"GRAPH_EXPLORER_GRAPH_URL_BASE", "OCIS_URL"},
Destination: &cfg.GraphExplorer.GraphURLBase,
},
&cli.StringFlag{
Name: "graph-url-path",
Value: flags.OverrideDefaultString(cfg.GraphExplorer.GraphURLPath, "/graph"),
Usage: "Set the url path to the graph api service",
EnvVars: []string{"GRAPH_EXPLORER_GRAPH_URL_PATH"},
Destination: &cfg.GraphExplorer.GraphURLPath,
},
&cli.StringFlag{
Name: "extensions",
Usage: "Run specific extensions during supervised mode. This flag is set by the runtime",
},
}
}
-2
View File
@@ -24,8 +24,6 @@ func Server(cfg *config.Config) *cli.Command {
cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/")
}
// When running on single binary mode the before hook from the root command won't get called. We manually
// call this before hook from ocis command, so the configuration can be loaded.
if err := ParseConfig(ctx, cfg); err != nil {
return err
}
+1 -1
View File
@@ -119,7 +119,7 @@ func New() *Config {
Graph: graph.DefaultConfig(),
IDP: idp.DefaultConfig(),
Proxy: proxy.DefaultConfig(),
GraphExplorer: graphExplorer.New(),
GraphExplorer: graphExplorer.DefaultConfig(),
OCS: ocs.New(),
Web: web.New(),
Settings: settings.New(),