Restructure app
This commit is contained in:
@ -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
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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))
|
||||
}
|
||||
19
app/main.go
19
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()
|
||||
|
||||
Reference in New Issue
Block a user