47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
var connPool *pgxpool.Pool
|
|
|
|
func InitDB(connString string) error {
|
|
poolConfig, err := pgxpool.ParseConfig(connString)
|
|
if err != nil {
|
|
return fmt.Errorf("error while parsing connection string: %w", err)
|
|
}
|
|
|
|
poolConfig.MaxConns = 10
|
|
poolConfig.MinConns = 0
|
|
poolConfig.MaxConnLifetime = time.Hour
|
|
poolConfig.HealthCheckPeriod = 30 * time.Second
|
|
|
|
connPool, err = pgxpool.NewWithConfig(context.Background(), poolConfig)
|
|
if err != nil {
|
|
return fmt.Errorf("error while initializing DB connection pool: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//#region Users
|
|
|
|
func UserLogin(ctx context.Context, login, password string) (user_id string, err error) {
|
|
row := connPool.QueryRow(ctx, "SELECT id FROM users WHERE login=$1 AND password=crypt($2, password)", login, password)
|
|
err = row.Scan(&user_id)
|
|
return
|
|
}
|
|
|
|
func UserAuth(ctx context.Context, user_id string) (ok, editor bool) {
|
|
row := connPool.QueryRow(ctx, "SELECT editor FROM users WHERE id=$1", user_id)
|
|
err := row.Scan(&editor)
|
|
ok = (err == nil)
|
|
return
|
|
}
|
|
|
|
//#endregion Users
|