migrate webdav to the new config scheme

This commit is contained in:
A.Unger
2021-11-05 13:45:15 +01:00
parent ca997e5bfc
commit ad72f7574c
7 changed files with 270 additions and 131 deletions

View File

@@ -126,7 +126,7 @@ func New() *Config {
Storage: storage.New(),
Store: store.New(),
Thumbnails: thumbnails.DefaultConfig(),
WebDAV: webdav.New(),
WebDAV: webdav.DefaultConfig(),
}
}

View File

@@ -10,60 +10,60 @@ import (
// Log defines the available logging configuration.
type Log struct {
Level string
Pretty bool
Color bool
File string
Level string `mapstructure:"level"`
Pretty bool `mapstructure:"pretty"`
Color bool `mapstructure:"color"`
File string `mapstructure:"file"`
}
// Debug defines the available debug configuration.
type Debug struct {
Addr string
Token string
Pprof bool
Zpages bool
Addr string `mapstructure:"addr"`
Token string `mapstructure:"token"`
Pprof bool `mapstructure:"pprof"`
Zpages bool `mapstructure:"zpages"`
}
// HTTP defines the available http configuration.
type HTTP struct {
Addr string
Root string
Namespace string
CacheTTL int
Addr string `mapstructure:"addr"`
Root string `mapstructure:"root"`
Namespace string `mapstructure:"namespace"`
CacheTTL int `mapstructure:"cache_ttl"`
}
// Tracing defines the available tracing configuration.
type Tracing struct {
Enabled bool
Type string
Endpoint string
Collector string
Service string
Enabled bool `mapstructure:"enabled"`
Type string `mapstructure:"type"`
Endpoint string `mapstructure:"endpoint"`
Collector string `mapstructure:"collector"`
Service string `mapstructure:"service"`
}
// Asset defines the available asset configuration.
type Asset struct {
Path string
Path string `mapstructure:"path"`
}
// WebConfig defines the available web configuration for a dynamically rendered config.json.
type WebConfig struct {
Server string `json:"server,omitempty"`
Theme string `json:"theme,omitempty"`
Version string `json:"version,omitempty"` // TODO what is version used for?
OpenIDConnect OIDC `json:"openIdConnect,omitempty"`
Apps []string `json:"apps"` // TODO add nilasempty when https://go-review.googlesource.com/c/go/+/205897/ is released
ExternalApps []ExternalApp `json:"external_apps,omitempty"`
Options map[string]interface{} `json:"options,omitempty"`
Server string `json:"server,omitempty",mapstructure:"server"`
Theme string `json:"theme,omitempty",mapstructure:"theme"`
Version string `json:"version,omitempty",mapstructure:"version"` // TODO what is version used for?
OpenIDConnect OIDC `json:"openIdConnect,omitempty",mapstructure:"oids"`
Apps []string `json:"apps",mapstructure:"apps"` // TODO add nil as empty when https://go-review.googlesource.com/c/go/+/205897/ is released
ExternalApps []ExternalApp `json:"external_apps,omitempty",mapstructure:"external_apps"`
Options map[string]interface{} `json:"options,omitempty",mapstructure:"options"`
}
// OIDC defines the available oidc configuration
type OIDC struct {
MetadataURL string `json:"metadata_url,omitempty"`
Authority string `json:"authority,omitempty"`
ClientID string `json:"client_id,omitempty"`
ResponseType string `json:"response_type,omitempty"`
Scope string `json:"scope,omitempty"`
MetadataURL string `json:"metadata_url,omitempty",mapstructure:"metadata_url"`
Authority string `json:"authority,omitempty",mapstructure:"authority"`
ClientID string `json:"client_id,omitempty",mapstructure:"client_id"`
ResponseType string `json:"response_type,omitempty",mapstructure:"response_type"`
Scope string `json:"scope,omitempty",mapstructure:"scope"`
}
// ExternalApp defines an external web app.
@@ -75,34 +75,34 @@ type OIDC struct {
// }
// }
type ExternalApp struct {
ID string `json:"id,omitempty"`
Path string `json:"path,omitempty"`
ID string `json:"id,omitempty",mapstructure:"id"`
Path string `json:"path,omitempty",mapstructure:"path"`
// Config is completely dynamic, because it depends on the extension
Config map[string]interface{} `json:"config,omitempty"`
Config map[string]interface{} `json:"config,omitempty",mapstructure:"config"`
}
// ExternalAppConfig defines an external web app configuration.
type ExternalAppConfig struct {
URL string `json:"url,omitempty"`
URL string `json:"url,omitempty",mapstructure:"url"`
}
// Web defines the available web configuration.
type Web struct {
Path string
ThemeServer string // used to build Theme in WebConfig
ThemePath string // used to build Theme in WebConfig
Config WebConfig
Path string `mapstructure:"path"`
ThemeServer string `mapstructure:"theme_server"` // used to build Theme in WebConfig
ThemePath string `mapstructure:"theme_path"` // used to build Theme in WebConfig
Config WebConfig `mapstructure:"config"`
}
// Config combines all available configuration parts.
type Config struct {
File string
Log Log
Debug Debug
HTTP HTTP
Tracing Tracing
Asset Asset
Web Web
File string `mapstructure:"file"`
Log Log `mapstructure:"log"`
Debug Debug `mapstructure:"debug"`
HTTP HTTP `mapstructure:"http"`
Tracing Tracing `mapstructure:"tracing"`
Asset Asset `mapstructure:"asset"`
Web Web `mapstructure:"web"`
Context context.Context
Supervised bool

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/webdav/pkg/config"
"github.com/spf13/viper"
"github.com/thejerf/suture/v4"
"github.com/urfave/cli/v2"
)
@@ -65,46 +61,18 @@ func NewLogger(cfg *config.Config) log.Logger {
)
}
// ParseConfig loads webdav 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("WEBDAV")
viper.AutomaticEnv()
if c.IsSet("config-file") {
viper.SetConfigFile(c.String("config-file"))
} else {
viper.SetConfigName("webdav")
viper.AddConfigPath("/etc/ocis")
viper.AddConfigPath("$HOME/.ocis")
viper.AddConfigPath("./config")
conf, err := ociscfg.BindSourcesToStructs("webdav", 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

@@ -22,23 +22,15 @@ func Server(cfg *config.Config) *cli.Command {
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.TrimSuffix(cfg.HTTP.Root, "/")
}
if !cfg.Supervised {
return ParseConfig(ctx, cfg)
}
if origins := ctx.StringSlice("cors-allowed-origins"); len(origins) != 0 {
cfg.HTTP.CORS.AllowedOrigins = origins
}
if methods := ctx.StringSlice("cors-allowed-methods"); len(methods) != 0 {
cfg.HTTP.CORS.AllowedMethods = methods
}
if headers := ctx.StringSlice("cors-allowed-headers"); len(headers) != 0 {
cfg.HTTP.CORS.AllowedOrigins = headers
if err := ParseConfig(ctx, cfg); err != nil {
return err
}
logger := NewLogger(cfg)
logger.Debug().Str("service", "webdav").Msg("ignoring config file parsing when running supervised")
return nil
},

View File

@@ -1,64 +1,70 @@
package config
import "context"
import (
"context"
"fmt"
"reflect"
gofig "github.com/gookit/config/v2"
)
// Log defines the available logging configuration.
type Log struct {
Level string
Pretty bool
Color bool
File string
Level string `mapstructure:"level"`
Pretty bool `mapstructure:"pretty"`
Color bool `mapstructure:"color"`
File string `mapstructure:"file"`
}
// Debug defines the available debug configuration.
type Debug struct {
Addr string
Token string
Pprof bool
Zpages bool
Addr string `mapstructure:"addr"`
Token string `mapstructure:"token"`
Pprof bool `mapstructure:"pprof"`
Zpages bool `mapstructure:"zpages"`
}
// CORS defines the available cors configuration.
type CORS struct {
AllowedOrigins []string
AllowedMethods []string
AllowedHeaders []string
AllowCredentials bool
AllowedOrigins []string `mapstructure:"allowed_origins"`
AllowedMethods []string `mapstructure:"allowed_methods"`
AllowedHeaders []string `mapstructure:"allowed_headers"`
AllowCredentials bool `mapstructure:"allow_credentials"`
}
// HTTP defines the available http configuration.
type HTTP struct {
Addr string
Root string
CORS CORS
Addr string `mapstructure:"addr"`
Root string `mapstructure:"root"`
CORS CORS `mapstructure:"cors"`
}
// Service defines the available service configuration.
type Service struct {
Name string
Namespace string
Version string
Name string `mapstructure:"name"`
Namespace string `mapstructure:"namespace"`
Version string `mapstructure:"version"`
}
// Tracing defines the available tracing configuration.
type Tracing struct {
Enabled bool
Type string
Endpoint string
Collector string
Service string
Enabled bool `mapstructure:"enabled"`
Type string `mapstructure:"type"`
Endpoint string `mapstructure:"endpoint"`
Collector string `mapstructure:"collector"`
Service string `mapstructure:"service"`
}
// Config combines all available configuration parts.
type Config struct {
File string
Log Log
Debug Debug
HTTP HTTP
Tracing Tracing
Service Service
OcisPublicURL string
WebdavNamespace string
File string `mapstructure:"file"`
Log Log `mapstructure:"log"`
Debug Debug `mapstructure:"debug"`
HTTP HTTP `mapstructure:"http"`
Tracing Tracing `mapstructure:"tracing"`
Service Service `mapstructure:"service"`
OcisPublicURL string `mapstructure:"ocis_public_url"`
WebdavNamespace string `mapstructure:"webdav_namespace"`
Context context.Context
Supervised bool
@@ -68,3 +74,84 @@ type Config struct {
func New() *Config {
return &Config{}
}
func DefaultConfig() *Config {
return &Config{
Log: Log{},
Debug: Debug{
Addr: "",
Token: "",
Pprof: false,
Zpages: false,
},
HTTP: HTTP{
Addr: "127.0.0.1:9115",
Root: "/",
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: "webdav",
},
Service: Service{
Name: "webdav",
Namespace: "com.owncloud.web",
},
OcisPublicURL: "https://127.0.0.1:9200",
WebdavNamespace: "/home",
}
}
// 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
}

92
webdav/pkg/config/env.go Normal file
View File

@@ -0,0 +1,92 @@
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{"WEBDAV_LOG_FILE", "OCIS_LOG_FILE"},
Destination: &cfg.Log.File,
},
{
EnvVars: []string{"WEBDAV_LOG_LEVEL", "OCIS_LOG_LEVEL"},
Destination: &cfg.Log.Level,
},
{
EnvVars: []string{"WEBDAV_LOG_PRETTY", "OCIS_LOG_PRETTY"},
Destination: &cfg.Log.Pretty,
},
{
EnvVars: []string{"WEBDAV_LOG_COLOR", "OCIS_LOG_COLOR"},
Destination: &cfg.Log.Color,
},
{
EnvVars: []string{"WEBDAV_CONFIG_FILE"},
Destination: &cfg.File,
},
{
EnvVars: []string{"WEBDAV_TRACING_ENABLED", "OCIS_TRACING_ENABLED"},
Destination: &cfg.Tracing.Enabled,
},
{
EnvVars: []string{"WEBDAV_TRACING_TYPE", "OCIS_TRACING_TYPE"},
Destination: &cfg.Tracing.Type,
},
{
EnvVars: []string{"WEBDAV_TRACING_ENDPOINT", "OCIS_TRACING_ENDPOINT"},
Destination: &cfg.Tracing.Endpoint,
},
{
EnvVars: []string{"WEBDAV_TRACING_COLLECTOR", "OCIS_TRACING_COLLECTOR"},
Destination: &cfg.Tracing.Collector,
},
{
EnvVars: []string{"WEBDAV_TRACING_SERVICE"},
Destination: &cfg.Tracing.Service,
},
{
EnvVars: []string{"WEBDAV_DEBUG_ADDR"},
Destination: &cfg.Debug.Addr,
},
{
EnvVars: []string{"WEBDAV_DEBUG_TOKEN"},
Destination: &cfg.Debug.Token,
},
{
EnvVars: []string{"WEBDAV_DEBUG_PPROF"},
Destination: &cfg.Debug.Pprof,
},
{
EnvVars: []string{"WEBDAV_DEBUG_ZPAGES"},
Destination: &cfg.Debug.Zpages,
},
{
EnvVars: []string{"WEBDAV_HTTP_ADDR"},
Destination: &cfg.HTTP.Addr,
},
{
EnvVars: []string{"WEBDAV_HTTP_NAMESPACE"},
Destination: &cfg.Service.Namespace,
},
{
EnvVars: []string{"WEBDAV_SERVICE_NAME"},
Destination: &cfg.Service.Name,
},
{
EnvVars: []string{"WEBDAV_HTTP_ROOT"},
Destination: &cfg.HTTP.Root,
},
{
EnvVars: []string{"OCIS_PUBLIC_URL", "OCIS_URL"},
Destination: &cfg.OcisPublicURL,
},
{
EnvVars: []string{"STORAGE_WEBDAV_NAMESPACE"},
Destination: &cfg.WebdavNamespace,
},
}
}