Allow overriding default database.yaml paths via DATABASE_CONFIG_FILE environment variable. When set, uses single specified file instead of default ["database.yaml", "/vault/secrets/database.yaml"] search paths. Maintains backward compatibility when env var not set.
73 lines
2.2 KiB
Go
73 lines
2.2 KiB
Go
package database
|
|
|
|
import (
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
// Config represents the database configuration structure
|
|
type Config struct {
|
|
MySQL DBConfig `yaml:"mysql"`
|
|
}
|
|
|
|
// DBConfig represents the MySQL database configuration
|
|
type DBConfig struct {
|
|
DSN string `default:"" flag:"dsn" usage:"Database DSN"`
|
|
User string `default:"" flag:"user"`
|
|
Pass string `default:"" flag:"pass"`
|
|
DBName string // Optional database name override
|
|
}
|
|
|
|
// ConfigOptions allows customization of database opening behavior
|
|
type ConfigOptions struct {
|
|
// ConfigFiles is a list of config file paths to search for database configuration
|
|
ConfigFiles []string
|
|
|
|
// EnablePoolMonitoring enables connection pool metrics collection
|
|
EnablePoolMonitoring bool
|
|
|
|
// PrometheusRegisterer for metrics collection. If nil, no metrics are collected.
|
|
PrometheusRegisterer prometheus.Registerer
|
|
|
|
// Connection pool settings
|
|
MaxOpenConns int
|
|
MaxIdleConns int
|
|
ConnMaxLifetime time.Duration
|
|
}
|
|
|
|
// getConfigFiles returns the list of config files to search for database configuration.
|
|
// If DATABASE_CONFIG_FILE environment variable is set, it returns that single file.
|
|
// Otherwise, it returns the default paths.
|
|
func getConfigFiles() []string {
|
|
if configFile := os.Getenv("DATABASE_CONFIG_FILE"); configFile != "" {
|
|
return []string{configFile}
|
|
}
|
|
return []string{"database.yaml", "/vault/secrets/database.yaml"}
|
|
}
|
|
|
|
// DefaultConfigOptions returns the standard configuration options used by API package
|
|
func DefaultConfigOptions() ConfigOptions {
|
|
return ConfigOptions{
|
|
ConfigFiles: getConfigFiles(),
|
|
EnablePoolMonitoring: true,
|
|
PrometheusRegisterer: prometheus.DefaultRegisterer,
|
|
MaxOpenConns: 25,
|
|
MaxIdleConns: 10,
|
|
ConnMaxLifetime: 3 * time.Minute,
|
|
}
|
|
}
|
|
|
|
// MonitorConfigOptions returns configuration options optimized for Monitor package
|
|
func MonitorConfigOptions() ConfigOptions {
|
|
return ConfigOptions{
|
|
ConfigFiles: getConfigFiles(),
|
|
EnablePoolMonitoring: false, // Monitor doesn't need metrics
|
|
PrometheusRegisterer: nil, // No Prometheus dependency
|
|
MaxOpenConns: 10,
|
|
MaxIdleConns: 5,
|
|
ConnMaxLifetime: 3 * time.Minute,
|
|
}
|
|
}
|