version: "3.9"

services:
  crowdlending-backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: crowdlending-backend
    restart: unless-stopped
    env_file:
      - .env
    environment:
      NODE_ENV: production
      PORT: 4000
      DB_PATH: /app/data/crowdlending.db
      UPLOAD_DIR: /app/uploads
      DATA_DIR: /app/data
    volumes:
      - ../../volumes/crowdlending/data:/app/data
      - ../../volumes/crowdlending/uploads:/app/uploads
    healthcheck:
      test: ["CMD", "node", "-e", "fetch('http://localhost:4000/api/health').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 10s
    networks:
      - internal
 
  crowdlending-frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
      args:
        VITE_API_URL: /api
    container_name: crowdlending-frontend
    restart: unless-stopped
    depends_on:
      crowdlending-backend:
        condition: service_healthy
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.crowdlending.rule=Host(`crowdlending.croguennec.net`)"
      - "traefik.http.routers.crowdlending.entrypoints=websecure"
      - "traefik.http.routers.crowdlending.tls.certresolver=le"
      - "traefik.http.routers.crowdlending.middlewares=ipwhitelist-all@file"
      - "traefik.http.services.crowdlending.loadbalancer.server.port=80"
    networks:
      - internal
      - backend    # réseau Traefik
 
networks:
  internal:        # communication interne backend <-> frontend
    driver: bridge
  backend:         # réseau partagé avec Traefik
    external: true