migrate web to the new config scheme

This commit is contained in:
A.Unger
2021-11-05 13:33:03 +01:00
parent 11855bcaab
commit ca997e5bfc
9 changed files with 242 additions and 60 deletions

View File

@@ -163,7 +163,7 @@ func DefaultConfig() *Config {
Addr: "127.0.0.1:9181",
Namespace: "com.owncloud.web",
Root: "/",
CacheTTL: 604800,
CacheTTL: 604800, // 7 days
CORS: CORS{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},

View File

@@ -122,7 +122,7 @@ func New() *Config {
GraphExplorer: graphExplorer.DefaultConfig(),
OCS: ocs.DefaultConfig(),
Settings: settings.DefaultConfig(),
Web: web.New(),
Web: web.DefaultConfig(),
Storage: storage.New(),
Store: store.New(),
Thumbnails: thumbnails.DefaultConfig(),

View File

@@ -5,7 +5,6 @@ import (
"github.com/owncloud/ocis/ocis/pkg/register"
"github.com/owncloud/ocis/web/pkg/command"
svcconfig "github.com/owncloud/ocis/web/pkg/config"
"github.com/owncloud/ocis/web/pkg/flagset"
"github.com/urfave/cli/v2"
)
@@ -15,7 +14,6 @@ func WebCommand(cfg *config.Config) *cli.Command {
Name: "web",
Usage: "Start web server",
Category: "Extensions",
Flags: flagset.ServerWithConfig(cfg.Web),
Before: func(ctx *cli.Context) error {
return ParseConfig(ctx, cfg)
},

View File

@@ -115,7 +115,7 @@ func DefaultConfig() *Config {
Addr: "127.0.0.1:9190",
Namespace: "com.owncloud.web",
Root: "/",
CacheTTL: 604800, // 10 days
CacheTTL: 604800, // 7 days
CORS: CORS{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},

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)
}
}

View File

@@ -3,15 +3,11 @@ package command
import (
"context"
"os"
"strings"
"github.com/owncloud/ocis/ocis-pkg/sync"
ociscfg "github.com/owncloud/ocis/ocis-pkg/config"
"github.com/owncloud/ocis/ocis-pkg/log"
"github.com/owncloud/ocis/ocis-pkg/version"
"github.com/owncloud/ocis/web/pkg/config"
"github.com/spf13/viper"
"github.com/thejerf/suture/v4"
"github.com/urfave/cli/v2"
)
@@ -59,46 +55,18 @@ func NewLogger(cfg *config.Config) log.Logger {
)
}
// ParseConfig loads proxy 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("WEB")
viper.AutomaticEnv()
if c.IsSet("config-file") {
viper.SetConfigFile(c.String("config-file"))
} else {
viper.SetConfigName("web")
viper.AddConfigPath("/etc/ocis")
viper.AddConfigPath("$HOME/.ocis")
viper.AddConfigPath("./config")
conf, err := ociscfg.BindSourcesToStructs("web", cfg)
if err != nil {
return err
}
if err := viper.ReadInConfig(); err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
logger.Debug().
Msg("no config found on preconfigured location")
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

View File

@@ -9,7 +9,6 @@ import (
"github.com/oklog/run"
"github.com/owncloud/ocis/ocis-pkg/sync"
"github.com/owncloud/ocis/web/pkg/config"
"github.com/owncloud/ocis/web/pkg/flagset"
"github.com/owncloud/ocis/web/pkg/metrics"
"github.com/owncloud/ocis/web/pkg/server/debug"
"github.com/owncloud/ocis/web/pkg/server/http"
@@ -22,24 +21,16 @@ 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.TrimRight(cfg.HTTP.Root, "/")
}
// StringSliceFlag doesn't support Destination
// UPDATE Destination on string flags supported. Wait for https://github.com/urfave/cli/pull/1078 to get to micro/cli
if len(ctx.StringSlice("web-config-app")) > 0 {
cfg.Web.Config.Apps = ctx.StringSlice("web-config-app")
if err := ParseConfig(ctx, cfg); err != nil {
return err
}
if !cfg.Supervised {
if err := ParseConfig(ctx, cfg); err != nil {
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

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 {
@@ -96,7 +102,6 @@ type Config struct {
HTTP HTTP
Tracing Tracing
Asset Asset
OIDC OIDC
Web Web
Context context.Context
@@ -107,3 +112,95 @@ type Config struct {
func New() *Config {
return &Config{}
}
func DefaultConfig() *Config {
return &Config{
Log: Log{},
Debug: Debug{
Addr: "127.0.0.1:9104",
Token: "",
Pprof: false,
Zpages: false,
},
HTTP: HTTP{
Addr: "127.0.0.1:9100",
Root: "/",
Namespace: "com.owncloud.web",
CacheTTL: 604800, // 7 days
},
Tracing: Tracing{
Enabled: false,
Type: "jaeger",
Endpoint: "",
Collector: "",
Service: "web",
},
Asset: Asset{
Path: "",
},
Web: Web{
Path: "",
ThemeServer: "https://localhost:9200",
ThemePath: "/themes/owncloud/theme.json",
Config: WebConfig{
Server: "https://localhost:9200",
Theme: "",
Version: "0.1.0",
OpenIDConnect: OIDC{
MetadataURL: "",
Authority: "https://localhost:9200",
ClientID: "web",
ResponseType: "code",
Scope: "openid profile email",
},
Apps: []string{"files", "search", "media-viewer", "external"},
},
},
}
}
// 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
}

128
web/pkg/config/env.go Normal file
View File

@@ -0,0 +1,128 @@
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{"WEB_LOG_LEVEL", "OCIS_LOG_LEVEL"},
Destination: &cfg.Log.Level,
},
{
EnvVars: []string{"WEB_LOG_PRETTY", "OCIS_LOG_PRETTY"},
Destination: &cfg.Log.Pretty,
},
{
EnvVars: []string{"WEB_LOG_COLOR", "OCIS_LOG_COLOR"},
Destination: &cfg.Log.Color,
},
{
EnvVars: []string{"WEB_LOG_FILE", "OCIS_LOG_FILE"},
Destination: &cfg.Log.File,
},
{
EnvVars: []string{"WEB_CONFIG_FILE"},
Destination: &cfg.File,
},
{
EnvVars: []string{"WEB_TRACING_ENABLED", "OCIS_TRACING_ENABLED"},
Destination: &cfg.Tracing.Enabled,
},
{
EnvVars: []string{"WEB_TRACING_TYPE", "OCIS_TRACING_TYPE"},
Destination: &cfg.Tracing.Type,
},
{
EnvVars: []string{"WEB_TRACING_ENDPOINT", "OCIS_TRACING_ENDPOINT"},
Destination: &cfg.Tracing.Endpoint,
},
{
EnvVars: []string{"WEB_TRACING_COLLECTOR", "OCIS_TRACING_COLLECTOR"},
Destination: &cfg.Tracing.Collector,
},
{
EnvVars: []string{"WEB_TRACING_SERVICE"},
Destination: &cfg.Tracing.Service,
},
{
EnvVars: []string{"WEB_DEBUG_ADDR"},
Destination: &cfg.Debug.Addr,
},
{
EnvVars: []string{"WEB_DEBUG_TOKEN"},
Destination: &cfg.Debug.Token,
},
{
EnvVars: []string{"WEB_DEBUG_PPROF"},
Destination: &cfg.Debug.Pprof,
},
{
EnvVars: []string{"WEB_DEBUG_ZPAGES"},
Destination: &cfg.Debug.Zpages,
},
{
EnvVars: []string{"WEB_HTTP_ADDR"},
Destination: &cfg.HTTP.Addr,
},
{
EnvVars: []string{"WEB_HTTP_ROOT"},
Destination: &cfg.HTTP.Root,
},
{
EnvVars: []string{"WEB_NAMESPACE"},
Destination: &cfg.HTTP.Namespace,
},
{
EnvVars: []string{"WEB_CACHE_TTL"},
Destination: &cfg.HTTP.CacheTTL,
},
{
EnvVars: []string{"WEB_ASSET_PATH"},
Destination: &cfg.Asset.Path,
},
{
EnvVars: []string{"WEB_UI_CONFIG"},
Destination: &cfg.Web.Path,
},
{
EnvVars: []string{"WEB_UI_CONFIG_SERVER", "OCIS_URL"}, // WEB_UI_CONFIG_SERVER takes precedence over OCIS_URL
Destination: &cfg.Web.Config.Server,
},
{
EnvVars: []string{"WEB_UI_THEME_SERVER", "OCIS_URL"}, // WEB_UI_THEME_SERVER takes precedence over OCIS_URL
Destination: &cfg.Web.ThemeServer,
},
{
EnvVars: []string{"WEB_UI_THEME_PATH"},
Destination: &cfg.Web.ThemePath,
},
{
EnvVars: []string{"WEB_UI_CONFIG_VERSION"},
Destination: &cfg.Web.Config.Version,
},
{
EnvVars: []string{"WEB_OIDC_METADATA_URL"},
Destination: &cfg.Web.Config.OpenIDConnect.MetadataURL,
},
{
EnvVars: []string{"WEB_OIDC_AUTHORITY", "OCIS_URL"}, // WEB_OIDC_AUTHORITY takes precedence over OCIS_URL
Destination: &cfg.Web.Config.OpenIDConnect.Authority,
},
{
EnvVars: []string{"WEB_OIDC_CLIENT_ID"},
Destination: &cfg.Web.Config.OpenIDConnect.ClientID,
},
{
EnvVars: []string{"WEB_OIDC_RESPONSE_TYPE"},
Destination: &cfg.Web.Config.OpenIDConnect.ResponseType,
},
{
EnvVars: []string{"WEB_OIDC_SCOPE"},
Destination: &cfg.Web.Config.OpenIDConnect.Scope,
},
}
}