Config
The config package provides configuration loading from YAML files with environment variable overrides, and database connection setup for all supported drivers.
Import
import "github.com/gofastadev/gofasta/pkg/config"Key Types
AppConfig
The root configuration struct that maps to your config.yaml file. Internally, the config system uses koanf for loading YAML and environment variable overrides.
type AppConfig struct {
Server ServerConfig `koanf:"server"`
Database DatabaseConfig `koanf:"database"`
GraphQL GraphQLConfig `koanf:"graphql"`
Log LogConfig `koanf:"log"`
Email EmailConfig `koanf:"email"`
Jobs []JobConfig `koanf:"jobs"`
Auth AuthConfig `koanf:"auth"`
RateLimit RateLimitConfig `koanf:"rate_limit"`
Cache CacheConfig `koanf:"cache"`
Security SecurityConfig `koanf:"security"`
Storage StorageConfig `koanf:"storage"`
Queue QueueConfig `koanf:"queue"`
WebSocket WebSocketConfig `koanf:"websocket"`
I18n I18nConfig `koanf:"i18n"`
FeatureFlag FeatureFlagConfig `koanf:"feature_flag"`
Encryption EncryptionConfig `koanf:"encryption"`
Session SessionConfig `koanf:"session"`
Observability ObservabilityConfig `koanf:"observability"`
}ServerConfig
type ServerConfig struct {
Port string `koanf:"port"`
ShutdownTimeout time.Duration `koanf:"shutdown_timeout"`
AllowedOrigins []string `koanf:"allowed_origins"`
}DatabaseConfig
type DatabaseConfig struct {
Driver string `koanf:"driver"`
Host string `koanf:"host"`
Port string `koanf:"port"`
User string `koanf:"user"`
Password string `koanf:"password"`
Name string `koanf:"name"`
SSLMode string `koanf:"sslmode"`
MaxIdle int `koanf:"max_idle"`
MaxOpen int `koanf:"max_open"`
MaxLife time.Duration `koanf:"max_life"`
}Key Functions
| Function | Signature | Description |
|---|---|---|
LoadConfig | func LoadConfig() (*AppConfig, error) | Loads configuration from config.yaml (if present) and applies GOFASTA_-prefixed environment variable overrides |
SetupDB | func SetupDB(cfg DatabaseConfig) (*gorm.DB, error) | Initializes a GORM database connection using the provided config |
SetupDBWithMigrate | func SetupDBWithMigrate(cfg DatabaseConfig, models ...interface{}) (*gorm.DB, error) | Sets up the database and runs auto-migration for the given models |
Usage
Loading Configuration
Create a config.yaml in your project root:
server:
port: "8080"
shutdown_timeout: 15s
allowed_origins:
- "*"
database:
driver: postgres
host: localhost
port: "5432"
name: mydb
user: postgres
password: secret
sslmode: disable
max_idle: 10
max_open: 100
max_life: 1hLoad it in your application:
cfg, err := config.LoadConfig()
if err != nil {
log.Fatalf("failed to load config: %v", err)
}
fmt.Println(cfg.Server.Port) // "8080"Environment Variable Overrides
Any configuration field can be overridden by setting an environment variable with the GOFASTA_ prefix. The prefix is stripped, and underscores map to nested keys (e.g., GOFASTA_DATABASE_HOST maps to database.host). Environment variables take precedence over YAML values.
export GOFASTA_SERVER_PORT=9090
export GOFASTA_DATABASE_HOST=production-db.example.comcfg, _ := config.LoadConfig()
// cfg.Server.Port is now "9090", not "8080"
// cfg.Database.Host is now "production-db.example.com"Setting Up the Database
cfg, _ := config.LoadConfig()
db, err := config.SetupDB(cfg.Database)
if err != nil {
log.Fatalf("failed to connect to database: %v", err)
}
// With auto-migration
db, err := config.SetupDBWithMigrate(cfg.Database, &User{}, &Post{})
if err != nil {
log.Fatalf("failed to setup database: %v", err)
}Supported Database Drivers
The driver field in DatabaseConfig accepts the following values:
| Driver | Value | Package |
|---|---|---|
| PostgreSQL | postgres | gorm.io/driver/postgres |
| MySQL | mysql | gorm.io/driver/mysql |
| SQLite | sqlite | gorm.io/driver/sqlite |
| SQL Server | sqlserver | gorm.io/driver/sqlserver |
| ClickHouse | clickhouse | gorm.io/driver/clickhouse |
Dependency Injection with Wire
Use the config in your Wire provider set:
var ConfigSet = wire.NewSet(
config.LoadConfig,
config.SetupDB,
)