From 7f42a90be68100d4c8bd8769665a7ebc339c4479 Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Sat, 22 Jul 2023 11:33:14 +0500 Subject: [PATCH] Add simple config module --- .env | 3 +++ backend/db.go | 11 +++-------- config/main.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 +++ go.sum | 7 +++++++ main.go | 24 +++++++++++++++++++++--- 6 files changed, 82 insertions(+), 11 deletions(-) create mode 100644 .env create mode 100644 config/main.go diff --git a/.env b/.env new file mode 100644 index 0000000..7546082 --- /dev/null +++ b/.env @@ -0,0 +1,3 @@ +P_SECRETKEY="super-secret-key" +P_DBPATH="db.sqlite3" +P_LOGLEVEL="debug" diff --git a/backend/db.go b/backend/db.go index 5b7b802..6e11925 100644 --- a/backend/db.go +++ b/backend/db.go @@ -1,18 +1,13 @@ package backend import ( + "github.com/ordinary-dev/phoenix/config" "gorm.io/driver/sqlite" "gorm.io/gorm" - "os" ) -func GetDatabaseConnection() (*gorm.DB, error) { - dbPath := os.Getenv("PHOENIX_DB_PATH") - if dbPath == "" { - dbPath = "db.sqlite3" - } - - db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{}) +func GetDatabaseConnection(cfg *config.Config) (*gorm.DB, error) { + db, err := gorm.Open(sqlite.Open(cfg.DBPath), &gorm.Config{}) if err != nil { return nil, err } diff --git a/config/main.go b/config/main.go new file mode 100644 index 0000000..163510e --- /dev/null +++ b/config/main.go @@ -0,0 +1,45 @@ +package config + +import ( + "github.com/joho/godotenv" + "github.com/kelseyhightower/envconfig" + "github.com/sirupsen/logrus" +) + +type Config struct { + SecretKey string `required:"true"` + DBPath string `required:"true"` + LogLevel string `default:"warning"` +} + +func GetConfig() (*Config, error) { + err := godotenv.Load() + if err != nil { + logrus.Infof("Config: %v", err) + } + + var cfg Config + err = envconfig.Process("p", &cfg) + if err != nil { + return nil, err + } + + return &cfg, nil +} + +func (cfg *Config) GetLogLevel() logrus.Level { + switch cfg.LogLevel { + case "debug": + return logrus.DebugLevel + case "info": + return logrus.InfoLevel + case "warning": + return logrus.WarnLevel + case "error": + return logrus.ErrorLevel + case "fatal": + return logrus.FatalLevel + default: + return logrus.WarnLevel + } +} diff --git a/go.mod b/go.mod index 0291847..ae470ee 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,9 @@ require ( github.com/goccy/go-json v0.10.2 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect + github.com/joho/godotenv v1.5.1 // indirect github.com/json-iterator/go v1.1.12 // indirect + github.com/kelseyhightower/envconfig v1.4.0 // indirect github.com/klauspost/cpuid/v2 v2.2.4 // indirect github.com/leodido/go-urn v1.2.4 // indirect github.com/mattn/go-isatty v0.0.19 // indirect @@ -28,6 +30,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.0.8 // indirect + github.com/sirupsen/logrus v1.9.3 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.11 // indirect golang.org/x/arch v0.3.0 // indirect diff --git a/go.sum b/go.sum index be01f4c..3917e5f 100644 --- a/go.sum +++ b/go.sum @@ -30,8 +30,12 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= +github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk= github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= @@ -50,6 +54,8 @@ github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZ github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -73,6 +79,7 @@ golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIi golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/main.go b/main.go index ab5c6f2..0be5356 100644 --- a/main.go +++ b/main.go @@ -3,14 +3,32 @@ package main import ( "github.com/gin-gonic/gin" "github.com/ordinary-dev/phoenix/backend" + "github.com/ordinary-dev/phoenix/config" "github.com/ordinary-dev/phoenix/views" - "log" + "github.com/sirupsen/logrus" ) func main() { - db, err := backend.GetDatabaseConnection() + // Configure logger + logrus.SetFormatter(&logrus.TextFormatter{ + FullTimestamp: true, + }) + + // Read config + cfg, err := config.GetConfig() if err != nil { - log.Fatal(err) + logrus.Fatalf("%v", err) + } + + // Set log level + logLevel := cfg.GetLogLevel() + logrus.SetLevel(logLevel) + logrus.Infof("Setting log level to %v", logLevel) + + // Connect to the database + db, err := backend.GetDatabaseConnection(cfg) + if err != nil { + logrus.Fatalf("%v", err) } r := gin.Default()