Skip to Content
DocumentationPackage LibraryConfig

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

FunctionSignatureDescription
LoadConfigfunc LoadConfig() (*AppConfig, error)Loads configuration from config.yaml (if present) and applies GOFASTA_-prefixed environment variable overrides
SetupDBfunc SetupDB(cfg DatabaseConfig) (*gorm.DB, error)Initializes a GORM database connection using the provided config
SetupDBWithMigratefunc 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: 1h

Load 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.com
cfg, _ := 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:

DriverValuePackage
PostgreSQLpostgresgorm.io/driver/postgres
MySQLmysqlgorm.io/driver/mysql
SQLitesqlitegorm.io/driver/sqlite
SQL Serversqlservergorm.io/driver/sqlserver
ClickHouseclickhousegorm.io/driver/clickhouse

Dependency Injection with Wire

Use the config in your Wire provider set:

var ConfigSet = wire.NewSet( config.LoadConfig, config.SetupDB, )
  • Logger — Configure logging output
  • Models — Base model that works with SetupDB
  • Seeds — Seed data after database setup
  • Cache — Reads the cache config block
  • Sessions — Reads the sessions config block
  • Queue — Reads the queue config block
  • Storage — Reads the storage config block
Last updated on