refactored totally, but now exits and doesn't serve for some reason
This commit is contained in:
62
models/db.go
62
models/db.go
@@ -1,62 +0,0 @@
|
||||
package models
|
||||
|
||||
import "os"
|
||||
import "time"
|
||||
import "github.com/astaxie/beego/orm"
|
||||
import "github.com/rs/zerolog/log"
|
||||
import _ "github.com/lib/pq" //revive:disable-line
|
||||
|
||||
var ormObject orm.Ormer
|
||||
|
||||
func initialize() {
|
||||
if os.Getenv("DEBUG") != "" {
|
||||
orm.Debug = true
|
||||
}
|
||||
|
||||
connectToDb()
|
||||
syncDB()
|
||||
}
|
||||
|
||||
// ConnectToDb - Initializes the ORM and Connection to the postgres DB
|
||||
func connectToDb() {
|
||||
|
||||
orm.DefaultTimeLoc = time.UTC
|
||||
|
||||
dbURL := os.Getenv("POSTGRES_DB_URL")
|
||||
|
||||
orm.RegisterDriver("postgres", orm.DRPostgres)
|
||||
orm.RegisterDataBase("default", "postgres", dbURL)
|
||||
orm.SetMaxIdleConns("default", 1)
|
||||
orm.SetMaxOpenConns("default", 5)
|
||||
|
||||
orm.RegisterModel(new(Merp))
|
||||
ormObject = orm.NewOrm()
|
||||
ormObject.Using("default")
|
||||
|
||||
}
|
||||
|
||||
// SyncDB() is responsible for creating the schema in the database
|
||||
func syncDB() {
|
||||
// Database alias.
|
||||
name := "default"
|
||||
|
||||
// Drop table and re-create.
|
||||
force := false
|
||||
|
||||
// Print log.
|
||||
verbose := true
|
||||
|
||||
// Error.
|
||||
err := orm.RunSyncdb(name, force, verbose)
|
||||
if err != nil {
|
||||
log.Fatal().Msg(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// GetOrmObject - Getter function for the ORM object with which we can query the database
|
||||
func GetOrmObject() orm.Ormer {
|
||||
if ormObject == nil {
|
||||
initialize()
|
||||
}
|
||||
return ormObject
|
||||
}
|
||||
Reference in New Issue
Block a user