Dump changes

This commit is contained in:
Andrey Chervyakov 2022-06-03 23:44:08 +06:00
parent e12550a643
commit aac2ea1b74
Signed by: cognio
GPG key ID: DAA316147EB0D58D
25 changed files with 129 additions and 76 deletions

View file

@ -1,47 +0,0 @@
package database
import (
"context"
"errors"
"fmt"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/knadh/koanf"
)
func Pool(config *koanf.Koanf) *pgxpool.Pool {
connStr, err := getConnectionString(config)
if err != nil {
panic(err)
}
pool, err := pgxpool.Connect(context.Background(), connStr)
if err != nil {
panic(err)
}
return pool
}
func getConnectionString(config *koanf.Koanf) (string, error) {
username := config.String("db.username")
if username == "" {
return "", errors.New("database username is missing")
}
password := config.String("db.password")
if password == "" {
return "", errors.New("database password is missing")
}
name := config.String("db.name")
if name == "" {
return "", errors.New("database name is missing")
}
host := config.String("db.host")
if name == "" {
return "", errors.New("database host is missing")
}
return fmt.Sprintf("postgresql://%s:%s@%s/%s", username, password, host, name), nil
}