From 34b2f55cfc6f40c7af5f6748b3fca1be8b0b6ca8 Mon Sep 17 00:00:00 2001 From: Eden Kirin Date: Wed, 25 Oct 2023 17:55:36 +0200 Subject: [PATCH] Restructure app --- .air.toml | 2 +- app/{cfg/cfg.go => config.go} | 4 ++-- app/{db => }/db.go | 18 ++++++++---------- app/{logging => }/logging.go | 15 +++++++-------- app/main.go | 19 ++++++++----------- 5 files changed, 26 insertions(+), 32 deletions(-) rename app/{cfg/cfg.go => config.go} (98%) rename app/{db => }/db.go (75%) rename app/{logging => }/logging.go (67%) diff --git a/.air.toml b/.air.toml index c2289e9..12c77a1 100644 --- a/.air.toml +++ b/.air.toml @@ -5,7 +5,7 @@ tmp_dir = "tmp" [build] args_bin = [] bin = "./tmp/main" -cmd = "go build -o ./tmp/main ./app/main.go" +cmd = "go build -o ./tmp/main ./app/." delay = 1000 exclude_dir = ["assets", "tmp", "vendor", "testdata", "build"] exclude_file = [] diff --git a/app/cfg/cfg.go b/app/config.go similarity index 98% rename from app/cfg/cfg.go rename to app/config.go index 776d891..d62ac2b 100644 --- a/app/cfg/cfg.go +++ b/app/config.go @@ -1,4 +1,4 @@ -package cfg +package main import ( "fmt" @@ -78,7 +78,7 @@ func readEnv(cfg *configStruct) { } } -func Init() { +func InitCfg() { cfgFile := os.Getenv("MAILSENDER_CONFIG") if cfgFile == "" { cfgFile = DEFAULT_CONFIG_FILE diff --git a/app/db/db.go b/app/db.go similarity index 75% rename from app/db/db.go rename to app/db.go index 742030a..4beb7a7 100644 --- a/app/db/db.go +++ b/app/db.go @@ -1,9 +1,7 @@ -package db +package main import ( "fmt" - "iris-test/app/cfg" - "iris-test/app/logging" "strconv" "strings" "time" @@ -21,18 +19,18 @@ var DBConn *gorm.DB func InitDB() *gorm.DB { var connectionString = strings.Join([]string{ "postgres://", - cfg.Config.Database.Username, ":", - cfg.Config.Database.Password, "@", - cfg.Config.Database.Host, ":", - cfg.Config.Database.Port, "/", - cfg.Config.Database.Name, + Config.Database.Username, ":", + Config.Database.Password, "@", + Config.Database.Host, ":", + Config.Database.Port, "/", + Config.Database.Name, "?sslmode=disable", "&TimeZone=UTC", "&connect_timeout=", strconv.Itoa(DB_CONNECTION_TIMEOUT), }, "") var logLevel = gormLogger.Silent - if cfg.Config.Application.DebugSQL { + if Config.Application.DebugSQL { logLevel = gormLogger.Info } @@ -42,7 +40,7 @@ func InitDB() *gorm.DB { }) if err != nil { msg := fmt.Sprintf("Error connecting to database: %s. Terminating!", err) - logging.Error(msg) + Log.Error(msg) panic(msg) } diff --git a/app/logging/logging.go b/app/logging.go similarity index 67% rename from app/logging/logging.go rename to app/logging.go index b09cb43..a7eb76f 100644 --- a/app/logging/logging.go +++ b/app/logging.go @@ -1,10 +1,9 @@ -package logging +package main import ( "encoding/json" "fmt" "io" - "iris-test/app/cfg" "os" "github.com/sirupsen/logrus" @@ -28,10 +27,10 @@ func Warn(message string) { Log.Warn(message) } -func Init() { - logLevel, err := logrus.ParseLevel(cfg.Config.Application.LogLevel) +func InitLogging() { + logLevel, err := logrus.ParseLevel(Config.Application.LogLevel) if err != nil { - panic(fmt.Sprintf("Invalid configured logLevel: %s\n", cfg.Config.Application.LogLevel)) + panic(fmt.Sprintf("Invalid configured logLevel: %s\n", Config.Application.LogLevel)) } Log.SetLevel(logLevel) @@ -43,14 +42,14 @@ func Init() { DisableQuote: true, }) - LogFile := cfg.Config.Application.LogFile + LogFile := 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", cfg.Config.Application.LogFile, err) + msg := fmt.Sprintf("Failed to log to file %s: %s", Config.Application.LogFile, err) Log.Warning(msg) panic(msg) } @@ -58,7 +57,7 @@ func Init() { mw := io.MultiWriter(os.Stdout, file) Log.SetOutput(mw) - configJson, err := json.Marshal(cfg.Config) + configJson, err := json.Marshal(Config) if err == nil { Info(fmt.Sprintf("Using config: %s", configJson)) } diff --git a/app/main.go b/app/main.go index 70d29c8..d7313bf 100644 --- a/app/main.go +++ b/app/main.go @@ -2,9 +2,6 @@ package main import ( "fmt" - "iris-test/app/cfg" - "iris-test/app/db" - "iris-test/app/logging" "iris-test/app/views" "time" @@ -16,17 +13,17 @@ import ( var redisDB *redis.Database func createSessionEngine() *sessions.Sessions { - redisAddr := fmt.Sprintf("%s:%d", cfg.Config.Redis.Host, cfg.Config.Redis.Port) + redisAddr := fmt.Sprintf("%s:%d", Config.Redis.Host, Config.Redis.Port) redisDB = redis.New(redis.Config{ Network: "tcp", Addr: redisAddr, Timeout: time.Duration(30) * time.Second, MaxActive: 10, - Username: cfg.Config.Redis.Username, - Password: cfg.Config.Redis.Password, - Database: cfg.Config.Redis.Database, - Prefix: cfg.Config.Redis.Prefix, + Username: Config.Redis.Username, + Password: Config.Redis.Password, + Database: Config.Redis.Database, + Prefix: 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) @@ -54,9 +51,9 @@ func createApp() *iris.Application { } func main() { - cfg.Init() - logging.Init() - db.InitDB() + InitCfg() + InitLogging() + InitDB() app := createApp() defer redisDB.Close()