Skip to content

Configuration (core/config) ​

Configuration is loaded by github.com/mmycin/goforge/core/config using spf13/viper:

  1. setDefaults() registers code defaults.
  2. Reads .env (SetConfigFile(".env"), SetConfigType("env")). If .env is missing, logs a warning and continues with OS environment variables and defaults; parse errors fail immediately.
  3. Enables viper.AutomaticEnv() so OS environment variables always override .env.
  4. Returns *config.AllConfig (preferred for DI injection) and populates legacy package-level globals (config.App, config.DB, config.GRPC, config.HTTP, config.Encrypt, config.Log).

Complete Environment Variable Reference ​

Application (AppConfig / cfg.App) ​

FieldGo TypeEnv VarCode Default.env.example
NamestringAPP_NAME""goforge
VersionstringAPP_VERSION""1.0.0
ModulestringAPP_MODULE""github.com/mmycin/goforge
DebugboolAPP_DEBUGfalsetrue
KeystringAPP_KEY""(generate via goforge gen:key)
HoststringAPP_HOST"127.0.0.1"127.0.0.1
PortintAPP_PORT30003000

HTTP Server (HTTPConfig / cfg.HTTP) ​

FieldGo TypeEnv VarCode Default.env.example
RateLimitPerMinuteintRATE_LIMIT_PER_MINUTE0 (disabled)10
CORSAllowedOriginsstringCORS_ALLOWED_ORIGINS"" (treats as *)# *
TrustedProxiesstringTRUSTED_PROXIES"" (trusts none)# (empty)
ReadHeaderTimeouttime.DurationHTTP_READ_HEADER_TIMEOUT10s# 10s
ReadTimeouttime.DurationHTTP_READ_TIMEOUT30s# 30s
WriteTimeouttime.DurationHTTP_WRITE_TIMEOUT30s# 30s
IdleTimeouttime.DurationHTTP_IDLE_TIMEOUT120s# 120s
TLSEnabledboolTLS_ENABLEDfalsefalse
TLSCertFilestringTLS_CERT_FILE""cert.pem
TLSKeyFilestringTLS_KEY_FILE""key.pem

gRPC Server (GRPCConfig / cfg.GRPC) ​

FieldGo TypeEnv VarCode Default.env.example
EnableboolGRPC_ENABLEfalsetrue
ReflectionboolGRPC_REFLECTIONfalsetrue
HoststringGRPC_HOST"127.0.0.1"127.0.0.1
PortintGRPC_PORT5005150051
TLSEnabledboolGRPC_TLS_ENABLEDfalse# false
TLSCertFilestringGRPC_TLS_CERT_FILE""# cert.pem
TLSKeyFilestringGRPC_TLS_KEY_FILE""# key.pem

Database (DBConfig / cfg.DB) ​

FieldGo TypeEnv VarCode Default.env.example
ConnectionstringDB_CONNECTION""sqlite
NamestringDB_NAME""forge.db
DevNamestringDB_DEV_NAME""# forge_dev
HoststringDB_HOST""127.0.0.1
PortintDB_PORT03306
UsernamestringDB_USERNAME""root
PasswordstringDB_PASSWORD""root
MigratorstringDB_MIGRATOR"" (migrate defaults to atlas)atlas
SSLModestringDB_SSL_MODE"disable"# disable
MaxOpenConnsintDB_MAX_OPEN_CONNS25# 25
MaxIdleConnsintDB_MAX_IDLE_CONNS5# 5
ConnMaxLifetimetime.DurationDB_CONN_MAX_LIFETIME5m# 5m

Encryption (EncryptConfig / cfg.Encrypt) ​

FieldGo TypeEnv VarCode Default.env.example
KeystringENCRYPTION_KEY""(empty)
RoundsintENCRYPTION_ROUNDS012
SaltstringENCRYPTION_SALT""(empty)

Logging (LogConfig / cfg.Log) ​

FieldGo TypeEnv VarCode Default.env.example
TypestringLOG_TYPE"" (treats as terminal)terminal (terminal | file | both)
LevelstringLOG_LEVEL"info"info
PathstringLOG_PATH""storage/logs/app.log
FormatstringLOG_FORMAT"" (treats as log)log (json | yaml | log)
ColorstringLOG_COLOR"" (treats as auto)auto (auto | always | never)

core/console also honors standard terminal environment variables: NO_COLOR, FORCE_COLOR, TERM, and CI.


Validation (Validate vs ValidateForServe) ​

  • (*AllConfig).Validate() error (called on every CLI command via PersistentPreRunE):
    • Verifies APP_PORT and GRPC_PORT are in [0, 65535].
    • Requires non-blank TLS_CERT_FILE & TLS_KEY_FILE when TLS_ENABLED=true.
    • Requires non-blank GRPC_TLS_CERT_FILE & GRPC_TLS_KEY_FILE when GRPC_TLS_ENABLED=true.
    • Requires DB_CONNECTION (when non-empty) to be one of sqlite, postgres, mysql, sqlserver.
  • (*AllConfig).ValidateForServe() error (called by serve):
    • Runs Validate(), then rejects an empty or whitespace-only APP_KEY.

Adding Custom Configuration ​

Run goforge gen:config <name> to scaffold core/config/<name>.go and wire it into AllConfig and Load() (see CLI gen:config for the manual var (...) line check).

Released under the Apache-2.0 License.