Restructure again
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
package common
|
||||
package cfg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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))
|
||||
}
|
||||
22
app/main.go
22
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()
|
||||
|
||||
@ -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
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user