API URL was not taken from environment after docker container and npm was build

CORS prevented from loading
This commit is contained in:
2026-01-01 14:02:38 +01:00
parent 8c7aa86181
commit 7eb44a7ae6
10 changed files with 3911 additions and 7 deletions
+8
View File
@@ -26,6 +26,9 @@ type Config struct {
DevTesting string // "yes" or "no", used to determine if we are in a test environment
// Frontend URLs for CORS
FrontendURL string // Frontend URL for CORS configuration
// PDF Templates
TemplatesDir string // Directory where PDF templates are stored
ExportTempDir string // Directory for temporary PDF exports
@@ -50,6 +53,7 @@ func defaultConfig() *Config {
LogLevel: "INFO",
Environment: "production",
DevTesting: "no", // Default to "no", can be overridden in tests
FrontendURL: "http://localhost:5173", // Default frontend URL for development
TemplatesDir: "./templates", // Default templates directory
ExportTempDir: "./xporttemp", // Default export temp directory
}
@@ -117,6 +121,10 @@ func LoadConfig() *Config {
fmt.Printf("DEBUG LoadConfig - DEVTESTING nicht gefunden, setze DevTesting auf 'no'\n")
}
// Frontend URL
if frontendURL := os.Getenv("BASE_URL"); frontendURL != "" {
config.FrontendURL = frontendURL
// Templates Directory
if templatesDir := os.Getenv("TEMPLATES_DIR"); templatesDir != "" {
config.TemplatesDir = templatesDir
+11 -1
View File
@@ -1,18 +1,28 @@
package router
import (
"bamort/config"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func SetupGin(r *gin.Engine) {
// Build allowed origins list from configuration
allowedOrigins := []string{
config.Cfg.FrontendURL,
"http://localhost:5173", // Development frontend
"https://bamort.trokan.de", // Production frontend
}
// Add CORS middleware
r.Use(cors.New(cors.Config{
//AllowOrigins: []string{"http://localhost:3000"}, // Replace with your frontend's URL
AllowOrigins: []string{"*"},
AllowOrigins: allowedOrigins,
AllowMethods: []string{"GET", "POST", "PUT", "DELETE"},
AllowHeaders: []string{"Origin", "Content-Type", "Authorization"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
MaxAge: 12 * 3600, // Cache preflight for 12 hours
}))
}
+10
View File
@@ -1,6 +1,16 @@
# =========== 1) Build stage ===========
FROM node:22-alpine AS build
# Accept build arguments for Vite environment variables
ARG VITE_API_URL
ARG VITE_BASE_URL
ARG VITE_API_PORT
# Set them as environment variables for the build process
ENV VITE_API_URL=$VITE_API_URL
ENV VITE_BASE_URL=$VITE_BASE_URL
ENV VITE_API_PORT=$VITE_API_PORT
WORKDIR /usr/src/app
# Copy package files
+6 -2
View File
@@ -24,13 +24,17 @@ services:
build:
context: ../frontend
dockerfile: ../docker/Dockerfile.frontend
args:
VITE_API_URL: ${API_URL:-https://bamort.trokan.de:8180}
VITE_BASE_URL: ${BASE_URL:-https://bamort.trokan.de}
VITE_API_PORT: ${API_PORT:-8180}
container_name: bamort-frontend
ports:
- "8181:80"
environment:
- NODE_ENV=production
- VITE_API_URL=${API_URL:-http://bamort.trokan.de:8180}
- VITE_BASE_URL=${BASE_URL:-http://bamort.trokan.de}
- VITE_API_URL=${API_URL:-https://bamort.trokan.de:8180}
- VITE_BASE_URL=${BASE_URL:-https://bamort.trokan.de}
- VITE_API_PORT=${API_PORT:-8180}
depends_on:
- backend
File diff suppressed because one or more lines are too long
+3 -3
View File
@@ -1,6 +1,6 @@
#!/bin/bash
echo "🚀 Starting Bamort Development Environment..."
echo "🚀 Starting Bamort Production Environment..."
# Prüfe ob Docker läuft
if ! docker info > /dev/null 2>&1; then
@@ -11,7 +11,7 @@ fi
# Gehe ins Docker-Verzeichnis
cd "$(dirname "$0")"
echo "📦 Building and starting development containers..."
echo "📦 Building and starting production containers..."
# Stoppe vorhandene Container
docker-compose -f docker-compose.yml down
@@ -19,4 +19,4 @@ docker-compose -f docker-compose.yml down
# Baue und starte die Container
docker-compose -f docker-compose.yml up --build -d
echo "✅ Development environment started."
echo "✅ Production environment started."
+1 -1
View File
@@ -1,7 +1,7 @@
import axios from 'axios'
const API = axios.create({
baseURL: import.meta.env.VITE_API_URL || 'http://localhost:8180', // Use env variable with fallback
baseURL: import.meta.env.VITE_API_URL || 'https://bamort.trokan.de:8180', // Use env variable with fallback
})
// Request interceptor to add auth token