test
This commit is contained in:
38
app/database.go
Normal file
38
app/database.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/vinovest/sqlx"
|
||||
)
|
||||
import _ "github.com/lib/pq"
|
||||
|
||||
var db *sqlx.DB = nil
|
||||
|
||||
func init() {
|
||||
if db == nil {
|
||||
connectPsql()
|
||||
}
|
||||
}
|
||||
|
||||
func getDSN() string {
|
||||
user := os.Getenv("PSQL_USER")
|
||||
password := os.Getenv("PSQL_PASS")
|
||||
database := os.Getenv("PSQL_NAME")
|
||||
host, exists := os.LookupEnv("PSQL_HOST")
|
||||
if !exists {
|
||||
host = "localhost"
|
||||
}
|
||||
return fmt.Sprintf("postgresql://%s:%s@%s/%s?sslmode=disable", user, password, host, database)
|
||||
}
|
||||
func connectPsql() {
|
||||
var err error
|
||||
db, err = sqlx.Connect("postgres", getDSN())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
func ClosePsql() error {
|
||||
return db.Close()
|
||||
}
|
||||
Reference in New Issue
Block a user