fathom/db/db.go

30 lines
739 B
Go
Raw Normal View History

2016-12-08 08:16:43 +00:00
package db
2016-11-19 21:35:23 +00:00
import (
"database/sql"
_"github.com/go-sql-driver/mysql"
"log"
"os"
"fmt"
2016-11-19 21:35:23 +00:00
)
2016-12-08 08:16:43 +00:00
var Conn *sql.DB
2016-11-19 21:35:23 +00:00
func SetupDatabaseConnection() *sql.DB {
2016-11-19 21:35:23 +00:00
var err error
var dataSourceName = fmt.Sprintf("%s:%s@%s/%s", os.Getenv("ANA_DATABASE_USER"), os.Getenv("ANA_DATABASE_PASSWORD"), os.Getenv("ANA_DATABASE_HOST"), os.Getenv("ANA_DATABASE_NAME"))
2016-12-08 08:16:43 +00:00
Conn, err = sql.Open("mysql", dataSourceName)
2016-11-19 21:35:23 +00:00
if err != nil {
log.Fatal(err.Error()) // Just for example purpose. You should use proper error handling instead of panic
}
// Open doesn't open a connection. Validate DSN data:
2016-12-08 08:16:43 +00:00
err = Conn.Ping()
2016-11-19 21:35:23 +00:00
if err != nil {
log.Fatal(err.Error()) // proper error handling instead of panic in your app
}
2016-12-08 08:16:43 +00:00
return Conn
2016-11-19 21:35:23 +00:00
}