2022-10-20 16:59:33 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"GoWeb/app"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2022-11-02 04:07:22 +00:00
|
|
|
_ "github.com/lib/pq"
|
2023-02-14 04:28:11 +00:00
|
|
|
"log"
|
2022-10-20 16:59:33 +00:00
|
|
|
)
|
|
|
|
|
2023-08-03 17:09:40 +00:00
|
|
|
// Connect returns a new database connection
|
|
|
|
func Connect(app *app.App) *sql.DB {
|
2022-10-20 16:59:33 +00:00
|
|
|
postgresConfig := fmt.Sprintf("host=%s port=%s user=%s "+
|
|
|
|
"password=%s dbname=%s sslmode=disable",
|
|
|
|
app.Config.Db.Ip, app.Config.Db.Port, app.Config.Db.User, app.Config.Db.Password, app.Config.Db.Name)
|
|
|
|
|
|
|
|
db, err := sql.Open("postgres", postgresConfig)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Ping()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Connected to database successfully on " + app.Config.Db.Ip + ":" + app.Config.Db.Port + " using database " + app.Config.Db.Name)
|
|
|
|
|
|
|
|
return db
|
|
|
|
}
|