From dd671d561c9a1e7fa570c26ccb20f9ebbd78e099 Mon Sep 17 00:00:00 2001 From: Eden Kirin Date: Thu, 26 Oct 2023 16:49:24 +0200 Subject: [PATCH] Restructure again --- app/{common => lib/cfg}/config.go | 2 +- app/{common => lib/db}/db.go | 18 ++++++++++-------- app/{common => lib/logging}/logging.go | 13 +++++++------ app/main.go | 22 ++++++++++++---------- app/models/user.go | 4 ++-- 5 files changed, 32 insertions(+), 27 deletions(-) rename app/{common => lib/cfg}/config.go (99%) rename app/{common => lib/db}/db.go (76%) rename app/{common => lib/logging}/logging.go (68%) diff --git a/app/common/config.go b/app/lib/cfg/config.go similarity index 99% rename from app/common/config.go rename to app/lib/cfg/config.go index 1abea8e..1625d76 100644 --- a/app/common/config.go +++ b/app/lib/cfg/config.go @@ -1,4 +1,4 @@ -package common +package cfg import ( "fmt" diff --git a/app/common/db.go b/app/lib/db/db.go similarity index 76% rename from app/common/db.go rename to app/lib/db/db.go index 6a4c477..9ebead5 100644 --- a/app/common/db.go +++ b/app/lib/db/db.go @@ -1,7 +1,9 @@ -package common +package db import ( "fmt" + "iris-test/app/lib/cfg" + "iris-test/app/lib/logging" "iris-test/app/repository" "strconv" "strings" @@ -20,18 +22,18 @@ var DBConn *gorm.DB func InitDB() *gorm.DB { var connectionString = strings.Join([]string{ "postgres://", - Config.Database.Username, ":", - Config.Database.Password, "@", - Config.Database.Host, ":", - Config.Database.Port, "/", - Config.Database.Name, + cfg.Config.Database.Username, ":", + cfg.Config.Database.Password, "@", + cfg.Config.Database.Host, ":", + cfg.Config.Database.Port, "/", + cfg.Config.Database.Name, "?sslmode=disable", "&TimeZone=UTC", "&connect_timeout=", strconv.Itoa(DB_CONNECTION_TIMEOUT), }, "") var logLevel = gormLogger.Silent - if Config.Application.DebugSQL { + if cfg.Config.Application.DebugSQL { logLevel = gormLogger.Info } @@ -41,7 +43,7 @@ func InitDB() *gorm.DB { }) if err != nil { msg := fmt.Sprintf("Error connecting to database: %s. Terminating!", err) - Log.Error(msg) + logging.Error(msg) panic(msg) } diff --git a/app/common/logging.go b/app/lib/logging/logging.go similarity index 68% rename from app/common/logging.go rename to app/lib/logging/logging.go index f381ae3..f7f8526 100644 --- a/app/common/logging.go +++ b/app/lib/logging/logging.go @@ -1,9 +1,10 @@ -package common +package logging import ( "encoding/json" "fmt" "io" + "iris-test/app/lib/cfg" "os" "github.com/sirupsen/logrus" @@ -28,9 +29,9 @@ func Warn(message string) { } func InitLogging() { - logLevel, err := logrus.ParseLevel(Config.Application.LogLevel) + logLevel, err := logrus.ParseLevel(cfg.Config.Application.LogLevel) if err != nil { - panic(fmt.Sprintf("Invalid configured logLevel: %s\n", Config.Application.LogLevel)) + panic(fmt.Sprintf("Invalid configured logLevel: %s\n", cfg.Config.Application.LogLevel)) } Log.SetLevel(logLevel) @@ -42,14 +43,14 @@ func InitLogging() { DisableQuote: true, }) - LogFile := Config.Application.LogFile + LogFile := cfg.Config.Application.LogFile file, err := os.OpenFile( LogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0655, ) if err != nil { - msg := fmt.Sprintf("Failed to log to file %s: %s", Config.Application.LogFile, err) + msg := fmt.Sprintf("Failed to log to file %s: %s", cfg.Config.Application.LogFile, err) Log.Warning(msg) panic(msg) } @@ -57,7 +58,7 @@ func InitLogging() { mw := io.MultiWriter(os.Stdout, file) Log.SetOutput(mw) - configJson, err := json.Marshal(Config) + configJson, err := json.Marshal(cfg.Config) if err == nil { Info(fmt.Sprintf("Using config: %s", configJson)) } diff --git a/app/main.go b/app/main.go index 0a695da..8b318cd 100644 --- a/app/main.go +++ b/app/main.go @@ -2,7 +2,9 @@ package main import ( "fmt" - "iris-test/app/common" + "iris-test/app/lib/cfg" + "iris-test/app/lib/db" + "iris-test/app/lib/logging" "iris-test/app/views" "os" "time" @@ -16,17 +18,17 @@ import ( var redisDB *redis.Database func createSessionEngine() *sessions.Sessions { - redisAddr := fmt.Sprintf("%s:%d", common.Config.Redis.Host, common.Config.Redis.Port) + redisAddr := fmt.Sprintf("%s:%d", cfg.Config.Redis.Host, cfg.Config.Redis.Port) redisDB = redis.New(redis.Config{ Network: "tcp", Addr: redisAddr, Timeout: time.Duration(30) * time.Second, MaxActive: 10, - Username: common.Config.Redis.Username, - Password: common.Config.Redis.Password, - Database: common.Config.Redis.Database, - Prefix: common.Config.Redis.Prefix, + Username: cfg.Config.Redis.Username, + Password: cfg.Config.Redis.Password, + Database: cfg.Config.Redis.Database, + Prefix: cfg.Config.Redis.Prefix, Driver: redis.GoRedis(), // defaults to this driver. // To set a custom, existing go-redis client, use the "SetClient" method: // Driver: redis.GoRedis().SetClient(customGoRedisClient) @@ -84,7 +86,7 @@ func createApp() *iris.Application { accessLog := createAccessLog() app := iris.New() - app.Logger().SetLevel(common.Config.Application.LogLevel) + app.Logger().SetLevel(cfg.Config.Application.LogLevel) app.Use(sessionsEngine.Handler()) app.UseRouter(accessLog.Handler) app.RegisterView(iris.Jet("./app/templates", ".jet").Reload(true)) @@ -93,9 +95,9 @@ func createApp() *iris.Application { } func main() { - common.InitCfg() - common.InitLogging() - common.InitDB() + cfg.InitCfg() + logging.InitLogging() + db.InitDB() app := createApp() defer redisDB.Close() diff --git a/app/models/user.go b/app/models/user.go index 14ad147..37d8c11 100644 --- a/app/models/user.go +++ b/app/models/user.go @@ -20,7 +20,7 @@ func (u *User) TableName() string { } // func (u *User) SetPassword(password string) (string, error) { -// secretKey := Config.Application.SecretKey -// bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14) +// secretKey := common.Config.Application.SecretKey +// bytes, err := bcrypt.GenerateFromPassword([]byte(password+secretKey), 14) // return string(bytes), err // }