services:
  db:
    build:
      context: ./apps/db
      dockerfile: Dockerfile
      args:
        POSTGRES_USER: ${POSTGRES_USER}
        POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
        POSTGRES_DB: ${POSTGRES_DB}
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    restart: always
    networks:
      - dokploy-network
    expose:
      - "5432"
    volumes:
      - dbdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}"]
      interval: 1s
      timeout: 5s
      retries: 10

  minio:
    image: minio/minio
    expose:
      - "9000"
      - "9001"
    volumes:
      - minio_storage:/data
    networks:
      - dokploy-network
    environment:
      MINIO_ROOT_USER: ${MINIO_ROOT_USER}
      MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD}
    command: server --console-address ":9001" /data
    healthcheck:
      test: [ "CMD", "curl", "-f", "http://minio:9000/minio/health/live" ]
      interval: 10s
      timeout: 10s
      retries: 5

  tiles:
    image: eqalpha/keydb:latest
    volumes:
      - tiles-data:/data
    networks:
      - dokploy-network
    expose:
      - "6379"
    command: keydb-server /etc/keydb/keydb.conf --maxmemory 100mb --maxmemory-policy volatile-lru --maxmemory-samples 5
    healthcheck:
      test: ["CMD", "keydb-cli", "-h", "localhost", "ping"]
      interval: 5s
      timeout: 3s
      retries: 5
    restart: unless-stopped
    deploy:
      resources:
        limits:
          cpus: "0.5"
  # sysctls:
  #   vm.overcommit_memory: "1"
  
  keydb:
    image: eqalpha/keydb:latest
    volumes:
      - keydb-data:/data
    networks:
      - dokploy-network
    expose:
      - "6379"
    healthcheck:
      test: ["CMD", "keydb-cli", "-h", "localhost", "ping"]
      interval: 5s
      timeout: 3s
      retries: 5
    restart: unless-stopped
    deploy:
      resources:
        limits:
          cpus: "0.5"
  # sysctls:
  #   vm.overcommit_memory: "1"

  backend:
    build:
      context: ./apps/backend
      dockerfile: Dockerfile
    networks:
      - dokploy-network
    expose:
      - 1323
    restart: unless-stopped
    volumes:
      - images_volume:/app/images
      - storage_volume:/app/storage
    environment:
      STORAGE_STRATEGY: "minio"
      MINIO_URL: minio:9000
      MINIO_ROOT_USER: ${MINIO_ROOT_USER}
      MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
      PGHOST: db
      PGPORT: "5432"
      IMAGEOPTION: "true"
      CONCURRENT_IMAGES: ${CONCURRENT_IMAGES}
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:1323/"]
      interval: 10s
      timeout: 5s
      retries: 5
    depends_on:
      minio:
        condition: service_healthy
      db:
        condition: service_healthy
      keydb:
        condition: service_healthy
      tiles:
        condition: service_healthy

  web:
    platform: "linux/amd64"
    build:
      context: ./apps/infoalloggi
      dockerfile: Dockerfile
      args:
        INTERNAL_BASE_URL: http://web:3000
        POSTGRES_USER: ${POSTGRES_USER}
        POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
        POSTGRES_DB: ${POSTGRES_DB}
        PGHOST: db
        PGPORT: "5432"
        BACKENDSERVER_URL: http://backend:1323
        TILES_URL: tiles:6379
        KEYDB_URL: keydb:6379
        BASE_URL: ${BASE_URL}
        SENDGRID_API_KEY: ${SENDGRID_API_KEY}
        STRIPE_SECRET_KEY: ${STRIPE_SECRET_KEY}
        NEXT_PUBLIC_STRIPE_PUBLIC_KEY: ${NEXT_PUBLIC_STRIPE_PUBLIC_KEY}
        STRIPE_LIMITED_KEY: ${STRIPE_LIMITED_KEY}
        STRIPE_WEBHOOK_SECRET: ${STRIPE_WEBHOOK_SECRET}
        FIC_CLIENT_ID: ${FIC_CLIENT_ID}
        FIC_ACCESS_TOKEN: ${FIC_ACCESS_TOKEN}
        FIC_COMPANY_ID: ${FIC_COMPANY_ID}
        ARUBA_USER: ${ARUBA_USER}
        ARUBA_PASS: ${ARUBA_PASS}
        EXP_API_USER: ${EXP_API_USER}
        EXP_API_PASS: ${EXP_API_PASS}
        JWT_SECRET: ${JWT_SECRET}
    working_dir: /app
    environment:
      INTERNAL_BASE_URL: http://web:3000
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
      PGHOST: db
      PGPORT: "5432"
      BACKENDSERVER_URL: http://backend:1323
      KEYDB_URL: keydb:6379
      TILES_URL: tiles:6379
      BASE_URL: ${BASE_URL}
      SENDGRID_API_KEY: ${SENDGRID_API_KEY}
      STRIPE_SECRET_KEY: ${STRIPE_SECRET_KEY}
      NEXT_PUBLIC_STRIPE_PUBLIC_KEY: ${NEXT_PUBLIC_STRIPE_PUBLIC_KEY}
      STRIPE_LIMITED_KEY: ${STRIPE_LIMITED_KEY}
      STRIPE_WEBHOOK_SECRET: ${STRIPE_WEBHOOK_SECRET}
      FIC_CLIENT_ID: ${FIC_CLIENT_ID}
      FIC_ACCESS_TOKEN: ${FIC_ACCESS_TOKEN}
      FIC_COMPANY_ID: ${FIC_COMPANY_ID}
      ARUBA_USER: ${ARUBA_USER}
      ARUBA_PASS: ${ARUBA_PASS}
      EXP_API_USER: ${EXP_API_USER}
      EXP_API_PASS: ${EXP_API_PASS}
      JWT_SECRET: ${JWT_SECRET}
    networks:
      - dokploy-network
    ports:
      - "3000"
    depends_on:
      backend:
        condition: service_healthy
      db:
        condition: service_healthy
      keydb:
        condition: service_healthy
      tiles:
        condition: service_healthy

  

networks:
  dokploy-network:
    external: true
      
volumes:
  minio_storage:
  images_volume:
  storage_volume:
  tiles-data:
  keydb-data:
  dbdata: