tercul-backend/internal/platform/config/config.go
google-labs-jules[bot] ac29aaa1d5 This is a work-in-progress commit for the core architectural refactoring of configuration handling.
The goal of this refactoring is to eliminate the global configuration singleton (`config.Cfg`) and replace it with explicit dependency injection of a `Config` struct.

This commit includes the following partial changes:
- The `Config` struct in `internal/platform/config/config.go` has been updated with all necessary fields.
- Several platform packages (`db`, `cache`, `auth`, `http`, `jobs/sync`) have been modified to accept the `*config.Config` struct.
- The API server entry point (`cmd/api/main.go`) has been updated to load and provide the configuration.
- A new worker entry point (`cmd/worker/main.go`) has been created to house the background job runner, as per the architecture defined in `refactor.md`.

NOTE: The build is currently broken as this refactoring is incomplete. This commit is for saving progress as requested.
2025-10-05 15:16:22 +00:00

57 lines
2.1 KiB
Go

package config
import (
"github.com/spf13/viper"
)
// Config stores all configuration of the application.
type Config struct {
Environment string `mapstructure:"ENVIRONMENT"`
ServerPort string `mapstructure:"SERVER_PORT"`
DBHost string `mapstructure:"DB_HOST"`
DBPort string `mapstructure:"DB_PORT"`
DBUser string `mapstructure:"DB_USER"`
DBPassword string `mapstructure:"DB_PASSWORD"`
DBName string `mapstructure:"DB_NAME"`
JWTSecret string `mapstructure:"JWT_SECRET"`
JWTExpiration int `mapstructure:"JWT_EXPIRATION_HOURS"`
WeaviateHost string `mapstructure:"WEAVIATE_HOST"`
WeaviateScheme string `mapstructure:"WEAVIATE_SCHEME"`
MigrationPath string `mapstructure:"MIGRATION_PATH"`
RedisAddr string `mapstructure:"REDIS_ADDR"`
RedisPassword string `mapstructure:"REDIS_PASSWORD"`
RedisDB int `mapstructure:"REDIS_DB"`
SyncBatchSize int `mapstructure:"SYNC_BATCH_SIZE"`
RateLimit int `mapstructure:"RATE_LIMIT"`
RateLimitBurst int `mapstructure:"RATE_LIMIT_BURST"`
}
// LoadConfig reads configuration from file or environment variables.
func LoadConfig() (*Config, error) {
viper.SetDefault("ENVIRONMENT", "development")
viper.SetDefault("SERVER_PORT", ":8080")
viper.SetDefault("DB_HOST", "localhost")
viper.SetDefault("DB_PORT", "5432")
viper.SetDefault("DB_USER", "user")
viper.SetDefault("DB_PASSWORD", "password")
viper.SetDefault("DB_NAME", "tercul")
viper.SetDefault("JWT_SECRET", "secret")
viper.SetDefault("JWT_EXPIRATION_HOURS", 24)
viper.SetDefault("WEAVIATE_HOST", "localhost:8080")
viper.SetDefault("WEAVIATE_SCHEME", "http")
viper.SetDefault("MIGRATION_PATH", "internal/data/migrations")
viper.SetDefault("REDIS_ADDR", "localhost:6379")
viper.SetDefault("REDIS_PASSWORD", "")
viper.SetDefault("REDIS_DB", 0)
viper.SetDefault("SYNC_BATCH_SIZE", 100)
viper.SetDefault("RATE_LIMIT", 10)
viper.SetDefault("RATE_LIMIT_BURST", 100)
viper.AutomaticEnv()
var config Config
if err := viper.Unmarshal(&config); err != nil {
return nil, err
}
return &config, nil
}