Initial structure for the votes

This commit is contained in:
2025-10-31 12:52:38 -04:00
parent a52e73a295
commit 47310997ec
5 changed files with 147 additions and 0 deletions

47
server/db/db.go Normal file
View File

@@ -0,0 +1,47 @@
package db
import (
"database/sql"
"fmt"
"go-sjles-pta-vote/config"
_ "github.com/lib/pq"
)
var db *sql.DB
func Connect() error {
var err error
dbConfig := config.GetConfig()
db, err = sql.Open(
"postgres",
fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
dbConfig.Host,
dbConfig.Port,
dbConfig.User,
dbCofnig.Password,
dbConfig.Database
)
)
if err != nil {
return err
}
if err = db.Ping(); err != nil {
return err
}
fmt.Println("Connected to PostgreSQL")
return nil
}
// TODO: Is there a way to close automatically on app closure
func Close() {
if db != nil {
_ = db.Close()
}
}