Files
admin-panel/internal/config/config.go
coderabbitai[bot] 2adb7e3605 📝 Add docstrings to feat/go-rewrite
Docstrings generation was requested by @blakeridgway.

* https://github.com/RideAware/admin-panel/pull/1#issuecomment-3528008426

The following files were modified:

* `cmd/admin-panel/main.go`
* `internal/config/config.go`
* `internal/database/database.go`
* `internal/email/email.go`
* `internal/handlers/auth.go`
* `internal/handlers/newsletter.go`
* `internal/handlers/subscribers.go`
* `internal/middleware/auth.go`
2025-11-13 14:11:06 +00:00

89 lines
2.6 KiB
Go

package config
import (
"log"
"os"
"strconv"
"github.com/joho/godotenv"
)
type Config struct {
Port string
PGHost string
PGPort string
PGUser string
PGPassword string
PGDatabase string
SMTPServer string
SMTPPort int
SMTPUser string
SMTPPassword string
SenderEmail string
AdminUsername string
AdminPassword string
SecretKey string
BaseURL string
}
var Current *Config
// Load loads configuration from environment variables or a .env file and initializes the package-level Current configuration.
// It constructs a Config with sensible defaults for server, PostgreSQL, SMTP, admin credentials, secret key, and base URL.
// If SENDER_EMAIL is not set, it falls back to SMTP_USER. The created Config is assigned to Current and returned.
func Load() *Config {
if err := godotenv.Load(); err != nil {
log.Println("No .env file found, using environment variables")
}
cfg := &Config{
Port: getEnv("PORT", "5001"),
PGHost: getEnv("PG_HOST", "localhost"),
PGPort: getEnv("PG_PORT", "5432"),
PGUser: getEnv("PG_USER", "postgres"),
PGPassword: getEnv("PG_PASSWORD", ""),
PGDatabase: getEnv("PG_DATABASE", "newsletter"),
SMTPServer: getEnv("SMTP_SERVER", ""),
SMTPPort: getEnvInt("SMTP_PORT", 465),
SMTPUser: getEnv("SMTP_USER", ""),
SMTPPassword: getEnv("SMTP_PASSWORD", ""),
SenderEmail: getEnv("SENDER_EMAIL", ""),
AdminUsername: getEnv("ADMIN_USERNAME", "admin"),
AdminPassword: getEnv("ADMIN_PASSWORD", "changeme"),
SecretKey: getEnv("SECRET_KEY", "your-secret-key"),
BaseURL: getEnv("BASE_URL", "localhost:5001"),
}
if cfg.SenderEmail == "" {
cfg.SenderEmail = cfg.SMTPUser
}
Current = cfg
return cfg
}
// getEnv returns the value of the environment variable named by key, or defaultValue if that variable is not set or is empty.
// If the environment variable exists but is the empty string, defaultValue is returned.
func getEnv(key, defaultValue string) string {
value := os.Getenv(key)
if value == "" {
return defaultValue
}
return value
}
// getEnvInt retrieves the environment variable named by key and returns its integer value or defaultValue.
// If the variable is not set, it returns defaultValue. If the variable is set but cannot be parsed as an integer,
// it logs the parse error and returns defaultValue.
func getEnvInt(key string, defaultValue int) int {
value := os.Getenv(key)
if value == "" {
return defaultValue
}
intVal, err := strconv.Atoi(value)
if err != nil {
log.Printf("Invalid integer for %s: %v", key, err)
return defaultValue
}
return intVal
}