2022-05-02 08:19:37 -07:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-04 07:27:16 -07:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-12-22 06:42:43 -08:00
|
|
|
"net/url"
|
2022-05-02 08:19:37 -07:00
|
|
|
"os"
|
|
|
|
|
2023-01-04 13:41:29 -08:00
|
|
|
"codeberg.org/u1f320/pronouns.cc/backend/db/queries"
|
2022-11-19 18:44:20 -08:00
|
|
|
"codeberg.org/u1f320/pronouns.cc/backend/log"
|
2022-05-04 07:27:16 -07:00
|
|
|
"emperror.dev/errors"
|
2022-05-02 08:19:37 -07:00
|
|
|
"github.com/Masterminds/squirrel"
|
2023-01-04 13:41:29 -08:00
|
|
|
"github.com/jackc/pgconn"
|
|
|
|
"github.com/jackc/pgx/v4"
|
2022-05-02 08:19:37 -07:00
|
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
|
|
|
"github.com/mediocregopher/radix/v4"
|
2022-09-20 03:55:00 -07:00
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
2022-05-02 08:19:37 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var sq = squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar)
|
|
|
|
|
2022-06-16 05:54:15 -07:00
|
|
|
const ErrNothingToUpdate = errors.Sentinel("nothing to update")
|
|
|
|
|
2023-01-04 13:41:29 -08:00
|
|
|
type querier interface {
|
|
|
|
Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)
|
|
|
|
QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row
|
|
|
|
Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error)
|
|
|
|
}
|
|
|
|
|
2022-05-02 08:19:37 -07:00
|
|
|
type DB struct {
|
|
|
|
*pgxpool.Pool
|
|
|
|
|
|
|
|
Redis radix.Client
|
2022-09-20 03:55:00 -07:00
|
|
|
|
|
|
|
minio *minio.Client
|
|
|
|
minioBucket string
|
2022-12-22 06:42:43 -08:00
|
|
|
baseURL *url.URL
|
2023-01-04 13:41:29 -08:00
|
|
|
|
|
|
|
q queries.Querier
|
2022-05-02 08:19:37 -07:00
|
|
|
}
|
|
|
|
|
2022-09-20 03:55:00 -07:00
|
|
|
func New() (*DB, error) {
|
|
|
|
pool, err := pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL"))
|
2022-05-02 08:19:37 -07:00
|
|
|
if err != nil {
|
2022-09-20 03:55:00 -07:00
|
|
|
return nil, errors.Wrap(err, "creating postgres client")
|
2022-05-02 08:19:37 -07:00
|
|
|
}
|
|
|
|
|
2022-11-19 18:44:20 -08:00
|
|
|
var redis radix.Client
|
|
|
|
if os.Getenv("REDIS") != "" {
|
|
|
|
redis, err = (&radix.PoolConfig{}).New(context.Background(), "tcp", os.Getenv("REDIS"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "creating redis client")
|
|
|
|
}
|
|
|
|
} else {
|
2022-11-24 04:41:36 -08:00
|
|
|
log.Warn("$REDIS was empty! Any functionality using Redis (such as authentication) will not work")
|
|
|
|
redis = &dummyRedis{}
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
minioClient, err := minio.New(os.Getenv("MINIO_ENDPOINT"), &minio.Options{
|
|
|
|
Creds: credentials.NewStaticV4(os.Getenv("MINIO_ACCESS_KEY_ID"), os.Getenv("MINIO_ACCESS_KEY_SECRET"), ""),
|
|
|
|
Secure: os.Getenv("MINIO_SSL") == "true",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "creating minio client")
|
2022-05-02 08:19:37 -07:00
|
|
|
}
|
|
|
|
|
2022-12-22 06:42:43 -08:00
|
|
|
baseURL, err := url.Parse(os.Getenv("BASE_URL"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "parsing base URL")
|
|
|
|
}
|
|
|
|
|
2022-05-02 08:19:37 -07:00
|
|
|
db := &DB{
|
|
|
|
Pool: pool,
|
|
|
|
Redis: redis,
|
2022-09-20 03:55:00 -07:00
|
|
|
|
|
|
|
minio: minioClient,
|
|
|
|
minioBucket: os.Getenv("MINIO_BUCKET"),
|
2022-12-22 06:42:43 -08:00
|
|
|
baseURL: baseURL,
|
2023-01-04 13:41:29 -08:00
|
|
|
|
|
|
|
q: queries.NewQuerier(pool),
|
2022-05-02 08:19:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MultiCmd executes the given Redis commands in order.
|
|
|
|
// If any return an error, the function is aborted.
|
|
|
|
func (db *DB) MultiCmd(ctx context.Context, cmds ...radix.Action) error {
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
err := db.Redis.Do(ctx, cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-04 07:27:16 -07:00
|
|
|
|
|
|
|
// SetJSON sets the given key to v marshaled as JSON.
|
|
|
|
func (db *DB) SetJSON(ctx context.Context, key string, v any, args ...string) error {
|
|
|
|
b, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "marshaling json")
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdArgs := make([]string, 0, len(args)+2)
|
|
|
|
cmdArgs = append(cmdArgs, key, string(b))
|
|
|
|
cmdArgs = append(cmdArgs, args...)
|
|
|
|
|
|
|
|
err = db.Redis.Do(ctx, radix.Cmd(nil, "SET", cmdArgs...))
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "writing to Redis")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetJSON gets the given key as a JSON object.
|
|
|
|
func (db *DB) GetJSON(ctx context.Context, key string, v any) error {
|
|
|
|
var b []byte
|
|
|
|
|
|
|
|
err := db.Redis.Do(ctx, radix.Cmd(&b, "GET", key))
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "reading from Redis")
|
|
|
|
}
|
|
|
|
|
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if v == nil {
|
|
|
|
return fmt.Errorf("nil pointer passed into GetJSON")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(b, v)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "unmarshaling json")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDelJSON gets the given key as a JSON object and deletes it.
|
|
|
|
func (db *DB) GetDelJSON(ctx context.Context, key string, v any) error {
|
|
|
|
var b []byte
|
|
|
|
|
|
|
|
err := db.Redis.Do(ctx, radix.Cmd(&b, "GETDEL", key))
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "reading from Redis")
|
|
|
|
}
|
|
|
|
|
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if v == nil {
|
|
|
|
return fmt.Errorf("nil pointer passed into GetDelJSON")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(b, v)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "unmarshaling json")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|