name: tft-miniapp

x-app: &app
  build:
    context: .
    dockerfile: Dockerfile
  image: tft-miniapp-api:production
  env_file:
    - .env.production
  restart: unless-stopped
  init: true
  networks:
    - private
  logging:
    driver: json-file
    options:
      max-size: 10m
      max-file: "5"

services:
  postgres:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - private
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
      interval: 10s
      timeout: 5s
      retries: 10
    mem_limit: 1g
    cpus: 0.75
    logging:
      driver: json-file
      options:
        max-size: 10m
        max-file: "5"

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    command:
      - redis-server
      - --appendonly
      - "yes"
      - --maxmemory
      - 256mb
      - --maxmemory-policy
      - allkeys-lru
    volumes:
      - redis_data:/data
    networks:
      - private
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 10
    mem_limit: 384m
    cpus: 0.35
    logging:
      driver: json-file
      options:
        max-size: 10m
        max-file: "5"

  migrate:
    <<: *app
    restart: "no"
    user: "0:0"
    command: ["node_modules/.bin/prisma", "migrate", "deploy"]
    depends_on:
      postgres:
        condition: service_healthy

  api:
    <<: *app
    command: ["node", "dist/src/server.js"]
    ports:
      - "127.0.0.1:3100:3100"
    depends_on:
      migrate:
        condition: service_completed_successfully
      redis:
        condition: service_healthy
    healthcheck:
      test:
        [
          "CMD",
          "node",
          "-e",
          "fetch('http://127.0.0.1:3100/health/ready').then(r=>{if(!r.ok)process.exit(1)}).catch(()=>process.exit(1))",
        ]
      interval: 15s
      timeout: 5s
      retries: 10
      start_period: 20s
    mem_limit: 640m
    cpus: 0.7

  worker:
    <<: *app
    command: ["node", "dist/src/worker.js"]
    depends_on:
      migrate:
        condition: service_completed_successfully
      redis:
        condition: service_healthy
    mem_limit: 384m
    cpus: 0.35

networks:
  private:
    driver: bridge

volumes:
  postgres_data:
  redis_data:
