71 lines
1.4 KiB
Go
71 lines
1.4 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
type ApplicationConfig struct {
|
|
Port int
|
|
DatabaseURL string
|
|
UploadDir string
|
|
MaxFileSizeMB int64
|
|
OcrLang string
|
|
OcrWorkers int
|
|
AuthSecret string
|
|
AdminUsername string
|
|
AdminPassword string
|
|
}
|
|
|
|
func LoadApplicationConfig() (*ApplicationConfig, error) {
|
|
|
|
// .env optionnel — les défauts suffisent pour le dev local.
|
|
_ = godotenv.Load()
|
|
|
|
port, err := getInt("PORT", 8080)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
maxSize, err := getInt("MAX_FILE_SIZE_MB", 50)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ocrWorkers, err := getInt("OCR_WORKERS", 2)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &ApplicationConfig{
|
|
Port: port,
|
|
DatabaseURL: get("DATABASE_URL", "postgres://kazier:kazier@localhost:5432/kazier_dev?sslmode=disable"),
|
|
UploadDir: get("UPLOAD_DIR", "./uploads"),
|
|
MaxFileSizeMB: int64(maxSize),
|
|
OcrLang: get("OCR_LANG", "fra+eng"),
|
|
OcrWorkers: ocrWorkers,
|
|
AuthSecret: get("AUTH_SECRET", "dev-secret-change-me"),
|
|
AdminUsername: get("ADMIN_USERNAME", ""),
|
|
AdminPassword: get("ADMIN_PASSWORD", ""),
|
|
}, nil
|
|
|
|
}
|
|
|
|
func get(name string, defaultValue string) string {
|
|
value := os.Getenv(name)
|
|
if value == "" {
|
|
return defaultValue
|
|
}
|
|
return value
|
|
}
|
|
|
|
func getInt(name string, defaultValue int) (int, error) {
|
|
value := os.Getenv(name)
|
|
if value == "" {
|
|
return defaultValue, nil
|
|
}
|
|
return strconv.Atoi(value)
|
|
}
|