[feat] add pkgs, dockerfile, makefile
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"go-api/pkg/config"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
var x *sqlx.DB
|
||||
|
||||
func Init(conf *config.Config) error {
|
||||
var err error
|
||||
x, err = sqlx.Connect("postgres", conf.Database.DSN)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
x.SetMaxIdleConns(int(conf.Database.IdleConn))
|
||||
x.SetMaxOpenConns(int(conf.Database.MaxConn))
|
||||
|
||||
if err := x.Ping(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Get() *sqlx.DB {
|
||||
if x == nil {
|
||||
panic("database not init")
|
||||
}
|
||||
return x
|
||||
}
|
||||
Reference in New Issue
Block a user