Skip to content
This repository was archived by the owner on Nov 25, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions dwh/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"cloud.google.com/go/bigquery"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)

// Driver handles BigQuery operations for data warehouse queries
Expand All @@ -24,9 +25,12 @@ type monthlyUsageRow struct {
// Constructor and Cleanup
// ===========================================================================================

// NewDriver creates a new BigQuery Driver instance
func NewDriver(ctx context.Context, projectID string) (*Driver, error) {
clientBQ, err := bigquery.NewClient(ctx, projectID)
// NewDriver creates a new BigQuery Driver instance.
// If GOOGLE_APPLICATION_CREDENTIALS_FILE environment variable is set,
// it will use that JSON credentials file for authentication.
// Otherwise, it falls back to default credentials (ADC).
func NewDriver(ctx context.Context, projectID string, credsFile string) (*Driver, error) {
clientBQ, err := bigquery.NewClient(ctx, projectID, option.WithCredentialsFile(credsFile))
if err != nil {
return nil, fmt.Errorf("failed to connect to bigQuery: %w", err)
}
Expand Down
11 changes: 11 additions & 0 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const (
// - Example: "your-project-id"
gcpProjectIDEnv = "GCP_PROJECT_ID"

// [REQUIRED]: Path to the JSON credentials file for the data warehouse used by the rate limit store.
// - Example: "/path/to/credentials.json"
gcpCredentialsFileEnv = "GOOGLE_APPLICATION_CREDENTIALS_FILE"

// [OPTIONAL]: Port to run the external auth server on.
// - Default: 10001 if not set
portEnv = "PORT"
Expand Down Expand Up @@ -52,6 +56,7 @@ var postgresConnectionStringRegex = regexp.MustCompile(`^postgres(?:ql)?:\/\/[^:
type envVars struct {
postgresConnectionString string
gcpProjectID string
gcpCredentialsFile string
port int
loggerLevel string
portalAppStoreRefreshInterval time.Duration
Expand All @@ -66,6 +71,7 @@ func gatherEnvVars() (envVars, error) {
e := envVars{
postgresConnectionString: os.Getenv(postgresConnectionStringEnv),
gcpProjectID: os.Getenv(gcpProjectIDEnv),
gcpCredentialsFile: os.Getenv(gcpCredentialsFileEnv),
}

// Parse port environment variable (if provided)
Expand Down Expand Up @@ -126,6 +132,11 @@ func (e *envVars) validate() error {
return fmt.Errorf("%s is not set", gcpProjectIDEnv)
}

// GCP credentials file must be set
if e.gcpCredentialsFile == "" {
return fmt.Errorf("%s is not set", gcpCredentialsFileEnv)
}

// Connection string must match expected format
matched, err := regexp.MatchString(postgresConnectionStringRegex.String(), e.postgresConnectionString)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ func main() {
logger.Info().Msg("🐘 Successfully connected to postgres as a data source")

// Create a new data warehouse driver
dataWarehouseDriver, err := dwh.NewDriver(context.Background(), env.gcpProjectID)
dataWarehouseDriver, err := dwh.NewDriver(
context.Background(),
env.gcpProjectID,
env.gcpCredentialsFile,
)
if err != nil {
panic(err)
}
Expand Down