You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.6 KiB
Go

// Package postgres provides PostgreSQL database client for the KnowFoolery application.
package postgres
import (
"context"
"fmt"
"time"
)
// Config holds the configuration for the PostgreSQL client.
type Config struct {
Host string
Port int
User string
Password string
Database string
SSLMode string
MaxOpenConns int
MaxIdleConns int
ConnMaxLifetime time.Duration
ConnMaxIdleTime time.Duration
}
// DefaultConfig returns a default configuration for development.
func DefaultConfig() Config {
return Config{
Host: "localhost",
Port: 5432,
User: "postgres",
Password: "postgres",
Database: "knowfoolery",
SSLMode: "disable",
MaxOpenConns: 25,
MaxIdleConns: 10,
ConnMaxLifetime: 5 * time.Minute,
ConnMaxIdleTime: 1 * time.Minute,
}
}
// DSN returns the PostgreSQL connection string.
func (c Config) DSN() string {
return fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
c.Host, c.Port, c.User, c.Password, c.Database, c.SSLMode,
)
}
// URL returns the PostgreSQL connection URL.
func (c Config) URL() string {
return fmt.Sprintf(
"postgresql://%s:%s@%s:%d/%s?sslmode=%s",
c.User, c.Password, c.Host, c.Port, c.Database, c.SSLMode,
)
}
// Client wraps database operations.
// This is a placeholder that should be implemented with actual database client.
type Client struct {
config Config
}
// NewClient creates a new PostgreSQL client.
// Note: Actual implementation should use Ent client from each service.
func NewClient(config Config) (*Client, error) {
// This is a placeholder. The actual Ent client should be created
// in each service's infrastructure layer.
return &Client{
config: config,
}, nil
}
// Close closes the database connection.
func (c *Client) Close() error {
// Placeholder for closing database connection
return nil
}
// Ping checks if the database connection is alive.
func (c *Client) Ping(ctx context.Context) error {
// Placeholder for ping implementation
return nil
}
// HealthCheck performs a health check on the database.
func (c *Client) HealthCheck(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return c.Ping(ctx)
}
// ConfigFromEnv creates a Config from environment variables.
// This is a placeholder that should be implemented with actual env parsing.
func ConfigFromEnv() Config {
// TODO: Implement environment variable parsing
// Should use os.Getenv or a configuration library
return DefaultConfig()
}