normalize web
This commit is contained in:
+3
-22
@@ -62,14 +62,9 @@ func ParseConfig(c *cli.Context, cfg *config.Config) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// load all env variables relevant to the config in the current context.
|
|
||||||
conf.LoadOSEnv(config.GetEnv(), false)
|
conf.LoadOSEnv(config.GetEnv(), false)
|
||||||
|
bindings := config.StructMappings(cfg)
|
||||||
if err = cfg.UnmapEnv(conf); err != nil {
|
return ociscfg.BindEnv(conf, bindings)
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SutureService allows for the web command to be embedded and supervised by a suture supervisor tree.
|
// SutureService allows for the web command to be embedded and supervised by a suture supervisor tree.
|
||||||
@@ -79,11 +74,7 @@ type SutureService struct {
|
|||||||
|
|
||||||
// NewSutureService creates a new web.SutureService
|
// NewSutureService creates a new web.SutureService
|
||||||
func NewSutureService(cfg *ociscfg.Config) suture.Service {
|
func NewSutureService(cfg *ociscfg.Config) suture.Service {
|
||||||
inheritLogging(cfg)
|
cfg.Web.Log = cfg.Log
|
||||||
if cfg.Mode == 0 {
|
|
||||||
cfg.Web.Supervised = true
|
|
||||||
}
|
|
||||||
cfg.Web.Log.File = cfg.Log.File
|
|
||||||
return SutureService{
|
return SutureService{
|
||||||
cfg: cfg.Web,
|
cfg: cfg.Web,
|
||||||
}
|
}
|
||||||
@@ -97,13 +88,3 @@ func (s SutureService) Serve(ctx context.Context) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// inheritLogging is a poor man's global logging state tip-toeing around circular dependencies. It sets the logging
|
|
||||||
// of the service to whatever is in the higher config (in this case coming from ocis.yaml) and sets them as defaults,
|
|
||||||
// being overwritten when the extension parses its config file / env variables.
|
|
||||||
func inheritLogging(cfg *ociscfg.Config) {
|
|
||||||
cfg.Web.Log.File = cfg.Log.File
|
|
||||||
cfg.Web.Log.Color = cfg.Log.Color
|
|
||||||
cfg.Web.Log.Pretty = cfg.Log.Pretty
|
|
||||||
cfg.Web.Log.Level = cfg.Log.Level
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -30,9 +30,6 @@ func Server(cfg *config.Config) *cli.Command {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
logger := NewLogger(cfg)
|
|
||||||
logger.Debug().Str("service", "web").Msg("ignoring config file parsing when running supervised")
|
|
||||||
|
|
||||||
// build well known openid-configuration endpoint if it is not set
|
// build well known openid-configuration endpoint if it is not set
|
||||||
if cfg.Web.Config.OpenIDConnect.MetadataURL == "" {
|
if cfg.Web.Config.OpenIDConnect.MetadataURL == "" {
|
||||||
cfg.Web.Config.OpenIDConnect.MetadataURL = strings.TrimRight(cfg.Web.Config.OpenIDConnect.Authority, "/") + "/.well-known/openid-configuration"
|
cfg.Web.Config.OpenIDConnect.MetadataURL = strings.TrimRight(cfg.Web.Config.OpenIDConnect.Authority, "/") + "/.well-known/openid-configuration"
|
||||||
|
|||||||
@@ -2,22 +2,12 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
|
|
||||||
gofig "github.com/gookit/config/v2"
|
"github.com/owncloud/ocis/ocis-pkg/shared"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultIngressURL = "https://localhost:9200"
|
const defaultIngressURL = "https://localhost:9200"
|
||||||
|
|
||||||
// Log defines the available logging configuration.
|
|
||||||
type Log struct {
|
|
||||||
Level string `mapstructure:"level"`
|
|
||||||
Pretty bool `mapstructure:"pretty"`
|
|
||||||
Color bool `mapstructure:"color"`
|
|
||||||
File string `mapstructure:"file"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Debug defines the available debug configuration.
|
// Debug defines the available debug configuration.
|
||||||
type Debug struct {
|
type Debug struct {
|
||||||
Addr string `mapstructure:"addr"`
|
Addr string `mapstructure:"addr"`
|
||||||
@@ -98,13 +88,13 @@ type Web struct {
|
|||||||
|
|
||||||
// Config combines all available configuration parts.
|
// Config combines all available configuration parts.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
File string `mapstructure:"file"`
|
File string `mapstructure:"file"`
|
||||||
Log Log `mapstructure:"log"`
|
Log shared.Log `mapstructure:"log"`
|
||||||
Debug Debug `mapstructure:"debug"`
|
Debug Debug `mapstructure:"debug"`
|
||||||
HTTP HTTP `mapstructure:"http"`
|
HTTP HTTP `mapstructure:"http"`
|
||||||
Tracing Tracing `mapstructure:"tracing"`
|
Tracing Tracing `mapstructure:"tracing"`
|
||||||
Asset Asset `mapstructure:"asset"`
|
Asset Asset `mapstructure:"asset"`
|
||||||
Web Web `mapstructure:"web"`
|
Web Web `mapstructure:"web"`
|
||||||
|
|
||||||
Context context.Context
|
Context context.Context
|
||||||
Supervised bool
|
Supervised bool
|
||||||
@@ -117,7 +107,7 @@ func New() *Config {
|
|||||||
|
|
||||||
func DefaultConfig() *Config {
|
func DefaultConfig() *Config {
|
||||||
return &Config{
|
return &Config{
|
||||||
Log: Log{},
|
Log: shared.Log{},
|
||||||
Debug: Debug{
|
Debug: Debug{
|
||||||
Addr: "127.0.0.1:9104",
|
Addr: "127.0.0.1:9104",
|
||||||
Token: "",
|
Token: "",
|
||||||
@@ -171,38 +161,3 @@ func GetEnv() []string {
|
|||||||
|
|
||||||
return r
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,13 +1,17 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
type mapping struct {
|
import "github.com/owncloud/ocis/ocis-pkg/shared"
|
||||||
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. 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.
|
// structMappings binds a set of environment variables to a destination on cfg.
|
||||||
func structMappings(cfg *Config) []mapping {
|
func structMappings(cfg *Config) []shared.EnvBinding {
|
||||||
return []mapping{
|
return []shared.EnvBinding{
|
||||||
{
|
{
|
||||||
EnvVars: []string{"WEB_LOG_LEVEL", "OCIS_LOG_LEVEL"},
|
EnvVars: []string{"WEB_LOG_LEVEL", "OCIS_LOG_LEVEL"},
|
||||||
Destination: &cfg.Log.Level,
|
Destination: &cfg.Log.Level,
|
||||||
Reference in New Issue
Block a user