mirror of
https://github.com/Dvorinka/Bookra.git
synced 2026-06-03 20:13:00 +00:00
cleanup
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
AppEnv string
|
||||
Port string
|
||||
DatabaseURL string
|
||||
FrontendURL string
|
||||
JWTSecret string
|
||||
NeonAuthURL string
|
||||
|
||||
SMTPHost string
|
||||
SMTPPort int
|
||||
SMTPUsername string
|
||||
SMTPPassword string
|
||||
EmailFrom string
|
||||
|
||||
GoogleClientID string
|
||||
GoogleClientSecret string
|
||||
GoogleRedirectURL string
|
||||
|
||||
StripeSecretKey string
|
||||
StripeWebhookSecret string
|
||||
StripePriceIDs map[string]string
|
||||
}
|
||||
|
||||
func Load() (*Config, error) {
|
||||
port := getEnv("PORT", "8081")
|
||||
|
||||
dbURL := getEnv("DATABASE_URL", "")
|
||||
if dbURL == "" {
|
||||
return nil, fmt.Errorf("DATABASE_URL is required")
|
||||
}
|
||||
|
||||
smtpPort, _ := strconv.Atoi(getEnv("SMTP_PORT", "465"))
|
||||
|
||||
return &Config{
|
||||
AppEnv: getEnv("APP_ENV", "development"),
|
||||
Port: port,
|
||||
DatabaseURL: dbURL,
|
||||
FrontendURL: getEnv("FRONTEND_URL", "http://localhost:3000"),
|
||||
JWTSecret: getEnv("JWT_SECRET", "change-me-in-production"),
|
||||
NeonAuthURL: getEnv("NEON_AUTH_URL", ""),
|
||||
|
||||
SMTPHost: getEnv("SMTP_HOST", "smtp.purelymail.com"),
|
||||
SMTPPort: smtpPort,
|
||||
SMTPUsername: getEnvAllowEmpty("SMTP_USERNAME", "noreply@tdvorak.dev"),
|
||||
SMTPPassword: getEnv("SMTP_PASSWORD", ""),
|
||||
EmailFrom: getEnv("EMAIL_FROM", "noreply@tdvorak.dev"),
|
||||
|
||||
GoogleClientID: getEnv("GOOGLE_CLIENT_ID", ""),
|
||||
GoogleClientSecret: getEnv("GOOGLE_CLIENT_SECRET", ""),
|
||||
GoogleRedirectURL: getEnv("GOOGLE_REDIRECT_URL", ""),
|
||||
|
||||
StripeSecretKey: getEnv("STRIPE_SECRET_KEY", ""),
|
||||
StripeWebhookSecret: getEnv("STRIPE_WEBHOOK_SECRET", ""),
|
||||
StripePriceIDs: map[string]string{
|
||||
"monthly": getEnv("STRIPE_PRICE_ID", ""),
|
||||
"starter": getEnv("STRIPE_STARTER_PRICE_ID", ""),
|
||||
"growth": getEnv("STRIPE_GROWTH_PRICE_ID", ""),
|
||||
"multi-location": getEnv("STRIPE_MULTI_LOCATION_PRICE_ID", ""),
|
||||
"pro": getEnv("STRIPE_PRO_PRICE_ID", ""),
|
||||
"business": getEnv("STRIPE_BUSINESS_PRICE_ID", ""),
|
||||
"starter:czk": getEnv("STRIPE_STARTER_CZK_PRICE_ID", ""),
|
||||
"starter:usd": getEnv("STRIPE_STARTER_USD_PRICE_ID", ""),
|
||||
"pro:czk": getEnv("STRIPE_PRO_CZK_PRICE_ID", ""),
|
||||
"pro:usd": getEnv("STRIPE_PRO_USD_PRICE_ID", ""),
|
||||
"business:czk": getEnv("STRIPE_BUSINESS_CZK_PRICE_ID", ""),
|
||||
"business:usd": getEnv("STRIPE_BUSINESS_USD_PRICE_ID", ""),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func getEnv(key, defaultVal string) string {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
return v
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
func getEnvAllowEmpty(key, defaultVal string) string {
|
||||
if v, ok := os.LookupEnv(key); ok {
|
||||
return v
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
func (cfg *Config) StripeSecretConfigured() bool {
|
||||
return strings.TrimSpace(cfg.StripeSecretKey) != ""
|
||||
}
|
||||
|
||||
func (cfg *Config) StripeWebhookConfigured() bool {
|
||||
return strings.TrimSpace(cfg.StripeWebhookSecret) != ""
|
||||
}
|
||||
|
||||
func (cfg *Config) StripeHasAnyPriceConfigured() bool {
|
||||
for _, priceID := range cfg.StripePriceIDs {
|
||||
if strings.TrimSpace(priceID) != "" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (cfg *Config) StripeCheckoutReady() bool {
|
||||
return cfg.StripeSecretConfigured() && cfg.StripeHasAnyPriceConfigured()
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStripeReadinessHelpers(t *testing.T) {
|
||||
cfg := &Config{
|
||||
StripeSecretKey: "sk_test_123",
|
||||
StripePriceIDs: map[string]string{
|
||||
"pro:czk": "price_123",
|
||||
},
|
||||
}
|
||||
|
||||
if !cfg.StripeSecretConfigured() {
|
||||
t.Fatal("expected secret configured")
|
||||
}
|
||||
if cfg.StripeWebhookConfigured() {
|
||||
t.Fatal("expected webhook not configured")
|
||||
}
|
||||
if !cfg.StripeHasAnyPriceConfigured() {
|
||||
t.Fatal("expected prices configured")
|
||||
}
|
||||
if !cfg.StripeCheckoutReady() {
|
||||
t.Fatal("expected checkout ready")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStripeCheckoutReadyRequiresSecretAndPrice(t *testing.T) {
|
||||
cfg := &Config{
|
||||
StripePriceIDs: map[string]string{
|
||||
"pro:czk": "price_123",
|
||||
},
|
||||
}
|
||||
if cfg.StripeCheckoutReady() {
|
||||
t.Fatal("expected checkout not ready without secret")
|
||||
}
|
||||
|
||||
cfg.StripeSecretKey = "sk_test_123"
|
||||
cfg.StripePriceIDs = map[string]string{}
|
||||
if cfg.StripeCheckoutReady() {
|
||||
t.Fatal("expected checkout not ready without price")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDefaultsAuthServicePortTo8081(t *testing.T) {
|
||||
originals := map[string]string{}
|
||||
for _, key := range []string{
|
||||
"PORT",
|
||||
"DATABASE_URL",
|
||||
} {
|
||||
originals[key] = os.Getenv(key)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
for key, value := range originals {
|
||||
if value == "" {
|
||||
_ = os.Unsetenv(key)
|
||||
continue
|
||||
}
|
||||
_ = os.Setenv(key, value)
|
||||
}
|
||||
})
|
||||
|
||||
_ = os.Unsetenv("PORT")
|
||||
_ = os.Setenv("DATABASE_URL", "postgresql://localhost/bookra")
|
||||
|
||||
cfg, err := Load()
|
||||
if err != nil {
|
||||
t.Fatalf("load config: %v", err)
|
||||
}
|
||||
if cfg.Port != "8081" {
|
||||
t.Fatalf("expected default port 8081, got %s", cfg.Port)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user