Restructure app

This commit is contained in:
Eden Kirin
2023-10-25 17:55:36 +02:00
parent 7fdf131d53
commit 34b2f55cfc
5 changed files with 26 additions and 32 deletions

View File

@ -5,7 +5,7 @@ tmp_dir = "tmp"
[build] [build]
args_bin = [] args_bin = []
bin = "./tmp/main" bin = "./tmp/main"
cmd = "go build -o ./tmp/main ./app/main.go" cmd = "go build -o ./tmp/main ./app/."
delay = 1000 delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata", "build"] exclude_dir = ["assets", "tmp", "vendor", "testdata", "build"]
exclude_file = [] exclude_file = []

View File

@ -1,4 +1,4 @@
package cfg package main
import ( import (
"fmt" "fmt"
@ -78,7 +78,7 @@ func readEnv(cfg *configStruct) {
} }
} }
func Init() { func InitCfg() {
cfgFile := os.Getenv("MAILSENDER_CONFIG") cfgFile := os.Getenv("MAILSENDER_CONFIG")
if cfgFile == "" { if cfgFile == "" {
cfgFile = DEFAULT_CONFIG_FILE cfgFile = DEFAULT_CONFIG_FILE

View File

@ -1,9 +1,7 @@
package db package main
import ( import (
"fmt" "fmt"
"iris-test/app/cfg"
"iris-test/app/logging"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -21,18 +19,18 @@ var DBConn *gorm.DB
func InitDB() *gorm.DB { func InitDB() *gorm.DB {
var connectionString = strings.Join([]string{ var connectionString = strings.Join([]string{
"postgres://", "postgres://",
cfg.Config.Database.Username, ":", Config.Database.Username, ":",
cfg.Config.Database.Password, "@", Config.Database.Password, "@",
cfg.Config.Database.Host, ":", Config.Database.Host, ":",
cfg.Config.Database.Port, "/", Config.Database.Port, "/",
cfg.Config.Database.Name, Config.Database.Name,
"?sslmode=disable", "?sslmode=disable",
"&TimeZone=UTC", "&TimeZone=UTC",
"&connect_timeout=", strconv.Itoa(DB_CONNECTION_TIMEOUT), "&connect_timeout=", strconv.Itoa(DB_CONNECTION_TIMEOUT),
}, "") }, "")
var logLevel = gormLogger.Silent var logLevel = gormLogger.Silent
if cfg.Config.Application.DebugSQL { if Config.Application.DebugSQL {
logLevel = gormLogger.Info logLevel = gormLogger.Info
} }
@ -42,7 +40,7 @@ func InitDB() *gorm.DB {
}) })
if err != nil { if err != nil {
msg := fmt.Sprintf("Error connecting to database: %s. Terminating!", err) msg := fmt.Sprintf("Error connecting to database: %s. Terminating!", err)
logging.Error(msg) Log.Error(msg)
panic(msg) panic(msg)
} }

View File

@ -1,10 +1,9 @@
package logging package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"iris-test/app/cfg"
"os" "os"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -28,10 +27,10 @@ func Warn(message string) {
Log.Warn(message) Log.Warn(message)
} }
func Init() { func InitLogging() {
logLevel, err := logrus.ParseLevel(cfg.Config.Application.LogLevel) logLevel, err := logrus.ParseLevel(Config.Application.LogLevel)
if err != nil { 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) Log.SetLevel(logLevel)
@ -43,14 +42,14 @@ func Init() {
DisableQuote: true, DisableQuote: true,
}) })
LogFile := cfg.Config.Application.LogFile LogFile := Config.Application.LogFile
file, err := os.OpenFile( file, err := os.OpenFile(
LogFile, LogFile,
os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.O_CREATE|os.O_WRONLY|os.O_APPEND,
0655, 0655,
) )
if err != nil { 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) Log.Warning(msg)
panic(msg) panic(msg)
} }
@ -58,7 +57,7 @@ func Init() {
mw := io.MultiWriter(os.Stdout, file) mw := io.MultiWriter(os.Stdout, file)
Log.SetOutput(mw) Log.SetOutput(mw)
configJson, err := json.Marshal(cfg.Config) configJson, err := json.Marshal(Config)
if err == nil { if err == nil {
Info(fmt.Sprintf("Using config: %s", configJson)) Info(fmt.Sprintf("Using config: %s", configJson))
} }

View File

@ -2,9 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"iris-test/app/cfg"
"iris-test/app/db"
"iris-test/app/logging"
"iris-test/app/views" "iris-test/app/views"
"time" "time"
@ -16,17 +13,17 @@ import (
var redisDB *redis.Database var redisDB *redis.Database
func createSessionEngine() *sessions.Sessions { 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{ redisDB = redis.New(redis.Config{
Network: "tcp", Network: "tcp",
Addr: redisAddr, Addr: redisAddr,
Timeout: time.Duration(30) * time.Second, Timeout: time.Duration(30) * time.Second,
MaxActive: 10, MaxActive: 10,
Username: cfg.Config.Redis.Username, Username: Config.Redis.Username,
Password: cfg.Config.Redis.Password, Password: Config.Redis.Password,
Database: cfg.Config.Redis.Database, Database: Config.Redis.Database,
Prefix: cfg.Config.Redis.Prefix, Prefix: Config.Redis.Prefix,
Driver: redis.GoRedis(), // defaults to this driver. Driver: redis.GoRedis(), // defaults to this driver.
// To set a custom, existing go-redis client, use the "SetClient" method: // To set a custom, existing go-redis client, use the "SetClient" method:
// Driver: redis.GoRedis().SetClient(customGoRedisClient) // Driver: redis.GoRedis().SetClient(customGoRedisClient)
@ -54,9 +51,9 @@ func createApp() *iris.Application {
} }
func main() { func main() {
cfg.Init() InitCfg()
logging.Init() InitLogging()
db.InitDB() InitDB()
app := createApp() app := createApp()
defer redisDB.Close() defer redisDB.Close()