Files
Crussell/backend/internal/dav/service_prod.go
T
popertotsandSisyphus 58996c553a refactor(internal): replace log.Fatal with panic, add timezone to DSN, add empty S3 bucket check
Replace log.Fatal in dev service init with panic for consistency. Add timezone=UTC to DAV connection DSN. Add IsEmpty() check for S3 dev bucket.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-06-24 23:43:14 +01:00

59 lines
1.1 KiB
Go

//go:build !dev
// +build !dev
package dav
import (
"context"
"fmt"
"os"
"github.com/jackc/pgx/v5/pgxpool"
)
var Service *BaseService
func init() {
if err := connect(); err != nil {
// Don't fatal in test mode - tests will use testdb instead
if os.Getenv("GO_TESTING") != "" {
return
}
panic("failed to initialize prod service: " + err.Error())
}
}
func connect() error {
dsn := fmt.Sprintf(
"postgres://%s:%s@%s:5432/%s?timezone=UTC&require_auth=scram-sha-256",
getEnv("POSTGRES_USER"),
getEnv("POSTGRES_PASSWORD"),
getEnv("POSTGRES_HOST"),
getEnv("POSTGRES_DB"),
)
pool, err := pgxpool.New(context.Background(), dsn)
if err != nil {
return err
}
Service = newBaseService(pool)
return testDB(pool)
}
func testDB(pool *pgxpool.Pool) error {
var n int
err := pool.QueryRow(context.Background(), "SELECT 1").Scan(&n)
if err != nil || n != 1 {
return fmt.Errorf("db test failed: %w", err)
}
return nil
}
func getEnv(key string) string {
if v := os.Getenv(key); v != "" {
return v
}
// Return empty string instead of fatal error - allows tests to run without prod env vars
return ""
}