Add environment files

This commit is contained in:
Achim Rohn
2025-09-01 01:05:34 +02:00
parent dae891a610
commit 5f0a1872a5
2 changed files with 182 additions and 0 deletions
+102
View File
@@ -0,0 +1,102 @@
package env
import (
. "ersteller-lib"
"os"
"github.com/joho/godotenv"
)
const (
EnvKeyRepositoryType = "REPOSITORY_TYPE"
EnvKeyDatabaseURL = "DATABASE_URL"
EnvKeySessionSecret = "SESSION_SECRET"
EnvKeyBaseURL = "BASE_URL"
EnvKeyIsLocal = "IS_LOCAL"
EnvKeyIsDev = "IS_DEV"
EnvKeyGoogleClientID = "GOOGLE_CLIENT_ID"
EnvKeyGoogleClientSecret = "GOOGLE_CLIENT_SECRET"
EnvKeyGoogleRedirectURL = "GOOGLE_REDIRECT_URL"
EnvKeyKeycloakDiscoveryURL = "KEYCLOAK_DISCOVERY_URL"
EnvKeyKeycloakClientID = "KEYCLOAK_CLIENT_ID"
EnvKeyKeycloakClientSecret = "KEYCLOAK_CLIENT_SECRET"
)
type UrlApiKey struct {
Url string
ApiKey string
}
type Environment struct {
DatabaseUrl string
MistralApiKey string
IsDev bool
RepositoryType string
GoogleSheetsCredPath string
GoogleSheetsSpreadId string
GoogleSheetsSheetName string
GoogleClientId string
GoogleClientSecret string
GoogleRedirectUrl string
IsLocal bool
SessionSecret string
ApifyKey string
Keycloak KeycloakConfig
BaseUrl string
}
type KeycloakConfig struct {
DiscoveryUrl string
CLientId string
ClientSecret string
}
func LoadEnvironment() Environment {
err := godotenv.Load()
if err != nil {
LogError("Error loading .env file: %v", err)
panic(err)
}
// Default repository type is postgres if not specified
repoType := os.Getenv(EnvKeyRepositoryType)
if repoType == "" {
repoType = "postgres"
}
return Environment{
DatabaseUrl: os.Getenv(EnvKeyDatabaseURL),
IsDev: os.Getenv(EnvKeyIsDev) == "true" || os.Getenv(EnvKeyIsLocal) == "true",
RepositoryType: repoType,
GoogleClientId: os.Getenv(EnvKeyGoogleClientID),
GoogleClientSecret: os.Getenv(EnvKeyGoogleClientSecret),
GoogleRedirectUrl: os.Getenv(EnvKeyGoogleRedirectURL),
IsLocal: os.Getenv(EnvKeyIsLocal) == "true",
SessionSecret: os.Getenv(EnvKeySessionSecret),
BaseUrl: os.Getenv(EnvKeyBaseURL),
Keycloak: KeycloakConfig{
DiscoveryUrl: os.Getenv(EnvKeyKeycloakDiscoveryURL),
CLientId: os.Getenv(EnvKeyKeycloakClientID),
ClientSecret: os.Getenv(EnvKeyKeycloakClientSecret),
},
}
}
// EnvKeys returns all environment variable names used by the application configuration.
// This allows other packages (e.g., CLI tools) to discover the required keys
// without duplicating or hard-coding the list elsewhere.
func EnvKeys() []string {
return []string{
EnvKeyRepositoryType,
EnvKeyDatabaseURL,
EnvKeySessionSecret,
EnvKeyBaseURL,
EnvKeyIsLocal,
EnvKeyIsDev,
EnvKeyGoogleClientID,
EnvKeyGoogleClientSecret,
EnvKeyGoogleRedirectURL,
EnvKeyKeycloakDiscoveryURL,
EnvKeyKeycloakClientID,
EnvKeyKeycloakClientSecret,
}
}