migrate ocs to the new config scheme
This commit is contained in:
@@ -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 {
|
||||
@@ -84,3 +90,93 @@ type Config struct {
|
||||
func New() *Config {
|
||||
return &Config{}
|
||||
}
|
||||
|
||||
// DefaultConfig provides default values for a config struct.
|
||||
func DefaultConfig() *Config {
|
||||
return &Config{
|
||||
Log: Log{},
|
||||
Debug: Debug{
|
||||
Addr: "127.0.0.1:9114",
|
||||
Token: "",
|
||||
Pprof: false,
|
||||
Zpages: false,
|
||||
},
|
||||
HTTP: HTTP{
|
||||
Addr: "127.0.0.1:9110",
|
||||
Root: "/ocs",
|
||||
CORS: CORS{
|
||||
AllowedOrigins: []string{"*"},
|
||||
AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
|
||||
AllowedHeaders: []string{"Authorization", "Origin", "Content-Type", "Accept", "X-Requested-With"},
|
||||
AllowCredentials: true,
|
||||
},
|
||||
},
|
||||
Tracing: Tracing{
|
||||
Enabled: false,
|
||||
Type: "jaeger",
|
||||
Endpoint: "",
|
||||
Collector: "",
|
||||
Service: "ocs",
|
||||
},
|
||||
TokenManager: TokenManager{
|
||||
JWTSecret: "Pive-Fumkiu4",
|
||||
},
|
||||
Service: Service{
|
||||
Name: "ocs",
|
||||
Namespace: "com.owncloud.web",
|
||||
},
|
||||
AccountBackend: "accounts",
|
||||
RevaAddress: "127.0.0.1:9142",
|
||||
StorageUsersDriver: "ocis",
|
||||
MachineAuthAPIKey: "change-me-please",
|
||||
IdentityManagement: IdentityManagement{
|
||||
Address: "https://localhost:9200",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
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{"OCS_LOG_FILE", "OCIS_LOG_FILE"},
|
||||
Destination: &cfg.Log.File,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_LOG_LEVEL", "OCIS_LOG_LEVEL"},
|
||||
Destination: &cfg.Log.Level,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_LOG_PRETTY", "OCIS_LOG_PRETTY"},
|
||||
Destination: &cfg.Log.Pretty,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_LOG_COLOR", "OCIS_LOG_COLOR"},
|
||||
Destination: &cfg.Log.Color,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_CONFIG_FILE"},
|
||||
Destination: &cfg.File,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_TRACING_ENABLED", "OCIS_TRACING_ENABLED"},
|
||||
Destination: &cfg.Tracing.Enabled,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_TRACING_TYPE", "OCIS_TRACING_TYPE"},
|
||||
Destination: &cfg.Tracing.Type,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_TRACING_ENDPOINT", "OCIS_TRACING_ENDPOINT"},
|
||||
Destination: &cfg.Tracing.Endpoint,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_TRACING_COLLECTOR", "OCIS_TRACING_COLLECTOR"},
|
||||
Destination: &cfg.Tracing.Collector,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_TRACING_SERVICE"},
|
||||
Destination: &cfg.Tracing.Service,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_DEBUG_ADDR"},
|
||||
Destination: &cfg.Debug.Addr,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_DEBUG_TOKEN"},
|
||||
Destination: &cfg.Debug.Token,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_DEBUG_PPROF"},
|
||||
Destination: &cfg.Debug.Pprof,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_DEBUG_ZPAGES"},
|
||||
Destination: &cfg.Debug.Zpages,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_HTTP_ADDR"},
|
||||
Destination: &cfg.HTTP.Addr,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_NAMESPACE"},
|
||||
Destination: &cfg.Service.Namespace,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_NAME"},
|
||||
Destination: &cfg.Service.Name,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_HTTP_ROOT"},
|
||||
Destination: &cfg.HTTP.Root,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_JWT_SECRET", "OCIS_JWT_SECRET"},
|
||||
Destination: &cfg.TokenManager.JWTSecret,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_ACCOUNT_BACKEND_TYPE"},
|
||||
Destination: &cfg.AccountBackend,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"REVA_GATEWAY"},
|
||||
Destination: &cfg.RevaAddress,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_MACHINE_AUTH_API_KEY", "OCIS_MACHINE_AUTH_API_KEY"},
|
||||
Destination: &cfg.MachineAuthAPIKey,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_IDM_ADDRESS", "OCIS_URL"},
|
||||
Destination: &cfg.IdentityManagement.Address,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCS_STORAGE_USERS_DRIVER", "STORAGE_USERS_DRIVER"},
|
||||
Destination: &cfg.StorageUsersDriver,
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user