switch all other services to struct tag based env config

This commit is contained in:
Willy Kloucek
2022-01-07 15:47:47 +00:00
committed by Jörn Friedrich Dreyer
parent 288d6c469e
commit 6990e7d660
116 changed files with 1024 additions and 1991 deletions
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"net/http"
"github.com/owncloud/ocis/thumbnails/pkg/config"
"github.com/owncloud/ocis/thumbnails/pkg/logging"
"github.com/urfave/cli/v2"
)
@@ -17,7 +18,7 @@ func Health(cfg *config.Config) *cli.Command {
return ParseConfig(c, cfg)
},
Action: func(c *cli.Context) error {
logger := NewLogger(cfg)
logger := logging.Configure(cfg.Service.Name, cfg.Log)
resp, err := http.Get(
fmt.Sprintf(
+27 -31
View File
@@ -4,14 +4,13 @@ import (
"context"
"os"
"github.com/owncloud/ocis/ocis-pkg/shared"
"github.com/imdario/mergo"
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/thumbnails/pkg/config"
"github.com/thejerf/suture/v4"
"github.com/urfave/cli/v2"
"github.com/wkloucek/envdecode"
)
// Execute is the entry point for the ocis-thumbnails command.
@@ -31,7 +30,7 @@ func Execute(cfg *config.Config) error {
Before: func(c *cli.Context) error {
cfg.Service.Version = version.String
return nil
return ParseConfig(c, cfg)
},
Commands: []*cli.Command{
@@ -54,40 +53,37 @@ func Execute(cfg *config.Config) error {
return app.Run(os.Args)
}
// NewLogger initializes a service-specific logger instance.
func NewLogger(cfg *config.Config) log.Logger {
return log.NewLogger(
log.Name("thumbnails"),
log.Level(cfg.Log.Level),
log.Pretty(cfg.Log.Pretty),
log.Color(cfg.Log.Color),
log.File(cfg.Log.File),
)
}
// ParseConfig loads configuration from Viper known paths.
// ParseConfig loads glauth configuration from known paths.
// ParseConfig loads accounts configuration from known paths.
func ParseConfig(c *cli.Context, cfg *config.Config) error {
conf, err := ociscfg.BindSourcesToStructs("thumbnails", cfg)
_, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg)
if err != nil {
return err
}
// provide with defaults for shared logging, since we need a valid destination address for BindEnv.
if cfg.Log == nil && cfg.Commons != nil && cfg.Commons.Log != nil {
cfg.Log = &shared.Log{
Level: cfg.Commons.Log.Level,
Pretty: cfg.Commons.Log.Pretty,
Color: cfg.Commons.Log.Color,
File: cfg.Commons.Log.File,
}
} else if cfg.Log == nil && cfg.Commons == nil {
cfg.Log = &shared.Log{}
//if cfg.Log == nil && cfg.Commons != nil && cfg.Commons.Log != nil {
// cfg.Log = &shared.Log{
// Level: cfg.Commons.Log.Level,
// Pretty: cfg.Commons.Log.Pretty,
// Color: cfg.Commons.Log.Color,
// File: cfg.Commons.Log.File,
// }
//} else if cfg.Log == nil && cfg.Commons == nil {
// cfg.Log = &shared.Log{}
//}
// load all env variables relevant to the config in the current context.
envCfg := config.Config{}
if err := envdecode.Decode(&envCfg); err != nil && err.Error() != "none of the target fields were set from environment variables" {
return err
}
conf.LoadOSEnv(config.GetEnv(cfg), false)
bindings := config.StructMappings(cfg)
return ociscfg.BindEnv(conf, bindings)
// merge environment variable config on top of the current config
if err := mergo.Merge(cfg, envCfg, mergo.WithOverride); err != nil {
return err
}
return nil
}
// SutureService allows for the thumbnails command to be embedded and supervised by a suture supervisor tree.
@@ -97,7 +93,7 @@ type SutureService struct {
// NewSutureService creates a new thumbnails.SutureService
func NewSutureService(cfg *ociscfg.Config) suture.Service {
cfg.Thumbnails.Commons = cfg.Commons
//cfg.Thumbnails.Commons = cfg.Commons
return SutureService{
cfg: cfg.Thumbnails,
}
+4 -2
View File
@@ -6,6 +6,7 @@ import (
"github.com/oklog/run"
"github.com/owncloud/ocis/thumbnails/pkg/config"
"github.com/owncloud/ocis/thumbnails/pkg/logging"
"github.com/owncloud/ocis/thumbnails/pkg/metrics"
"github.com/owncloud/ocis/thumbnails/pkg/server/debug"
"github.com/owncloud/ocis/thumbnails/pkg/server/grpc"
@@ -25,8 +26,9 @@ func Server(cfg *config.Config) *cli.Command {
return nil
},
Action: func(c *cli.Context) error {
logger := NewLogger(cfg)
if err := tracing.Configure(cfg); err != nil {
logger := logging.Configure(cfg.Service.Name, cfg.Log)
err := tracing.Configure(cfg)
if err != nil {
return err
}
+35 -33
View File
@@ -5,49 +5,56 @@ import (
"path"
"github.com/owncloud/ocis/ocis-pkg/config/defaults"
"github.com/owncloud/ocis/ocis-pkg/shared"
)
// Debug defines the available debug configuration.
type Debug struct {
Addr string `ocisConfig:"addr"`
Token string `ocisConfig:"token"`
Pprof bool `ocisConfig:"pprof"`
Zpages bool `ocisConfig:"zpages"`
Addr string `ocisConfig:"addr" env:"THUMBNAILS_DEBUG_ADDR"`
Token string `ocisConfig:"token" env:"THUMBNAILS_DEBUG_TOKEN"`
Pprof bool `ocisConfig:"pprof" env:"THUMBNAILS_DEBUG_PPROF"`
Zpages bool `ocisConfig:"zpages" env:"THUMBNAILS_DEBUG_ZPAGES"`
}
// GRPC defines the available grpc configuration.
type GRPC struct {
Addr string `ocisConfig:"addr"`
Addr string `ocisConfig:"addr" env:"THUMBNAILS_GRPC_ADDR"`
Namespace string
}
// Service provides configuration options for the service
// Service defines the available service configuration.
type Service struct {
Name string `ocisConfig:"name"`
Version string `ocisConfig:"version"`
Name string
Version string
}
// Tracing defines the available tracing configuration.
type Tracing struct {
Enabled bool `ocisConfig:"enabled"`
Type string `ocisConfig:"type"`
Endpoint string `ocisConfig:"endpoint"`
Collector string `ocisConfig:"collector"`
Service string `ocisConfig:"service"`
Enabled bool `ocisConfig:"enabled" env:"OCIS_TRACING_ENABLED;THUMBNAILS_TRACING_ENABLED"`
Type string `ocisConfig:"type" env:"OCIS_TRACING_TYPE;THUMBNAILS_TRACING_TYPE"`
Endpoint string `ocisConfig:"endpoint" env:"OCIS_TRACING_ENDPOINT;THUMBNAILS_TRACING_ENDPOINT"`
Collector string `ocisConfig:"collector" env:"OCIS_TRACING_COLLECTOR;THUMBNAILS_TRACING_COLLECTOR"`
Service string `ocisConfig:"service" env:"THUMBNAILS_TRACING_SERVICE"` //TODO: should this be an ID? or the same as Service.Name?
}
// Log defines the available log configuration.
type Log struct {
Level string `mapstructure:"level" env:"OCIS_LOG_LEVEL;THUMBNAILS_LOG_LEVEL"`
Pretty bool `mapstructure:"pretty" env:"OCIS_LOG_PRETTY;THUMBNAILS_LOG_PRETTY"`
Color bool `mapstructure:"color" env:"OCIS_LOG_COLOR;THUMBNAILS_LOG_COLOR"`
File string `mapstructure:"file" env:"OCIS_LOG_FILE;THUMBNAILS_LOG_FILE"`
}
// Config combines all available configuration parts.
type Config struct {
*shared.Commons
Service Service `ocisConfig:"service"`
File string `ocisConfig:"file"`
Log *shared.Log `ocisConfig:"log"`
Debug Debug `ocisConfig:"debug"`
GRPC GRPC `ocisConfig:"grpc"`
Service Service `ocisConfig:"service"`
Tracing Tracing `ocisConfig:"tracing"`
Thumbnail Thumbnail `ocisConfig:"thumbnail"`
Tracing Tracing `ocisConfig:"tracing"`
Log Log `ocisConfig:"log"`
Debug Debug `ocisConfig:"debug"`
GRPC GRPC `ocisConfig:"grpc"`
Thumbnail Thumbnail `ocisConfig:"thumbnail"`
Context context.Context
Supervised bool
@@ -55,7 +62,7 @@ type Config struct {
// FileSystemStorage defines the available filesystem storage configuration.
type FileSystemStorage struct {
RootDirectory string `ocisConfig:"root_directory"`
RootDirectory string `ocisConfig:"root_directory" env:"THUMBNAILS_FILESYSTEMSTORAGE_ROOT"`
}
// FileSystemSource defines the available filesystem source configuration.
@@ -65,17 +72,12 @@ type FileSystemSource struct {
// Thumbnail defines the available thumbnail related configuration.
type Thumbnail struct {
Resolutions []string `ocisConfig:"resolutions"`
Resolutions []string `ocisConfig:"resolutions"` // TODO: how to configure
FileSystemStorage FileSystemStorage `ocisConfig:"filesystem_storage"`
WebdavAllowInsecure bool `ocisConfig:"webdav_allow_insecure"`
CS3AllowInsecure bool `ocisConfig:"cs3_allow_insecure"`
RevaGateway string `ocisConfig:"reva_gateway"`
FontMapFile string `ocisConfig:"font_map_file"`
}
// New initializes a new configuration with or without defaults.
func New() *Config {
return &Config{}
WebdavAllowInsecure bool `ocisConfig:"webdav_allow_insecure" env:"OCIS_INSECURE;THUMBNAILS_WEBDAVSOURCE_INSECURE"`
CS3AllowInsecure bool `ocisConfig:"cs3_allow_insecure" env:"OCIS_INSECURE;THUMBNAILS_CS3SOURCE_INSECURE"`
RevaGateway string `ocisConfig:"reva_gateway" env:"REVA_GATEWAY"`
FontMapFile string `ocisConfig:"font_map_file" env:"THUMBNAILS_TXT_FONTMAP_FILE"`
}
func DefaultConfig() *Config {
-111
View File
@@ -1,111 +0,0 @@
package config
import "github.com/owncloud/ocis/ocis-pkg/shared"
// 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(cfg *Config) []string {
var r = make([]string, len(structMappings(cfg)))
for i := range structMappings(cfg) {
r = append(r, structMappings(cfg)[i].EnvVars...)
}
return r
}
// 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.
func structMappings(cfg *Config) []shared.EnvBinding {
return []shared.EnvBinding{
{
EnvVars: []string{"OCIS_LOG_FILE", "THUMBNAILS_LOG_FILE"},
Destination: &cfg.Log.File,
},
{
EnvVars: []string{"OCIS_LOG_LEVEL", "THUMBNAILS_LOG_LEVEL"},
Destination: &cfg.Log.Level,
},
{
EnvVars: []string{"OCIS_LOG_PRETTY", "THUMBNAILS_LOG_PRETTY"},
Destination: &cfg.Log.Pretty,
},
{
EnvVars: []string{"OCIS_LOG_COLOR", "THUMBNAILS_LOG_COLOR"},
Destination: &cfg.Log.Color,
},
{
EnvVars: []string{"THUMBNAILS_CONFIG_FILE"},
Destination: &cfg.File,
},
{
EnvVars: []string{"OCIS_TRACING_ENABLED", "THUMBNAILS_TRACING_ENABLED"},
Destination: &cfg.Tracing.Enabled,
},
{
EnvVars: []string{"OCIS_TRACING_TYPE", "THUMBNAILS_TRACING_TYPE"},
Destination: &cfg.Tracing.Type,
},
{
EnvVars: []string{"OCIS_TRACING_ENDPOINT", "THUMBNAILS_TRACING_ENDPOINT"},
Destination: &cfg.Tracing.Endpoint,
},
{
EnvVars: []string{"OCIS_TRACING_COLLECTOR", "THUMBNAILS_TRACING_COLLECTOR"},
Destination: &cfg.Tracing.Collector,
},
{
EnvVars: []string{"THUMBNAILS_TRACING_SERVICE"},
Destination: &cfg.Tracing.Service,
},
{
EnvVars: []string{"THUMBNAILS_DEBUG_ADDR"},
Destination: &cfg.Debug.Addr,
},
{
EnvVars: []string{"THUMBNAILS_DEBUG_TOKEN"},
Destination: &cfg.Debug.Token,
},
{
EnvVars: []string{"THUMBNAILS_DEBUG_PPROF"},
Destination: &cfg.Debug.Pprof,
},
{
EnvVars: []string{"THUMBNAILS_DEBUG_ZPAGES"},
Destination: &cfg.Debug.Zpages,
},
{
EnvVars: []string{"THUMBNAILS_GRPC_ADDR"},
Destination: &cfg.GRPC.Addr,
},
{
EnvVars: []string{"THUMBNAILS_GRPC_NAMESPACE"},
Destination: &cfg.GRPC.Namespace,
},
{
EnvVars: []string{"THUMBNAILS_TXT_FONTMAP_FILE"},
Destination: &cfg.Thumbnail.FontMapFile,
},
{
EnvVars: []string{"THUMBNAILS_FILESYSTEMSTORAGE_ROOT"},
Destination: &cfg.Thumbnail.FileSystemStorage.RootDirectory,
},
{
EnvVars: []string{"REVA_GATEWAY"},
Destination: &cfg.Thumbnail.RevaGateway,
},
{
EnvVars: []string{"OCIS_INSECURE", "THUMBNAILS_WEBDAVSOURCE_INSECURE"},
Destination: &cfg.Thumbnail.WebdavAllowInsecure,
},
{
EnvVars: []string{"OCIS_INSECURE", "THUMBNAILS_CS3SOURCE_INSECURE"},
Destination: &cfg.Thumbnail.CS3AllowInsecure,
},
}
}
+17
View File
@@ -0,0 +1,17 @@
package logging
import (
"github.com/owncloud/ocis/ocis-pkg/log"
"github.com/owncloud/ocis/thumbnails/pkg/config"
)
// LoggerFromConfig initializes a service-specific logger instance.
func Configure(name string, cfg config.Log) log.Logger {
return log.NewLogger(
log.Name(name),
log.Level(cfg.Level),
log.Pretty(cfg.Pretty),
log.Color(cfg.Color),
log.File(cfg.File),
)
}
@@ -26,7 +26,7 @@ func init() {
grpc.Address("localhost:9992"),
)
cfg := config.New()
cfg := config.DefaultConfig()
cfg.Thumbnail.Resolutions = []string{"16x16", "32x32", "64x64", "128x128"}
wd, _ := os.Getwd()
@@ -44,17 +44,17 @@ func init() {
if err != nil {
log.Fatalf("could not register ThumbnailHandler: %v", err)
}
if err := service.Server().Start(); err != nil {
log.Fatalf("could not start server: %v", err)
}
if err := service.Server().Start(); err != nil {
log.Fatalf("could not start server: %v", err)
}
}
func TestGetThumbnailInvalidImage(t *testing.T) {
req := proto.GetThumbnailRequest{
Filepath: "invalid.png",
Filepath: "invalid.png",
ThumbnailType: proto.GetThumbnailRequest_PNG,
Height: 32,
Width: 32,
Height: 32,
Width: 32,
}
client := service.Client()
cl := proto.NewThumbnailService("com.owncloud.api.thumbnails", client)
+1 -2
View File
@@ -31,8 +31,7 @@ func NewService(opts ...Option) v0proto.ThumbnailServiceHandler {
logger.Fatal().Err(err).Msg("resolutions not configured correctly")
}
svc := Thumbnail{
serviceID: options.Config.GRPC.Namespace + "." + options.Config.Service.Name,
webdavNamespace: options.Config.Thumbnail.WebdavNamespace,
serviceID: options.Config.GRPC.Namespace + "." + options.Config.Service.Name,
manager: thumbnail.NewSimpleManager(
resolutions,
options.ThumbnailStorage,