This commit is contained in:
2026-03-04 18:23:00 +03:00
parent b81df5866b
commit 205d9cf8b0
15 changed files with 439 additions and 165 deletions

38
app/database.go Normal file
View 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()
}