新增数据库连接及gorm框架
This commit is contained in:
@@ -1,24 +0,0 @@
|
|||||||
package libs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/libp2p/go-libp2p/core/host"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Client struct {
|
|
||||||
This host.Host
|
|
||||||
}
|
|
||||||
|
|
||||||
var once sync.Once
|
|
||||||
|
|
||||||
var clientObj *Client
|
|
||||||
|
|
||||||
func InitClient() *Client {
|
|
||||||
once.Do(func() {
|
|
||||||
|
|
||||||
clientObj = &Client{}
|
|
||||||
})
|
|
||||||
|
|
||||||
return clientObj
|
|
||||||
}
|
|
||||||
58
libs/database.go
Normal file
58
libs/database.go
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package libs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"go-desk-service/config"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gorm.io/driver/postgres"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
var DB *gorm.DB
|
||||||
|
|
||||||
|
func Connect() (*gorm.DB, error) {
|
||||||
|
appConfig, dberr := config.LoadConfig()
|
||||||
|
if dberr != nil {
|
||||||
|
fmt.Println("读取配置失败")
|
||||||
|
}
|
||||||
|
|
||||||
|
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Shanghai",
|
||||||
|
appConfig.Database.Host,
|
||||||
|
appConfig.Database.Username,
|
||||||
|
appConfig.Database.Password,
|
||||||
|
appConfig.Database.Database,
|
||||||
|
appConfig.Database.Port)
|
||||||
|
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to connect to database: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlDB, err := db.DB()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get sql.DB: %w", err)
|
||||||
|
}
|
||||||
|
sqlDB.SetMaxIdleConns(10)
|
||||||
|
sqlDB.SetMaxOpenConns(100)
|
||||||
|
sqlDB.SetConnMaxLifetime(time.Hour)
|
||||||
|
|
||||||
|
log.Println("Database connection established")
|
||||||
|
DB = db
|
||||||
|
return db, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetDB() *gorm.DB {
|
||||||
|
if DB == nil {
|
||||||
|
panic("database connection is not initialized")
|
||||||
|
}
|
||||||
|
return DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func Close() error {
|
||||||
|
sqlDB, err := DB.DB()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return sqlDB.Close()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user