mirror of
https://github.com/GMWalletApp/epusdt.git
synced 2026-07-07 10:16:15 +00:00
682aa3907a
- refine config loading with --config and current-directory .env - improve sqlite busy handling and runtime DB tuning - restore telegram payment notification and wallet input flow - add behavior-based config and callback recovery tests
64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
package dao
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/assimon/luuu/config"
|
|
"github.com/assimon/luuu/util/log"
|
|
"github.com/gookit/color"
|
|
"github.com/spf13/viper"
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/logger"
|
|
"gorm.io/gorm/schema"
|
|
)
|
|
|
|
// PostgreSQLInit 数据库初始化
|
|
func PostgreSQLInit() error {
|
|
var err error
|
|
user := viper.GetString("postgres_user")
|
|
pass := viper.GetString("postgres_passwd")
|
|
host := viper.GetString("postgres_host")
|
|
port := viper.GetString("postgres_port")
|
|
dbname := viper.GetString("postgres_database")
|
|
dsn := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=disable TimeZone=Asia/Shanghai",
|
|
user, pass, host, port, dbname,
|
|
)
|
|
Mdb, err = gorm.Open(postgres.Open(dsn),
|
|
&gorm.Config{
|
|
NamingStrategy: schema.NamingStrategy{
|
|
TablePrefix: viper.GetString("postgres_table_prefix"),
|
|
SingularTable: true,
|
|
},
|
|
Logger: logger.Default.LogMode(logger.Error),
|
|
},
|
|
)
|
|
if err != nil {
|
|
color.Red.Printf("[store_db] postgres open DB,err=%s\n", err)
|
|
// panic(err)
|
|
return err
|
|
}
|
|
|
|
if config.SQLDebug {
|
|
Mdb = Mdb.Debug()
|
|
}
|
|
sqlDB, err := Mdb.DB()
|
|
if err != nil {
|
|
color.Red.Printf("[store_db] postgres get DB,err=%s\n", err)
|
|
// panic(err)
|
|
return err
|
|
}
|
|
sqlDB.SetMaxIdleConns(viper.GetInt("postgres_max_idle_conns"))
|
|
sqlDB.SetMaxOpenConns(viper.GetInt("postgres_max_open_conns"))
|
|
sqlDB.SetConnMaxLifetime(time.Hour * time.Duration(viper.GetInt("postgres_max_life_time")))
|
|
err = sqlDB.Ping()
|
|
if err != nil {
|
|
color.Red.Printf("[store_db] postgres connDB err:%s", err.Error())
|
|
// panic(err)
|
|
return err
|
|
}
|
|
log.Sugar.Debug("[store_db] postgres connDB success")
|
|
return nil
|
|
}
|