update database to use new config package

This commit is contained in:
Keiran 2025-08-07 18:35:16 +01:00
parent 3145c2c0aa
commit db776d34dd
2 changed files with 22 additions and 19 deletions

View File

@ -9,6 +9,7 @@ import (
"path/filepath"
"time"
"git.keircn.com/keiran/termcloud/internal/config"
"github.com/jackc/pgx/v5/pgxpool"
)
@ -42,11 +43,15 @@ type Object struct {
}
type BucketService struct {
pool *pgxpool.Pool
pool *pgxpool.Pool
storageDir string
}
func NewBucketService(pool *pgxpool.Pool) *BucketService {
return &BucketService{pool: pool}
func NewBucketService(pool *pgxpool.Pool, cfg *config.Config) *BucketService {
return &BucketService{
pool: pool,
storageDir: cfg.StorageDir,
}
}
func (s *BucketService) CreateUser(ctx context.Context, username, email, apiKey string, storageLimit int64) (*User, error) {
@ -136,12 +141,12 @@ func (s *BucketService) DeleteBucket(ctx context.Context, bucketName string, own
}
for _, obj := range objects {
if err := os.Remove(filepath.Join("storage", bucketName, obj.Key)); err != nil {
if err := os.Remove(filepath.Join(s.storageDir, bucketName, obj.Key)); err != nil {
continue
}
}
os.RemoveAll(filepath.Join("storage", bucketName))
os.RemoveAll(filepath.Join(s.storageDir, bucketName))
_, err = s.pool.Exec(ctx, "DELETE FROM buckets WHERE name = $1 AND owner_id = $2", bucketName, ownerID)
if err != nil {
@ -165,7 +170,7 @@ func (s *BucketService) UploadObject(ctx context.Context, bucketID int64, key st
return nil, fmt.Errorf("storage limit exceeded")
}
bucketDir := filepath.Join("storage", bucket.Name)
bucketDir := filepath.Join(s.storageDir, bucket.Name)
if err := os.MkdirAll(bucketDir, 0755); err != nil {
return nil, fmt.Errorf("failed to create bucket directory: %w", err)
}
@ -249,7 +254,7 @@ func (s *BucketService) DeleteObject(ctx context.Context, bucketID int64, key st
return err
}
filePath := filepath.Join("storage", bucket.Name, key)
filePath := filepath.Join(s.storageDir, bucket.Name, key)
if err := os.Remove(filePath); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to remove file: %w", err)
}
@ -272,7 +277,7 @@ func (s *BucketService) GetObjectFile(ctx context.Context, bucketName, key strin
return nil, err
}
filePath := filepath.Join("storage", bucketName, key)
filePath := filepath.Join(s.storageDir, bucketName, key)
file, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("failed to open file: %w", err)

View File

@ -4,28 +4,26 @@ import (
"context"
"fmt"
"log"
"os"
"time"
"git.keircn.com/keiran/termcloud/internal/config"
"github.com/jackc/pgx/v5/pgxpool"
)
func NewPool(ctx context.Context) (*pgxpool.Pool, error) {
connStr := os.Getenv("DATABASE_URL")
if connStr == "" {
func NewPool(ctx context.Context, cfg *config.Config) (*pgxpool.Pool, error) {
if cfg.DatabaseURL == "" {
return nil, fmt.Errorf("DATABASE_URL environment variable is not set")
}
config, err := pgxpool.ParseConfig(connStr)
config, err := pgxpool.ParseConfig(cfg.DatabaseURL)
if err != nil {
return nil, fmt.Errorf("unable to parse DATABASE_URL: %w", err)
}
config.MaxConns = 100
config.MinConns = 10
config.MaxConnLifetime = time.Hour
config.MaxConnIdleTime = time.Minute
config.HealthCheckPeriod = 5 * time.Second
config.MaxConns = cfg.MaxConnections
config.MinConns = cfg.MinConnections
config.MaxConnLifetime = cfg.ConnLifetime
config.MaxConnIdleTime = cfg.ConnIdleTime
config.HealthCheckPeriod = cfg.HealthCheckPeriod
pool, err := pgxpool.NewWithConfig(ctx, config)
if err != nil {