infoalloggi-monorepo/apps/backend/config/config.go
Marco Pedone 208deeac28 Refactor image handling in announcement components
- Updated CardAnnuncio and related components to use a new images structure instead of url_immagini.
- Removed references to url_immagini and replaced them with images array containing img and thumb properties.
- Adjusted API responses to include images in the new format.
- Modified middleware and storage service to accommodate new image handling.
- Cleaned up unused code and schemas related to old image references.
- Updated Docker configuration for storage service.
2025-10-27 17:58:42 +01:00

112 lines
2.2 KiB
Go

package config
import (
"fmt"
"os"
"path/filepath"
"strconv"
"sync"
"github.com/joho/godotenv"
)
// Config holds all application configuration
type Config struct {
Database struct {
Host string
Port int
User string
Password string
DBName string
SSLMode string
}
Images struct {
Enabled bool
ConcurrentLimit int
Path string
}
Storage struct {
Endpoint string
}
Server struct {
Port int
}
}
var (
Cfg Config
once sync.Once
)
// Load initializes configuration once
func Load() *Config {
once.Do(func() {
// Try loading from .env file first
_ = godotenv.Load()
pwd, err := os.Getwd()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Database config
Cfg.Database.Host = getEnv("PGHOST", "localhost")
Cfg.Database.Port = getEnvAsInt("PGPORT", 5432)
Cfg.Database.User = getEnv("POSTGRES_USER", "postgres")
Cfg.Database.Password = getEnv("POSTGRES_PASSWORD", "")
Cfg.Database.DBName = getEnv("POSTGRES_DB", "postgres")
Cfg.Database.SSLMode = getEnv("PGSSLMODE", "true")
// Image processing config
Cfg.Images.Enabled = getEnvAsBool("IMAGEOPTION", true)
Cfg.Images.ConcurrentLimit = getEnvAsInt("CONCURRENT_IMAGES", 5)
Cfg.Images.Path = filepath.Join(pwd, "images")
// Storage config
Cfg.Storage.Endpoint = filepath.Join(pwd, "STORAGE_URL")
// Server config
Cfg.Server.Port = getEnvAsInt("PORT", 1323)
if os.Getenv("ASPNETCORE_PORT") != "" {
Cfg.Server.Port = getEnvAsInt("ASPNETCORE_PORT", 1323)
}
// Validate required config
if Cfg.Database.Password == "" {
fmt.Printf("required environment variable POSTGRES_PASSWORD not set\n")
}
})
return &Cfg
}
// Helper functions
func getEnv(key, fallback string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return fallback
}
func getEnvAsInt(key string, fallback int) int {
if strValue := getEnv(key, ""); strValue != "" {
if value, err := strconv.Atoi(strValue); err == nil {
return value
}
}
return fallback
}
func getEnvAsBool(key string, fallback bool) bool {
if strValue := getEnv(key, ""); strValue != "" {
if value, err := strconv.ParseBool(strValue); err == nil {
return value
}
}
return fallback
}