logging, environment und Datenbank connection

This commit is contained in:
2025-08-11 07:46:53 +02:00
parent 8874084a07
commit 4df7376e33
9 changed files with 315 additions and 71 deletions
+47 -5
View File
@@ -2,6 +2,7 @@ package config
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
@@ -23,7 +24,16 @@ type Config struct {
// Environment
Environment string
Testing string // "yes" or "no", used to determine if we are in a test environment
DevTesting string // "yes" or "no", used to determine if we are in a test environment
}
// Cfg ist die globale Konfigurationsvariable
// Sie wird beim Programmstart automatisch geladen
var Cfg *Config
// init lädt die Konfiguration einmal beim Programmstart
func init() {
Cfg = LoadConfig()
}
// defaultConfig gibt die Standard-Konfiguration zurück
@@ -35,7 +45,7 @@ func defaultConfig() *Config {
DebugMode: false,
LogLevel: "INFO",
Environment: "production",
Testing: "no", // Default to "no", can be overridden in tests
DevTesting: "no", // Default to "no", can be overridden in tests
}
}
@@ -46,6 +56,11 @@ func LoadConfig() *Config {
config := defaultConfig()
// Debug: Zeige geladene Umgebungsvariablen
fmt.Printf("DEBUG LoadConfig - ENVIRONMENT aus ENV: '%s'\n", os.Getenv("ENVIRONMENT"))
fmt.Printf("DEBUG LoadConfig - TESTING aus ENV: '%s'\n", os.Getenv("DEVTESTING"))
fmt.Printf("DEBUG LoadConfig - DATABASE_TYPE aus ENV: '%s'\n", os.Getenv("DATABASE_TYPE"))
// Server Port
if port := os.Getenv("PORT"); port != "" {
config.ServerPort = port
@@ -88,12 +103,17 @@ func LoadConfig() *Config {
}
}
// Testing in Development
if testing := os.Getenv("TESTING"); testing != "" {
config.Testing = strings.ToLower(testing)
if testing := os.Getenv("DEVTESTING"); testing != "" {
config.DevTesting = strings.ToLower(testing)
fmt.Printf("DEBUG LoadConfig - DEVTESTING gefunden: '%s' -> DevTesting: '%s'\n", testing, config.DevTesting)
} else {
config.Testing = "no" // Default to "no"
config.DevTesting = "no" // Default to "no"
fmt.Printf("DEBUG LoadConfig - DEVTESTING nicht gefunden, setze DevTesting auf 'no'\n")
}
fmt.Printf("DEBUG LoadConfig - Finale Config: Environment='%s', DevTesting='%s', DatabaseType='%s'\n",
config.Environment, config.DevTesting, config.DatabaseType)
return config
}
@@ -103,21 +123,28 @@ func loadEnvFile() {
for _, envFile := range envFiles {
if _, err := os.Stat(envFile); err == nil {
fmt.Printf("DEBUG loadEnvFile - Lade .env-Datei: %s\n", envFile)
loadEnvFileContent(envFile)
} else {
fmt.Printf("DEBUG loadEnvFile - .env-Datei nicht gefunden: %s\n", envFile)
}
}
}
// loadEnvFileContent lädt den Inhalt einer .env-Datei
func loadEnvFileContent(filename string) {
fmt.Printf("DEBUG loadEnvFileContent - Öffne Datei: %s\n", filename)
file, err := os.Open(filename)
if err != nil {
fmt.Printf("DEBUG loadEnvFileContent - Fehler beim Öffnen von %s: %v\n", filename, err)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
lineNum := 0
for scanner.Scan() {
lineNum++
line := strings.TrimSpace(scanner.Text())
// Überspringe leere Zeilen und Kommentare
@@ -134,14 +161,29 @@ func loadEnvFileContent(filename string) {
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
// Behandle Kommentare am Ende der Zeile (nur wenn nicht in Anführungszeichen)
if !strings.HasPrefix(value, `"`) && !strings.HasPrefix(value, `'`) {
// Suche nach Kommentar am Ende der Zeile
if commentPos := strings.Index(value, "#"); commentPos > 0 {
// Entferne Kommentar und Leerzeichen davor
value = strings.TrimSpace(value[:commentPos])
}
}
// Entferne Anführungszeichen falls vorhanden
value = strings.Trim(value, `"'`)
fmt.Printf("DEBUG loadEnvFileContent - Zeile %d: %s='%s' (nach Kommentar-Behandlung)\n", lineNum, key, value)
// Setze die Umgebungsvariable nur, wenn sie noch nicht gesetzt ist
if os.Getenv(key) == "" {
os.Setenv(key, value)
fmt.Printf("DEBUG loadEnvFileContent - Setze ENV %s='%s'\n", key, value)
} else {
fmt.Printf("DEBUG loadEnvFileContent - ENV %s bereits gesetzt, überspringe\n", key)
}
}
fmt.Printf("DEBUG loadEnvFileContent - Datei %s vollständig verarbeitet (%d Zeilen)\n", filename, lineNum)
}
// IsDevelopment prüft, ob die Anwendung im Development-Modus läuft