config

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 28, 2025 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package config provides configuration structures and loading functionality for the database MCP server.

Package config provides SSL/TLS configuration mapping for different database drivers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Validate

func Validate(cfg *Config) error

Validate checks the configuration for required fields and valid values. It ensures database type is supported, connection parameters are valid, and SSL modes are appropriate for the selected database type. Supports both connection string and individual parameter configuration. Returns an error describing any validation failures.

Types

type Config

type Config struct {
	Database DatabaseConfig `json:"database"` // Database connection configuration
}

Config represents the complete configuration for the database MCP server.

func Load

func Load() (*Config, error)

Load reads configuration from environment variables and .env file. It first loads variables from .env file if present, then processes environment variables which take precedence over .env file values. The configuration is validated before returning. Returns an error if loading or validation fails.

type ConnectionInfo

type ConnectionInfo struct {
	Type     string
	Host     string
	Port     int
	Database string
	Username string
	Password string
	SSLMode  string
}

ConnectionInfo holds parsed connection string information

func ParseConnectionString

func ParseConnectionString(connectionString string) (*ConnectionInfo, error)

ParseConnectionString parses a database connection string and returns ConnectionInfo. Supports both PostgreSQL and MySQL connection strings: - postgresql://[user[:password]@][host[:port]]/[dbname][?param1=value1&...] - mysql://[user[:password]@][host[:port]]/[dbname][?param1=value1&...]

func (*ConnectionInfo) ToConnectionString

func (info *ConnectionInfo) ToConnectionString() string

ToConnectionString converts ConnectionInfo back to a connection string format. This is useful for testing and validation purposes.

type DatabaseConfig

type DatabaseConfig struct {
	// Connection string approach (preferred)
	ConnectionString string `json:"connection_string" envconfig:"DB_CONNECTION_STRING"` // Full database connection string (postgresql:// or mysql://)

	// Legacy individual parameters (deprecated, but maintained for backwards compatibility)
	Type     string `json:"type" envconfig:"DB_TYPE"`         // Database type: "mysql" or "postgres"
	Host     string `json:"host" envconfig:"DB_HOST"`         // Database server hostname
	Port     int    `json:"port" envconfig:"DB_PORT"`         // Database server port
	Database string `json:"database" envconfig:"DB_NAME"`     // Primary database name to connect to
	Username string `json:"username" envconfig:"DB_USER"`     // Database username
	Password string `json:"password" envconfig:"DB_PASSWORD"` // Database password
	SSLMode  string `json:"ssl_mode" envconfig:"DB_SSL_MODE"` // SSL/TLS mode: "none", "prefer", or "require"

	// Additional configuration (applies to both approaches)
	AllowedDatabases []string `json:"allowed_databases" envconfig:"DB_ALLOWED_NAMES"` // List of allowed database names (empty means all allowed)
	MaxConns         int      `json:"max_conns" envconfig:"DB_MAX_CONNS"`             // Maximum number of open connections
	MaxIdleConns     int      `json:"max_idle_conns" envconfig:"DB_MAX_IDLE_CONNS"`   // Maximum number of idle connections
}

DatabaseConfig contains all settings required to connect to a database. It supports both MySQL and PostgreSQL databases with SSL/TLS configuration. Supports either individual connection parameters or a single connection string.

func (*DatabaseConfig) ApplyConnectionStringDefaults

func (cfg *DatabaseConfig) ApplyConnectionStringDefaults() error

ApplyConnectionStringDefaults parses the connection string and uses it to populate any individual configuration fields that are still at their default values. Individual parameters take precedence over connection string values when both are provided. This function should be called before environment variable processing to ensure env vars can override connection string parameters.

func (*DatabaseConfig) IsDatabaseAllowed

func (cfg *DatabaseConfig) IsDatabaseAllowed(databaseName string) bool

IsDatabaseAllowed checks if a database name is allowed to be accessed. If AllowedDatabases is empty, only the primary database (DB_NAME) is allowed. If AllowedDatabases is specified, only those databases plus the primary database are allowed.

func (*DatabaseConfig) ValidateSSLMode

func (cfg *DatabaseConfig) ValidateSSLMode() (SSLMode, error)

ValidateSSLMode checks if the configured SSL mode is valid and returns the parsed SSLMode. If no SSL mode is configured, it returns SSLModePrefer as default.

type SSLMode

type SSLMode string

SSLMode represents the common SSL/TLS configuration options that work across different database types. These values are mapped to database-specific SSL modes.

const (
	// SSLModeNone disables SSL/TLS encryption entirely
	SSLModeNone SSLMode = "none"

	// SSLModePrefer attempts to use SSL/TLS but falls back to unencrypted if unavailable
	SSLModePrefer SSLMode = "prefer"

	// SSLModeRequire mandates SSL/TLS encryption and fails if unavailable
	SSLModeRequire SSLMode = "require"
)

func ParseSSLMode

func ParseSSLMode(mode string) (SSLMode, error)

ParseSSLMode parses a string into an SSLMode, returning an error if invalid

func ValidSSLModes

func ValidSSLModes() []SSLMode

ValidSSLModes returns a list of all valid SSL mode values

func (SSLMode) IsValid

func (s SSLMode) IsValid() bool

IsValid checks if the given SSL mode string is valid

func (SSLMode) ToMySQLSSLMode

func (s SSLMode) ToMySQLSSLMode() (string, error)

ToMySQLSSLMode converts a common SSL mode to MySQL-specific SSL configuration

func (SSLMode) ToPostgreSQLSSLMode

func (s SSLMode) ToPostgreSQLSSLMode() (string, error)

ToPostgreSQLSSLMode converts a common SSL mode to PostgreSQL-specific SSL configuration

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL