feat: update framework

This commit is contained in:
Rogee
2024-11-29 15:03:47 +08:00
parent f7d95418a2
commit 391f217bd8
17 changed files with 368 additions and 132 deletions

View File

@@ -0,0 +1,34 @@
package postgres
import (
"database/sql"
"git.ipao.vip/rogeecn/atom/container"
"git.ipao.vip/rogeecn/atom/utils/opt"
_ "github.com/lib/pq"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var conf Config
if err := o.UnmarshalConfig(&conf); err != nil {
return err
}
return container.Container.Provide(func() (*sql.DB, error) {
log.Debugf("connect postgres with dsn: '%s'", conf.DSN())
db, err := sql.Open("postgres", conf.DSN())
if err != nil {
return nil, errors.Wrap(err, "connect database")
}
if err := db.Ping(); err != nil {
db.Close()
return nil, errors.Wrap(err, "ping database")
}
return db, err
}, o.DiOptions()...)
}