Skip to Content
DocumentationFramework APIConfig

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.

type AppConfig struct { App AppSettings `yaml:"app"` Database DatabaseConfig `yaml:"database"` Auth AuthConfig `yaml:"auth"` Cache CacheConfig `yaml:"cache"` Logger LoggerConfig `yaml:"logger"` Mailer MailerConfig `yaml:"mailer"` Storage StorageConfig `yaml:"storage"` }

AppSettings

type AppSettings struct { Name string `yaml:"name" env:"APP_NAME"` Port int `yaml:"port" env:"APP_PORT"` Environment string `yaml:"environment" env:"APP_ENV"` Debug bool `yaml:"debug" env:"APP_DEBUG"` }

DatabaseConfig

type DatabaseConfig struct { Driver string `yaml:"driver" env:"DB_DRIVER"` Host string `yaml:"host" env:"DB_HOST"` Port int `yaml:"port" env:"DB_PORT"` Name string `yaml:"name" env:"DB_NAME"` User string `yaml:"user" env:"DB_USER"` Password string `yaml:"password" env:"DB_PASSWORD"` SSLMode string `yaml:"sslmode" env:"DB_SSLMODE"` }

Key Functions

FunctionSignatureDescription
LoadConfigfunc LoadConfig(path string) (*AppConfig, error)Loads configuration from a YAML file and applies 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:

app: name: my-service port: 8080 environment: development debug: true database: driver: postgres host: localhost port: 5432 name: mydb user: postgres password: secret sslmode: disable

Load it in your application:

cfg, err := config.LoadConfig("config.yaml") if err != nil { log.Fatalf("failed to load config: %v", err) } fmt.Println(cfg.App.Name) // "my-service"

Environment Variable Overrides

Any field tagged with env can be overridden by setting the corresponding environment variable. Environment variables take precedence over YAML values.

export APP_PORT=9090 export DB_HOST=production-db.example.com
cfg, _ := config.LoadConfig("config.yaml") // cfg.App.Port is now 9090, not 8080 // cfg.Database.Host is now "production-db.example.com"

Setting Up the Database

cfg, _ := config.LoadConfig("config.yaml") 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
Last updated on