From 307f587d3d1988b449ee11d873bd23796566a4cc Mon Sep 17 00:00:00 2001 From: Tim Smyth Date: Wed, 19 Apr 2023 13:14:10 +0200 Subject: [PATCH] refactor config loading --- config/config.go | 9 ++-- config/data/config_test.yaml | 18 ++++++++ config/data/empty_test.yaml | 0 config/provider.go | 19 +++++++++ config/provider_test.go | 83 ++++++++++++++++++++++++++++++++++++ main.go | 59 ++++++------------------- server.yaml | 6 +-- 7 files changed, 141 insertions(+), 53 deletions(-) create mode 100644 config/data/config_test.yaml create mode 100644 config/data/empty_test.yaml create mode 100644 config/provider.go create mode 100644 config/provider_test.go diff --git a/config/config.go b/config/config.go index 2209d565..8bc59ee9 100644 --- a/config/config.go +++ b/config/config.go @@ -4,9 +4,10 @@ package config // general structure of the configuration file type Configuration struct { - Server *Server `mapstructure:"server"` - Verifier *Verifier `mapstructure:"verifier"` - SSIKit *SSIKit `mapstructure:"ssiKit"` + Server Server `mapstructure:"server"` + Verifier Verifier `mapstructure:"verifier"` + SSIKit SSIKit `mapstructure:"ssiKit"` + Logging Logging `mapstructure:"logging"` } // configuration to be used by the ssiKit configuration @@ -23,8 +24,6 @@ type Server struct { TemplateDir string `mapstructure:"templateDir" default:"views/"` // directory of static files to be provided, f.e. to be used inside the templates StaticDir string `mapstructure:"staticDir" default:"views/static/"` - // logging configuration - Logging *Logging `mapstructure:"logging"` } // logging config diff --git a/config/data/config_test.yaml b/config/data/config_test.yaml new file mode 100644 index 00000000..ef279914 --- /dev/null +++ b/config/data/config_test.yaml @@ -0,0 +1,18 @@ +server: + port: 3000 + staticDir: "views/static" + templateDir: "views/" + +logging: + level: "DEBUG" + jsonLogging: "true" + logRequests: "true" + pathsToSkip: [/health] + +verifier: + did: "did:key:somekey" + tirAddress: "https://test.dev/trusted_issuer/v3/issuers/" + sessionExpiry: 30 + +ssiKit: + auditorURL: http://waltid:7003 \ No newline at end of file diff --git a/config/data/empty_test.yaml b/config/data/empty_test.yaml new file mode 100644 index 00000000..e69de29b diff --git a/config/provider.go b/config/provider.go new file mode 100644 index 00000000..dad0afe1 --- /dev/null +++ b/config/provider.go @@ -0,0 +1,19 @@ +package config + +import ( + "github.com/gookit/config/v2" + "github.com/gookit/config/v2/yaml" +) + +// read the config from the config file +func ReadConfig(configFile string) (configuration Configuration, err error) { + config.WithOptions(config.ParseDefault) + config.AddDriver(yaml.Driver) + err = config.LoadFiles(configFile) + + if err != nil { + return + } + config.BindStruct("", &configuration) + return +} diff --git a/config/provider_test.go b/config/provider_test.go new file mode 100644 index 00000000..6690dc89 --- /dev/null +++ b/config/provider_test.go @@ -0,0 +1,83 @@ +package config + +import ( + "reflect" + "testing" + + "github.com/gookit/config/v2" +) + +func Test_ReadConfig(t *testing.T) { + type args struct { + configFile string + } + tests := []struct { + name string + args args + wantConfiguration Configuration + wantErr bool + }{ + { + "Read config", + args{"data/config_test.yaml"}, + Configuration{ + Server: Server{ + Port: 3000, + TemplateDir: "views/", + StaticDir: "views/static", + }, + Verifier: Verifier{ + Did: "did:key:somekey", + TirAddress: "https://test.dev/trusted_issuer/v3/issuers/", + SessionExpiry: 30, + RequestScope: "", + }, SSIKit: SSIKit{ + AuditorURL: "http://waltid:7003", + }, + Logging: Logging{ + Level: "DEBUG", + JsonLogging: true, + LogRequests: true, + PathsToSkip: []string{"/health"}, + }, + }, + false, + }, { + "Defaults only", + args{"data/empty_test.yaml"}, + Configuration{ + Server: Server{Port: 8080, + TemplateDir: "views/", + StaticDir: "views/static/", + }, + Verifier: Verifier{Did: "", + TirAddress: "", + SessionExpiry: 30, + RequestScope: "", + }, SSIKit: SSIKit{ + AuditorURL: "", + }, + Logging: Logging{ + Level: "INFO", + JsonLogging: true, + LogRequests: true, + PathsToSkip: nil, + }, + }, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + config.Reset() + gotConfiguration, err := ReadConfig(tt.args.configFile) + if (err != nil) != tt.wantErr { + t.Errorf("readConfig() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotConfiguration, tt.wantConfiguration) { + t.Errorf("readConfig() = %v, want %v", gotConfiguration, tt.wantConfiguration) + } + }) + } +} diff --git a/main.go b/main.go index 050be19d..bdc46b56 100644 --- a/main.go +++ b/main.go @@ -14,46 +14,38 @@ import ( "github.com/foolin/goview/supports/ginview" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" - "github.com/gookit/config/v2" - "github.com/gookit/config/v2/yaml" "github.com/penglongli/gin-metrics/ginmetrics" ) // default config file location - can be overwritten by envvar var configFile string = "server.yaml" -// config objects -var serverConfig configModel.Server -var loggingConfig configModel.Logging -var ssiKitConfig configModel.SSIKit -var verifierConfig configModel.Verifier - /** * Startup method to run the gin-server. */ func main() { - readConfig() + configuration, err := configModel.ReadConfig(configFile) + if err != nil { + panic(err) + } logging.Configure( - loggingConfig.JsonLogging, - loggingConfig.Level, - loggingConfig.LogRequests, - loggingConfig.PathsToSkip) + configuration.Logging.JsonLogging, + configuration.Logging.Level, + configuration.Logging.LogRequests, + configuration.Logging.PathsToSkip) logger := logging.Log() - logger.Infof("Logging config is: %s", logging.PrettyPrintObject(loggingConfig)) - logger.Infof("Server config is: %s", logging.PrettyPrintObject(serverConfig)) - logger.Infof("SSIKit config is: %s", logging.PrettyPrintObject(ssiKitConfig)) - logger.Infof("Verifier config is: %s", logging.PrettyPrintObject(verifierConfig)) + logger.Infof("Configuration is: %s", logging.PrettyPrintObject(configuration)) - ssiKitClient, err := ssi.NewSSIKitClient(&ssiKitConfig) + ssiKitClient, err := ssi.NewSSIKitClient(&configuration.SSIKit) if err != nil { logger.Errorf("Was not able to get an ssiKit client. Err: %v", err) return } - verifier.InitVerifier(&verifierConfig, ssiKitClient) + verifier.InitVerifier(&configuration.Verifier, ssiKitClient) router := getRouter() @@ -71,16 +63,16 @@ func main() { //new template engine router.HTMLRender = ginview.Default() // static files for the frontend - router.Static("/static", serverConfig.StaticDir) + router.Static("/static", configuration.Server.StaticDir) // initiate metrics metrics := ginmetrics.GetMonitor() metrics.SetMetricPath("/metrics") metrics.Use(router) - router.Run(fmt.Sprintf("0.0.0.0:%v", serverConfig.Port)) + router.Run(fmt.Sprintf("0.0.0.0:%v", configuration.Server.Port)) - logger.Infof("Started router at %v", serverConfig.Port) + logger.Infof("Started router at %v", configuration.Server.Port) } // initiate the router @@ -108,29 +100,6 @@ func getRouter() *gin.Engine { return router } -// read the config from the config file -func readConfig() { - config.WithOptions(config.ParseDefault) - config.AddDriver(yaml.Driver) - err := config.LoadFiles(configFile) - - if err != nil { - panic(err) - } - - serverConfig = configModel.Server{} - config.BindStruct("server", &serverConfig) - - loggingConfig = configModel.Logging{} - config.BindStruct("logging", &loggingConfig) - - ssiKitConfig = configModel.SSIKit{} - config.BindStruct("ssiKit", &ssiKitConfig) - - verifierConfig = configModel.Verifier{} - config.BindStruct("verifier", &verifierConfig) -} - // allow override of the config-file on init. Everything else happens on main to improve testability func init() { diff --git a/server.yaml b/server.yaml index f9282d9c..a78dc2f4 100644 --- a/server.yaml +++ b/server.yaml @@ -1,9 +1,9 @@ server: port: 8080 - logging: - jsonLogging: true - logRequests: true +logging: + jsonLogging: true + logRequests: true verifier: tirAddress: https://tir.de did: did:key:myverifier