xmr-remote-nodes/internal/database/mysql.go
ditatompel 5496692c5d
Avoid naming module using a domain name pattern
I hope it will be less discoverable by other users and less likely to
be used unintentionally in other projects.
2024-05-08 21:35:04 +07:00

49 lines
933 B
Go

package database
import (
"fmt"
"xmr-remote-nodes/internal/config"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
// DB holds the database
type DB struct{ *sqlx.DB }
// database instance
var defaultDB = &DB{}
// connect sets the db client of database using configuration
func (db *DB) connect(cfg *config.DB) (err error) {
dbURI := fmt.Sprintf("%s:%s@(%s:%d)/%s",
cfg.User,
cfg.Password,
cfg.Host,
cfg.Port,
cfg.Name,
)
db.DB, err = sqlx.Connect("mysql", dbURI)
if err != nil {
return err
}
// Try to ping database.
if err := db.Ping(); err != nil {
defer db.Close() // close database connection
return fmt.Errorf("can't sent ping to database, %w", err)
}
return nil
}
// GetDB returns db instance
func GetDB() *DB {
return defaultDB
}
// ConnectDB sets the db client of database using default configuration
func ConnectDB() error {
return defaultDB.connect(config.DBCfg())
}