init: first steps
This commit is contained in:
53
modules/db/db.go
Normal file
53
modules/db/db.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
appLog "git.ostiwe.com/ostiwe-com/status/modules/log"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
var Global *gorm.DB
|
||||
|
||||
func init() {
|
||||
appLog.Global.Put(appLog.DATABASE, logrus.New())
|
||||
}
|
||||
|
||||
func Connect() (*gorm.DB, error) {
|
||||
dsn := fmt.Sprintf(
|
||||
"host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=%s",
|
||||
os.Getenv("DATABASE_HOST"),
|
||||
os.Getenv("DATABASE_USER"),
|
||||
os.Getenv("DATABASE_PASS"),
|
||||
os.Getenv("DATABASE_DB"),
|
||||
os.Getenv("DATABASE_PORT"),
|
||||
os.Getenv("DATABASE_TZ"),
|
||||
)
|
||||
|
||||
newLogger := logger.New(
|
||||
appLog.Global.Get(appLog.DATABASE),
|
||||
logger.Config{
|
||||
SlowThreshold: time.Second, // Slow SQL threshold
|
||||
LogLevel: logger.Info, // Log level
|
||||
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
|
||||
ParameterizedQueries: false, // Don't include params in the SQL log
|
||||
Colorful: false, // Disable color
|
||||
},
|
||||
)
|
||||
|
||||
return gorm.Open(
|
||||
postgres.Open(dsn),
|
||||
&gorm.Config{
|
||||
Logger: newLogger,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func SetGlobal(bd *gorm.DB) {
|
||||
Global = bd
|
||||
}
|
||||
Reference in New Issue
Block a user