Restructure again
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
package common
|
package cfg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -1,7 +1,9 @@
|
|||||||
package common
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"iris-test/app/lib/cfg"
|
||||||
|
"iris-test/app/lib/logging"
|
||||||
"iris-test/app/repository"
|
"iris-test/app/repository"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -20,18 +22,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://",
|
||||||
Config.Database.Username, ":",
|
cfg.Config.Database.Username, ":",
|
||||||
Config.Database.Password, "@",
|
cfg.Config.Database.Password, "@",
|
||||||
Config.Database.Host, ":",
|
cfg.Config.Database.Host, ":",
|
||||||
Config.Database.Port, "/",
|
cfg.Config.Database.Port, "/",
|
||||||
Config.Database.Name,
|
cfg.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 Config.Application.DebugSQL {
|
if cfg.Config.Application.DebugSQL {
|
||||||
logLevel = gormLogger.Info
|
logLevel = gormLogger.Info
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,7 +43,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)
|
||||||
Log.Error(msg)
|
logging.Error(msg)
|
||||||
panic(msg)
|
panic(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1,9 +1,10 @@
|
|||||||
package common
|
package logging
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"iris-test/app/lib/cfg"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@ -28,9 +29,9 @@ func Warn(message string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func InitLogging() {
|
func InitLogging() {
|
||||||
logLevel, err := logrus.ParseLevel(Config.Application.LogLevel)
|
logLevel, err := logrus.ParseLevel(cfg.Config.Application.LogLevel)
|
||||||
if err != nil {
|
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)
|
Log.SetLevel(logLevel)
|
||||||
@ -42,14 +43,14 @@ func InitLogging() {
|
|||||||
DisableQuote: true,
|
DisableQuote: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
LogFile := Config.Application.LogFile
|
LogFile := cfg.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", Config.Application.LogFile, err)
|
msg := fmt.Sprintf("Failed to log to file %s: %s", cfg.Config.Application.LogFile, err)
|
||||||
Log.Warning(msg)
|
Log.Warning(msg)
|
||||||
panic(msg)
|
panic(msg)
|
||||||
}
|
}
|
||||||
@ -57,7 +58,7 @@ func InitLogging() {
|
|||||||
mw := io.MultiWriter(os.Stdout, file)
|
mw := io.MultiWriter(os.Stdout, file)
|
||||||
Log.SetOutput(mw)
|
Log.SetOutput(mw)
|
||||||
|
|
||||||
configJson, err := json.Marshal(Config)
|
configJson, err := json.Marshal(cfg.Config)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
Info(fmt.Sprintf("Using config: %s", configJson))
|
Info(fmt.Sprintf("Using config: %s", configJson))
|
||||||
}
|
}
|
||||||
22
app/main.go
22
app/main.go
@ -2,7 +2,9 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"iris-test/app/common"
|
"iris-test/app/lib/cfg"
|
||||||
|
"iris-test/app/lib/db"
|
||||||
|
"iris-test/app/lib/logging"
|
||||||
"iris-test/app/views"
|
"iris-test/app/views"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
@ -16,17 +18,17 @@ import (
|
|||||||
var redisDB *redis.Database
|
var redisDB *redis.Database
|
||||||
|
|
||||||
func createSessionEngine() *sessions.Sessions {
|
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{
|
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: common.Config.Redis.Username,
|
Username: cfg.Config.Redis.Username,
|
||||||
Password: common.Config.Redis.Password,
|
Password: cfg.Config.Redis.Password,
|
||||||
Database: common.Config.Redis.Database,
|
Database: cfg.Config.Redis.Database,
|
||||||
Prefix: common.Config.Redis.Prefix,
|
Prefix: cfg.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)
|
||||||
@ -84,7 +86,7 @@ func createApp() *iris.Application {
|
|||||||
accessLog := createAccessLog()
|
accessLog := createAccessLog()
|
||||||
|
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
app.Logger().SetLevel(common.Config.Application.LogLevel)
|
app.Logger().SetLevel(cfg.Config.Application.LogLevel)
|
||||||
app.Use(sessionsEngine.Handler())
|
app.Use(sessionsEngine.Handler())
|
||||||
app.UseRouter(accessLog.Handler)
|
app.UseRouter(accessLog.Handler)
|
||||||
app.RegisterView(iris.Jet("./app/templates", ".jet").Reload(true))
|
app.RegisterView(iris.Jet("./app/templates", ".jet").Reload(true))
|
||||||
@ -93,9 +95,9 @@ func createApp() *iris.Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
common.InitCfg()
|
cfg.InitCfg()
|
||||||
common.InitLogging()
|
logging.InitLogging()
|
||||||
common.InitDB()
|
db.InitDB()
|
||||||
|
|
||||||
app := createApp()
|
app := createApp()
|
||||||
defer redisDB.Close()
|
defer redisDB.Close()
|
||||||
|
|||||||
@ -20,7 +20,7 @@ func (u *User) TableName() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// func (u *User) SetPassword(password string) (string, error) {
|
// func (u *User) SetPassword(password string) (string, error) {
|
||||||
// secretKey := Config.Application.SecretKey
|
// secretKey := common.Config.Application.SecretKey
|
||||||
// bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
|
// bytes, err := bcrypt.GenerateFromPassword([]byte(password+secretKey), 14)
|
||||||
// return string(bytes), err
|
// return string(bytes), err
|
||||||
// }
|
// }
|
||||||
|
|||||||
Reference in New Issue
Block a user