teldrive/database/database.go

64 lines
1.1 KiB
Go
Raw Normal View History

2023-08-07 03:32:46 +08:00
package database
import (
2023-08-12 19:21:42 +08:00
"log"
2023-08-07 03:32:46 +08:00
"os"
"time"
2023-08-12 19:21:42 +08:00
"github.com/divyam234/teldrive-go/utils"
2023-08-07 03:32:46 +08:00
"gorm.io/driver/postgres"
"gorm.io/gorm"
2023-08-12 19:21:42 +08:00
"gorm.io/gorm/logger"
2023-08-07 03:32:46 +08:00
"gorm.io/gorm/schema"
)
var DB *gorm.DB
func InitDB() {
var err error
2023-08-12 19:21:42 +08:00
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags),
logger.Config{
SlowThreshold: time.Second,
LogLevel: logger.Silent,
IgnoreRecordNotFoundError: true,
ParameterizedQueries: true,
Colorful: false,
},
)
DB, err = gorm.Open(postgres.Open(utils.GetConfig().DatabaseUrl), &gorm.Config{
2023-08-07 03:32:46 +08:00
NamingStrategy: schema.NamingStrategy{
TablePrefix: "teldrive.",
SingularTable: false,
},
PrepareStmt: false,
NowFunc: func() time.Time {
return time.Now().UTC()
},
2023-08-12 19:21:42 +08:00
Logger: newLogger,
2023-08-07 03:32:46 +08:00
})
if err != nil {
panic(err)
}
sqlDB, err := DB.DB()
if err != nil {
panic(err)
}
sqlDB.SetMaxIdleConns(10)
sqlDB.SetMaxOpenConns(100)
sqlDB.SetConnMaxLifetime(time.Hour)
go func() {
DB.Exec("select 1")
}()
// Auto-migrate the File struct to the database
//db.AutoMigrate(&File{})
}