diff --git a/go.mod b/go.mod index 3bd83ec..73a7628 100644 --- a/go.mod +++ b/go.mod @@ -23,17 +23,15 @@ require ( github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.6.0 github.com/smartystreets/goconvey v1.6.4 // indirect - github.com/stretchr/testify v1.5.1 + github.com/spf13/viper v1.8.1 + github.com/stretchr/testify v1.7.0 github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14 github.com/swaggo/gin-swagger v1.2.0 github.com/swaggo/swag v1.6.7 github.com/tebeka/strftime v0.1.5 // indirect github.com/ugorji/go v1.1.8 // indirect - golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect - golang.org/x/sys v0.0.0-20200918174421-af09f7315aff // indirect - golang.org/x/tools v0.0.0-20200918232735-d647fc253266 // indirect - google.golang.org/protobuf v1.25.0 // indirect - gopkg.in/ini.v1 v1.61.0 + github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77 // indirect + gopkg.in/ini.v1 v1.62.0 gorm.io/driver/postgres v1.0.0 gorm.io/gorm v1.20.1 ) diff --git a/utils/setting.go b/utils/setting.go index 15c7bc8..c713262 100644 --- a/utils/setting.go +++ b/utils/setting.go @@ -6,9 +6,13 @@ SPDX-License-Identifier: Apache-2.0 package utils import ( - "gopkg.in/ini.v1" + "fmt" "os" "path/filepath" + "strconv" + "strings" + + "github.com/spf13/viper" ) // TODO use a struct ?? @@ -40,51 +44,80 @@ var ( ) func init() { - file, err := ini.Load("config/config.ini") - if err != nil { - file, err = ini.Load("../config/config.ini") - if err != nil { - file, err = ini.Load("../../config/config.ini") - if err != nil { - panic("failed to load ini config. err: " + err.Error()) - } - } + viper.SetConfigName("config") + viper.SetConfigType("ini") + viper.AddConfigPath("./config") + viper.AddConfigPath("../config") + viper.AddConfigPath("../../config") + viper.SetEnvPrefix("refiner") + viper.AutomaticEnv() + + replacer := strings.NewReplacer(".", "_") + viper.SetEnvKeyReplacer(replacer) + + if err := viper.ReadInConfig(); err != nil { + panic(fmt.Errorf("Fatal error config file: %w \n", err)) } - loadServer(file) - loadLog(file) - loadDatabase(file) - loadFabric(file) + loadDefault() + loadServer() + loadLog() + loadDatabase() + loadFabric() +} +func loadDefault() { + viper.SetDefault("server.app_mode", "debug") + viper.SetDefault("server.http_port", ":9999") + + viper.SetDefault("log.log_output", "stdout") + viper.SetDefault("log.log_level", "debug") + viper.SetDefault("log.log_format", "text") + viper.SetDefault("log.log_store", "") + viper.SetDefault("log.log_max_age", "432000") // 5 * 24 * 60 * 60 + viper.SetDefault("log.log_max_size", "1048576") // 1024 * 1024 + + viper.SetDefault("database.db_host", "localhost") + viper.SetDefault("database.db_port", "5432") + viper.SetDefault("database.db_user", "refiner") + viper.SetDefault("database.db_password", "123456") + viper.SetDefault("database.db_name", "ledgerdata_refiner") + + viper.SetDefault("fabric.org_name", "Org1") + viper.SetDefault("fabric.org_admin", "Admin") + viper.SetDefault("fabric.org_user", "User1") + viper.SetDefault("fabric.channel_id", "mychannel") + viper.SetDefault("fabric.network_name", "test-network") + viper.SetDefault("fabric.client_name", "test-client") } -func loadServer(file *ini.File) { - AppMode = file.Section("server").Key("app_mode").MustString("debug") - HttpPort = file.Section("server").Key("http_port").MustString(":9999") +func loadServer() { + AppMode = viper.GetString("server.app_mode") + HttpPort = viper.GetString("server.http_port") } -func loadLog(file *ini.File) { - LogOutput = file.Section("log").Key("log_output").MustString("stdout") - LogLevel = file.Section("log").Key("log_level").MustString("debug") - LogFormat = file.Section("log").Key("log_format").MustString("text") - LogStore = file.Section("log").Key("log_store").MustString("") +func loadLog() { + LogOutput = viper.GetString("log.log_output") + LogLevel = viper.GetString("log.log_level") + LogFormat = viper.GetString("log.log_format") + LogStore = viper.GetString("log.log_store") LogStore = filepath.Join(os.Getenv("HOME"), LogStore) - LogMaxAge = file.Section("log").Key("log_max_age").MustInt64(5 * 24 * 60 * 60) - LogMaxSize = file.Section("log").Key("log_max_size").MustInt64(1048576) + LogMaxAge, _ = strconv.ParseInt(viper.GetString("log.log_max_age"), 10, 64) + LogMaxSize, _ = strconv.ParseInt(viper.GetString("log.log_max_size"), 10, 64) } -func loadDatabase(file *ini.File) { - DBHost = file.Section("database").Key("db_host").MustString("localhost") - DBPort = file.Section("database").Key("db_port").MustString("5432") - DBUser = file.Section("database").Key("db_user").MustString("refiner") - DBPassword = file.Section("database").Key("db_password").MustString("123456") - DBName = file.Section("database").Key("db_name").MustString("ledgerdata_refiner") +func loadDatabase() { + DBHost = viper.GetString("database.db_host") + DBPort = viper.GetString("database.db_port") + DBUser = viper.GetString("database.db_user") + DBPassword = viper.GetString("database.db_password") + DBName = viper.GetString("database.db_name") } -func loadFabric(file *ini.File) { - OrgName = file.Section("fabric").Key("org_name").MustString("Org1") - OrgAdmin = file.Section("fabric").Key("org_admin").MustString("Admin") - OrgUser = file.Section("fabric").Key("org_user").MustString("User1") - ChannelId = file.Section("fabric").Key("channel_id").MustString("mychannel") - NetworkName = file.Section("fabric").Key("network_name").MustString("test-network") - ClientName = file.Section("fabric").Key("client_name").MustString("test-client") +func loadFabric() { + OrgName = viper.GetString("fabric.org_name") + OrgAdmin = viper.GetString("fabric.org_admin") + OrgUser = viper.GetString("fabric.org_user") + ChannelId = viper.GetString("fabric.channel_id") + NetworkName = viper.GetString("fabric.network_name") + ClientName = viper.GetString("fabric.client_name") } diff --git a/utils/setting_test.go b/utils/setting_test.go new file mode 100644 index 0000000..18d9a4b --- /dev/null +++ b/utils/setting_test.go @@ -0,0 +1,20 @@ +package utils + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +//go:generate sed -i -e /http_port/d ../config/config.ini +func TestSettings(t *testing.T) { + require.Equal(t, AppMode, "release") + require.Equal(t, LogOutput, "stdout") + require.Equal(t, DBName, "ledgerdata_refiner") + require.Equal(t, NetworkName, "test-fabric") + + // Default value + require.Equal(t, HttpPort, ":9999") + // Override with environment variable + // require.Equal(t, DBPort, "15432") +}