mirror of
https://github.com/purofle/remake_bot.git
synced 2025-06-17 14:53:29 +08:00
35 lines
695 B
Go
35 lines
695 B
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
_ "github.com/lib/pq"
|
|
"go.uber.org/fx"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
var Module = fx.Provide(NewDatabase)
|
|
|
|
func NewDatabase(lc fx.Lifecycle, logger *zap.Logger) (*sql.DB, error) {
|
|
connStr := "postgresql://postgres:114514@localhost:5432/postgres?sslmode=disable"
|
|
|
|
db, err := sql.Open("postgres", connStr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 在应用启动时验证连接
|
|
lc.Append(fx.Hook{
|
|
OnStart: func(context.Context) error {
|
|
logger.Info("[db] Verifying connection...")
|
|
return db.Ping()
|
|
},
|
|
OnStop: func(context.Context) error {
|
|
logger.Info("[db] Closing connection...")
|
|
return db.Close()
|
|
},
|
|
})
|
|
|
|
return db, nil
|
|
}
|