Sfoglia il codice sorgente

refactor environment configuration and remove unused mailer tests

lblt 1 settimana fa
parent
commit
66cd6106fa
6 ha cambiato i file con 52 aggiunte e 127 eliminazioni
  1. 2 16
      .env.sample
  2. 17 1
      Dockerfile
  3. 0 2
      README.md
  4. 8 1
      cmd/api/main.go
  5. 25 0
      docker-compose.yml
  6. 0 107
      internal/platform/mailer/mailer_test.go

+ 2 - 16
.env.sample

@@ -1,17 +1,3 @@
-SERVER_ADDRESS=:8080
-DATABASE_URL=byom.db
-OVH_ENDPOINT=ovh-eu
-OVH_APP_KEY=your_ovh_app_key
-OVH_APP_SECRET=your_ovh_app_secret
-STRIPE_KEY=sk_test_your_stripe_key
+STRIPE_SECRET_KEY=sk_test_your_stripe_key
+STRIPE_API_KEY=sk_test_your_stripe_key
 STRIPE_WEBHOOK_SECRET=whsec_your_webhook_secret
-
-# Admin credentials (change these in production)
-ADMIN_USERNAME=admin
-ADMIN_PASSWORD=admin
-
-# SMTP Configuration (if using email notifications)
-SMTP_HOST=smtp.example.com
-SMTP_PORT=587
-SMTP_USERNAME=noreply@yourdomain.com
-SMTP_PASSWORD=your_smtp_password

+ 17 - 1
Dockerfile

@@ -1,6 +1,18 @@
 # Build stage
 FROM golang:1.22.5-alpine AS builder
 
+# Build arguments
+ARG CORS_ALLOWED_ORIGIN
+ARG STRIPE_SECRET_KEY
+ARG STRIPE_API_KEY
+ARG STRIPE_WEBHOOK_SECRET
+
+# Set environment variables
+ENV CORS_ALLOWED_ORIGIN=${CORS_ALLOWED_ORIGIN}
+ENV STRIPE_SECRET_KEY=${STRIPE_SECRET_KEY}
+ENV STRIPE_API_KEY=${STRIPE_API_KEY}
+ENV STRIPE_WEBHOOK_SECRET=${STRIPE_WEBHOOK_SECRET}
+
 # Install build dependencies
 RUN apk add --no-cache git make gcc musl-dev sqlite-dev
 
@@ -40,5 +52,9 @@ USER appuser
 EXPOSE 8080
 
 ENV CONFIG_FILE=/app/config.yaml
+ENV CORS_ALLOWED_ORIGIN=${CORS_ALLOWED_ORIGIN}
+ENV STRIPE_SECRET_KEY=${STRIPE_SECRET_KEY}
+ENV STRIPE_API_KEY=${STRIPE_API_KEY}
+ENV STRIPE_WEBHOOK_SECRET=${STRIPE_WEBHOOK_SECRET}
 
-CMD ["./byom-onboard", "serve", "--config=/app/config.yaml"] 
+CMD ["./byom-onboard", "serve", "--config=/app/config.yaml"]

+ 0 - 2
README.md

@@ -8,8 +8,6 @@ Structure API incohérente avec le reste des projets
 
 Mais fonctionnelle.
 
-
-
 ## Description
 BYOM-Onboard est un service de provisionnement automatisé qui gère :
 

+ 8 - 1
cmd/api/main.go

@@ -7,6 +7,7 @@ import (
 	"net/http"
 	"os"
 	"os/signal"
+	"strings"
 	"syscall"
 	"time"
 
@@ -70,8 +71,14 @@ func (s *ServeCmd) Run(cli *CLI) error {
 	r.Use(gin.Logger())
 	r.Use(gin.Recovery())
 
+	//read cors from env
+
+	corsUrl := os.Getenv("CORS_ALLOWED_ORIGIN")
+	// Split the CORS URL by comma to handle multiple origins
+	corsOrigins := strings.Split(corsUrl, ",")
+
 	corsConfig := cors.DefaultConfig()
-	corsConfig.AllowOrigins = []string{"https://byom.moooffle.com"}
+	corsConfig.AllowOrigins = corsOrigins
 	corsConfig.AllowCredentials = true
 	corsConfig.AllowHeaders = []string{"Content-Type", "Authorization"}
 	corsConfig.AllowMethods = []string{"*"}

+ 25 - 0
docker-compose.yml

@@ -0,0 +1,25 @@
+version: '3'
+
+services:
+  api:
+    build:
+      context: .
+      args:
+        - CORS_ALLOWED_ORIGIN=http://${HOSTNAME},https://${HOSTNAME}
+    environment:
+      - CORS_ALLOWED_ORIGIN=http://${HOSTNAME},https://${HOSTNAME}
+      - STRIPE_SECRET_KEY=${STRIPE_SECRET_KEY}
+      - STRIPE_API_KEY=${STRIPE_API_KEY}
+      - STRIPE_WEBHOOK_SECRET=whsec_527fbb8ce7f9072a60a17a37e2807965c08a10fb48aa218e9ed14b7835520844
+    labels:
+      - "traefik.enable=true"
+      - "traefik.http.routers.api.rule=Host(`${HOSTNAME}`) && PathPrefix(`/api`)"
+      - "traefik.http.routers.api.entrypoints=web"
+      - "traefik.http.services.api.loadbalancer.server.port=8080"
+    networks:
+      - traefik_network
+
+networks:
+  traefik_network:
+    name: traefik_network
+    external: true

+ 0 - 107
internal/platform/mailer/mailer_test.go

@@ -1,107 +0,0 @@
-// mailer_test.go
-package mailer
-
-import (
-	"bytes"
-	"fmt"
-	"html/template"
-	"strings"
-	"testing"
-
-	"git.linuxforward.com/byom/byom-onboard/internal/platform/config"
-)
-
-func TestNewMailer(t *testing.T) {
-	cfg := &config.MailerConfig{
-		Identity: "",
-		Username: "test@example.com",
-		Password: "testpass",
-		Host:     "localhost",
-		Port:     "2525",
-	}
-
-	mailer := NewMailer(cfg)
-
-	if mailer == nil {
-		t.Error("Expected non-nil mailer")
-	}
-
-	if mailer.serverAddr != "localhost:2525" {
-		t.Errorf("Expected server address 'localhost:2525', got %s", mailer.serverAddr)
-	}
-
-	if mailer.from != "test@example.com" {
-		t.Errorf("Expected from address 'test@example.com', got %s", mailer.from)
-	}
-}
-
-func TestSendEmail(t *testing.T) {
-	// Create test data
-	data := &EmailData{
-		Username:   "testuser",
-		Password:   "testpass",
-		Subdomains: []string{"app1.example.com", "app2.example.com"},
-		WebAppURL:  "https://main.example.com",
-		SetupGuide: "1. Log in\n2. Configure settings",
-	}
-
-	// Create mailer with mock configuration
-	cfg := &config.MailerConfig{
-		Identity: "",
-		Username: "test@example.com",
-		Password: "testpass",
-		Host:     "localhost",
-		Port:     "2525",
-	}
-
-	mailer := NewMailer(cfg)
-
-	// Test email sending
-	err := mailer.SendEmail("recipient@example.com", data)
-	if err != nil {
-		t.Errorf("Expected no error, got %v", err)
-	}
-
-	// Test email content
-	// Note: In a real implementation, you might want to capture the email content
-	// using a mock SMTP server and verify its contents
-}
-
-// TestEmailTemplateRendering tests the template rendering separately
-func TestEmailTemplateRendering(t *testing.T) {
-	data := &EmailData{
-		Username:   "testuser",
-		Password:   "testpass",
-		Subdomains: []string{"app1.example.com", "app2.example.com"},
-		WebAppURL:  "https://main.example.com",
-		SetupGuide: "1. Log in\n2. Configure settings",
-	}
-
-	tmpl := template.Must(template.New("welcomeEmail").Parse(welcomeEmailTemplate))
-	var tpl bytes.Buffer
-
-	err := tmpl.Execute(&tpl, data)
-	if err != nil {
-		t.Errorf("Template execution failed: %v", err)
-	}
-
-	result := tpl.String()
-
-	fmt.Println(result)
-
-	// Verify that all required information is present in the rendered template
-	expectedContents := []string{
-		data.Username,
-		data.Password,
-		data.Subdomains[0],
-		data.Subdomains[1],
-		data.WebAppURL,
-		data.SetupGuide,
-	}
-
-	for _, expected := range expectedContents {
-		if !strings.Contains(result, expected) {
-			t.Errorf("Expected email to contain '%s'", expected)
-		}
-	}
-}