25 lines
		
	
	
		
			464 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			25 lines
		
	
	
		
			464 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| package db
 | |
| 
 | |
| import (
 | |
| 	"github.com/jmoiron/sqlx"
 | |
| 	"github.com/spf13/viper"
 | |
| )
 | |
| 
 | |
| func New() *sqlx.DB {
 | |
| 	drv := viper.GetString("db.driver")
 | |
| 	dsn := viper.GetString("db.data_source_name")
 | |
| 
 | |
| 	db, err := sqlx.Connect(drv, dsn)
 | |
| 	if err != nil {
 | |
| 		panic("Failed to connect to the database: " + err.Error())
 | |
| 	}
 | |
| 
 | |
| 	// Verify the connection to the database is still alive
 | |
| 	err = db.Ping()
 | |
| 	if err != nil {
 | |
| 		panic("Failed to ping the database: " + err.Error())
 | |
| 	}
 | |
| 
 | |
| 	return db
 | |
| }
 |