From 4607e42476d12280aebf8dbdff739b1fc74b2037 Mon Sep 17 00:00:00 2001 From: eccentric Date: Wed, 1 Nov 2023 21:51:14 +0000 Subject: [PATCH] Refactor how config is used. --- .air.toml | 2 +- aid/config.go | 53 +++++++++++++++++++++++++++++++++++++++++++++ aid/print.go | 17 +++++++++++++++ config/config.go | 52 -------------------------------------------- default.config.ini | 6 ++++- main.go | 11 ++++++---- storage/postgres.go | 4 ++-- 7 files changed, 85 insertions(+), 60 deletions(-) create mode 100644 aid/config.go delete mode 100644 config/config.go diff --git a/.air.toml b/.air.toml index 5907ee9..f5f8f10 100644 --- a/.air.toml +++ b/.air.toml @@ -14,7 +14,7 @@ tmp_dir = "tmp" follow_symlink = false full_bin = "" include_dir = [] - include_ext = ["go", "tpl", "tmpl", "html"] + include_ext = ["go", "tpl", "tmpl", "html", "ini"] include_file = [] kill_delay = "0s" log = "build-errors.log" diff --git a/aid/config.go b/aid/config.go new file mode 100644 index 0000000..c909b4a --- /dev/null +++ b/aid/config.go @@ -0,0 +1,53 @@ +package aid + +import ( + "os" + + "gopkg.in/ini.v1" +) + +type CS struct { + Database struct { + URI string + Type string + } + Output struct { + Level string + } +} + +var ( + Config *CS +) + +func LoadConfig() { + Config = &CS{} + + configPath := "config.ini" + if _, err := os.Stat(configPath); err != nil { + panic("config.ini not found! please rename default.config.ini to config.ini and complete") + } + + cfg, err := ini.Load("config.ini") + if err != nil { + panic(err) + } + + Config.Database.URI = cfg.Section("database").Key("uri").String() + if Config.Database.URI == "" { + panic("Database URI is empty") + } + Config.Database.Type = cfg.Section("database").Key("type").String() + if Config.Database.Type == "" { + panic("Database Type is empty") + } + + Config.Output.Level = cfg.Section("output").Key("level").String() + if Config.Output.Level == "" { + panic("Output Level is empty") + } + + if Config.Output.Level != "dev" && Config.Output.Level != "prod" && Config.Output.Level != "time" && Config.Output.Level != "info" { + panic("Output Level must be either dev or prod") + } +} \ No newline at end of file diff --git a/aid/print.go b/aid/print.go index c1d2a16..a32ab25 100644 --- a/aid/print.go +++ b/aid/print.go @@ -3,12 +3,29 @@ package aid import ( "encoding/json" "fmt" + "time" ) func PrintJSON(v interface{}) { + if Config.Output.Level == "prod" || Config.Output.Level == "time" { + return + } + json1, err := json.MarshalIndent(v, "", " ") if err != nil { panic(err) } fmt.Println(string(json1)) +} + +func PrintTime(label string, functions ...func()) { + if Config.Output.Level == "prod" { + return + } + + current := time.Now() + for _, f := range functions { + f() + } + fmt.Println(label + ":", time.Since(current)) } \ No newline at end of file diff --git a/config/config.go b/config/config.go deleted file mode 100644 index ca9c0c0..0000000 --- a/config/config.go +++ /dev/null @@ -1,52 +0,0 @@ -package config - -import ( - "os" - - "gopkg.in/ini.v1" -) - -var ( - local *Config -) - -type Config struct { - Database struct { - URI string - Type string - } -} - -func NewConfig() *Config { - return &Config{} -} - -func (c *Config) Load() { - configPath := "config.ini" - if _, err := os.Stat(configPath); err != nil { - panic("config.ini not found! please rename default.config.ini to config.ini and complete") - } - - cfg, err := ini.Load("config.ini") - if err != nil { - panic(err) - } - - c.Database.URI = cfg.Section("database").Key("uri").String() - if c.Database.URI == "" { - panic("Database URI is empty") - } - c.Database.Type = cfg.Section("database").Key("type").String() - if c.Database.Type == "" { - panic("Database Type is empty") - } -} - -func Get() *Config { - if local == nil { - local = NewConfig() - local.Load() - } - - return local -} \ No newline at end of file diff --git a/default.config.ini b/default.config.ini index e98ca21..1e0f7d8 100644 --- a/default.config.ini +++ b/default.config.ini @@ -2,4 +2,8 @@ ; connect string uri="host=localhost user=postgres password=pass dbname=snow port=5432 sslmode=disable" ; postgres -type="postgres" \ No newline at end of file +type="postgres" + +[output] +; level of logging +level="dev" # dev, prod \ No newline at end of file diff --git a/main.go b/main.go index 5a9d5ec..d3fb1b1 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "github.com/ectrc/snow/aid" - "github.com/ectrc/snow/config" "github.com/ectrc/snow/person" "github.com/ectrc/snow/storage" ) @@ -12,11 +11,11 @@ const ( ) func init() { - config := config.Get() + aid.LoadConfig() var device storage.Storage - switch config.Database.Type { + switch aid.Config.Database.Type { case "postgres": postgresStorage := storage.NewPostgresStorage() @@ -90,7 +89,11 @@ func init() { } func main() { - users := person.AllFromDatabase() + var users []*person.Person + + aid.PrintTime("Fetching Persons", func() { + users = person.AllFromDatabase() + }) for _, user := range users { aid.PrintJSON(user.Snapshot()) diff --git a/storage/postgres.go b/storage/postgres.go index 76c4d3b..e332c20 100644 --- a/storage/postgres.go +++ b/storage/postgres.go @@ -1,7 +1,7 @@ package storage import ( - "github.com/ectrc/snow/config" + "github.com/ectrc/snow/aid" "gorm.io/driver/postgres" "gorm.io/gorm" ) @@ -11,7 +11,7 @@ type PostgresStorage struct { } func NewPostgresStorage() *PostgresStorage { - db, err := gorm.Open(postgres.Open(config.Get().Database.URI), &gorm.Config{}) + db, err := gorm.Open(postgres.Open(aid.Config.Database.URI), &gorm.Config{}) if err != nil { panic(err) }