45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package initialization
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/spf13/viper"
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/gorm"
|
|
"log"
|
|
"testData/global"
|
|
"testData/model"
|
|
"time"
|
|
)
|
|
|
|
func InitPostgres() {
|
|
//dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai",
|
|
// "192.168.0.56",
|
|
// "tpdb",
|
|
// "TpMysql12#$",
|
|
// "test_data",
|
|
// "5432",
|
|
//)
|
|
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai",
|
|
viper.GetString("postgres.host"),
|
|
viper.GetString("postgres.user"),
|
|
viper.GetString("postgres.password"),
|
|
viper.GetString("postgres.dbname"),
|
|
viper.GetString("postgres.port"),
|
|
)
|
|
if db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
|
|
DisableForeignKeyConstraintWhenMigrating: true,
|
|
//Logger: logger.Default.LogMode(logger.Silent),
|
|
}); err != nil {
|
|
log.Printf("PostgreSQL Connection error: %s", err)
|
|
panic(err)
|
|
} else {
|
|
postgresDB, _ := db.DB()
|
|
postgresDB.SetMaxIdleConns(10)
|
|
postgresDB.SetMaxOpenConns(100)
|
|
postgresDB.SetConnMaxLifetime(time.Hour)
|
|
_ = db.AutoMigrate(&model.FileHandled{})
|
|
global.PostGreSQL = db
|
|
_ = global.PostGreSQL.AutoMigrate(&model.FileHandled{})
|
|
}
|
|
}
|