mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-01-04 11:19:39 -06:00
new config framework in proxy
This commit is contained in:
@@ -1,12 +1,8 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/owncloud/ocis/ocis-pkg/shared"
|
||||
|
||||
gofig "github.com/gookit/config/v2"
|
||||
accounts "github.com/owncloud/ocis/accounts/pkg/config"
|
||||
glauth "github.com/owncloud/ocis/glauth/pkg/config"
|
||||
graphExplorer "github.com/owncloud/ocis/graph-explorer/pkg/config"
|
||||
@@ -180,39 +176,11 @@ func GetEnv() []string {
|
||||
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
|
||||
// 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)
|
||||
}
|
||||
|
||||
func structMappings(cfg *Config) []shared.EnvBinding {
|
||||
|
||||
@@ -5,11 +5,14 @@ package command
|
||||
|
||||
import (
|
||||
"github.com/owncloud/ocis/ocis-pkg/config"
|
||||
"github.com/owncloud/ocis/ocis-pkg/shared"
|
||||
"github.com/owncloud/ocis/ocis/pkg/register"
|
||||
"github.com/owncloud/ocis/proxy/pkg/command"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var globalLog shared.Log
|
||||
|
||||
// ProxyCommand is the entry point for the proxy command.
|
||||
func ProxyCommand(cfg *config.Config) *cli.Command {
|
||||
return &cli.Command{
|
||||
@@ -20,10 +23,20 @@ func ProxyCommand(cfg *config.Config) *cli.Command {
|
||||
command.PrintVersion(cfg.Proxy),
|
||||
},
|
||||
Before: func(ctx *cli.Context) error {
|
||||
return ParseConfig(ctx, cfg)
|
||||
if err := ParseConfig(ctx, cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
globalLog = cfg.Log
|
||||
|
||||
return nil
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
cfg.Proxy.Log = cfg.Log
|
||||
// if proxy logging is empty in ocis.yaml
|
||||
if (cfg.Proxy.Log == shared.Log{}) && (globalLog != shared.Log{}) {
|
||||
// we can safely inherit the global logging values.
|
||||
cfg.Proxy.Log = globalLog
|
||||
}
|
||||
origCmd := command.Server(cfg.Proxy)
|
||||
return handleOriginalAction(c, origCmd)
|
||||
},
|
||||
|
||||
@@ -62,7 +62,7 @@ func NewLogger(cfg *config.Config) log.Logger {
|
||||
)
|
||||
}
|
||||
|
||||
// ParseConfig loads ocis configuration from known paths.
|
||||
// ParseConfig loads ocis configuration.
|
||||
func ParseConfig(c *cli.Context, cfg *config.Config) error {
|
||||
conf, err := ociscfg.BindSourcesToStructs("ocis", cfg)
|
||||
if err != nil {
|
||||
@@ -71,9 +71,6 @@ func ParseConfig(c *cli.Context, cfg *config.Config) error {
|
||||
|
||||
conf.LoadOSEnv(config.GetEnv(), false)
|
||||
|
||||
if err = cfg.UnmapEnv(conf); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
bindings := config.StructMappings(cfg)
|
||||
return ociscfg.BindEnv(conf, bindings)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"github.com/owncloud/ocis/ocis-pkg/shared"
|
||||
|
||||
ociscfg "github.com/owncloud/ocis/ocis-pkg/config"
|
||||
"github.com/owncloud/ocis/ocis-pkg/log"
|
||||
"github.com/owncloud/ocis/ocis-pkg/version"
|
||||
@@ -85,7 +87,9 @@ type SutureService struct {
|
||||
|
||||
// NewSutureService creates a new proxy.SutureService
|
||||
func NewSutureService(cfg *ociscfg.Config) suture.Service {
|
||||
cfg.Proxy.Log = cfg.Log
|
||||
if (cfg.Proxy.Log == shared.Log{}) {
|
||||
cfg.Proxy.Log = cfg.Log
|
||||
}
|
||||
return SutureService{
|
||||
cfg: cfg.Proxy,
|
||||
}
|
||||
|
||||
@@ -8,6 +8,11 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
gofig "github.com/gookit/config/v2"
|
||||
ociscfg "github.com/owncloud/ocis/ocis-pkg/config"
|
||||
|
||||
"github.com/owncloud/ocis/ocis-pkg/shared"
|
||||
|
||||
"github.com/coreos/go-oidc/v3/oidc"
|
||||
"github.com/cs3org/reva/pkg/token/manager/jwt"
|
||||
chimiddleware "github.com/go-chi/chi/v5/middleware"
|
||||
@@ -40,10 +45,26 @@ func Server(cfg *config.Config) *cli.Command {
|
||||
Name: "server",
|
||||
Usage: "Start integrated server",
|
||||
Before: func(ctx *cli.Context) error {
|
||||
// remember shared logging info to prevent empty overwrites
|
||||
inLog := cfg.Log
|
||||
|
||||
if err := ParseConfig(ctx, cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if (cfg.Log == shared.Log{}) && (inLog != shared.Log{}) {
|
||||
// set the default to the parent config
|
||||
cfg.Log = inLog
|
||||
|
||||
// and parse the environment
|
||||
conf := &gofig.Config{}
|
||||
conf.LoadOSEnv(config.GetEnv(), false)
|
||||
bindings := config.StructMappings(cfg)
|
||||
if err := ociscfg.BindEnv(conf, bindings); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.HTTP.Root != "/" {
|
||||
cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user