migrate settings to the new config scheme

This commit is contained in:
A.Unger
2021-11-05 12:25:32 +01:00
parent 28b0b8fccd
commit 17dbc3d5ee
9 changed files with 227 additions and 302 deletions
+102 -1
View File
@@ -1,6 +1,15 @@
package config
import "context"
import (
"context"
"fmt"
"path"
"reflect"
gofig "github.com/gookit/config/v2"
"github.com/owncloud/ocis/ocis-pkg/config/defaults"
)
// Log defines the available logging configuration.
type Log struct {
@@ -87,3 +96,95 @@ type Config struct {
func New() *Config {
return &Config{}
}
// DefaultConfig provides sane bootstrapping defaults.
func DefaultConfig() *Config {
return &Config{
Log: Log{},
Service: Service{
Name: "settings",
DataPath: path.Join(defaults.BaseDataPath(), "settings"),
},
Debug: Debug{
Addr: "127.0.0.1:9194",
Token: "",
Pprof: false,
Zpages: false,
},
HTTP: HTTP{
Addr: "127.0.0.1:9190",
Namespace: "com.owncloud.web",
Root: "/",
CacheTTL: 604800, // 10 days
CORS: CORS{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Authorization", "Origin", "Content-Type", "Accept", "X-Requested-With"},
AllowCredentials: true,
},
},
GRPC: GRPC{
Addr: "127.0.0.1:9191",
Namespace: "com.owncloud.api",
},
Tracing: Tracing{
Enabled: false,
Type: "jaeger",
Endpoint: "",
Collector: "",
Service: "settings",
},
Asset: Asset{
Path: "",
},
TokenManager: TokenManager{
JWTSecret: "Pive-Fumkiu4",
},
}
}
// 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
}
+104
View File
@@ -0,0 +1,104 @@
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{"SETTINGS_LOG_LEVEL", "OCIS_LOG_LEVEL"},
Destination: &cfg.Log.Level,
},
{
EnvVars: []string{"SETTINGS_LOG_PRETTY", "OCIS_LOG_PRETTY"},
Destination: &cfg.Log.Pretty,
},
{
EnvVars: []string{"SETTINGS_LOG_COLOR", "OCIS_LOG_COLOR"},
Destination: &cfg.Log.Color,
},
{
EnvVars: []string{"SETTINGS_CONFIG_FILE"},
Destination: &cfg.File,
},
{
EnvVars: []string{"SETTINGS_TRACING_ENABLED", "OCIS_TRACING_ENABLED"},
Destination: &cfg.Tracing.Enabled,
},
{
EnvVars: []string{"SETTINGS_TRACING_TYPE", "OCIS_TRACING_TYPE"},
Destination: &cfg.Tracing.Type,
},
{
EnvVars: []string{"SETTINGS_TRACING_ENDPOINT", "OCIS_TRACING_ENDPOINT"},
Destination: &cfg.Tracing.Endpoint,
},
{
EnvVars: []string{"SETTINGS_TRACING_COLLECTOR", "OCIS_TRACING_COLLECTOR"},
Destination: &cfg.Tracing.Collector,
},
{
EnvVars: []string{"SETTINGS_TRACING_SERVICE"},
Destination: &cfg.Tracing.Service,
},
{
EnvVars: []string{"SETTINGS_DEBUG_ADDR"},
Destination: &cfg.Debug.Addr,
},
{
EnvVars: []string{"SETTINGS_DEBUG_TOKEN"},
Destination: &cfg.Debug.Token,
},
{
EnvVars: []string{"SETTINGS_DEBUG_PPROF"},
Destination: &cfg.Debug.Pprof,
},
{
EnvVars: []string{"SETTINGS_DEBUG_ZPAGES"},
Destination: &cfg.Debug.Zpages,
},
{
EnvVars: []string{"SETTINGS_HTTP_ADDR"},
Destination: &cfg.HTTP.Addr,
},
{
EnvVars: []string{"SETTINGS_HTTP_NAMESPACE"},
Destination: &cfg.HTTP.Namespace,
},
{
EnvVars: []string{"SETTINGS_HTTP_ROOT"},
Destination: &cfg.HTTP.Root,
},
{
EnvVars: []string{"SETTINGS_CACHE_TTL"},
Destination: &cfg.HTTP.CacheTTL,
},
{
EnvVars: []string{"SETTINGS_GRPC_ADDR"},
Destination: &cfg.GRPC.Addr,
},
{
EnvVars: []string{"SETTINGS_ASSET_PATH"},
Destination: &cfg.Asset.Path,
},
{
EnvVars: []string{"SETTINGS_GRPC_NAMESPACE"},
Destination: &cfg.GRPC.Namespace,
},
{
EnvVars: []string{"SETTINGS_NAME"},
Destination: &cfg.Service.Name,
},
{
EnvVars: []string{"SETTINGS_DATA_PATH"},
Destination: &cfg.Service.DataPath,
},
{
EnvVars: []string{"SETTINGS_JWT_SECRET", "OCIS_JWT_SECRET"},
Destination: &cfg.TokenManager.JWTSecret,
},
}
}