name: compose

services:
  api:
    build:
      context: ./apps/api
      dockerfile: Dockerfile
    container_name: api
    restart: unless-stopped
    ports:
      - "3031:3001"
    env_file:
      - ./.env
    networks: [observability]
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:3001/health || exit 1"]
      interval: 10s
      timeout: 3s
      retries: 10
    # 로그 트리거를 받을 예정이면 api가 /ingest/syslog 같은 엔드포인트를 열어두면 됨

  mcp:
    build:
      context: ./apps/mcp
      dockerfile: Dockerfile
    container_name: mcp
    restart: unless-stopped
    ports:
      - "5000:5000"
    env_file:
      - ./.env
    environment:
      - LOKI_BASE_URL=http://loki:3100
    networks: [observability]
    depends_on:
      - loki

  syslog-ng:
    image: balabit/syslog-ng:latest
    container_name: syslog-ng
    restart: unless-stopped
    # 테스트는 5514 추천(514 불필요)
    ports:
      - "5514:5514/tcp"
      - "5514:5514/udp"
    volumes:
      - ./infra/syslog/syslog-ng.conf:/etc/syslog-ng/syslog-ng.conf:ro
      - syslog_data:/var/log/syslog-ng
    command: ["-Fv", "-f", "/etc/syslog-ng/syslog-ng.conf"]
    networks: [observability]
    depends_on:
      - vector

  vector:
    image: timberio/vector:latest-alpine
    container_name: vector
    restart: unless-stopped
    command: ["--config", "/etc/vector/vector.toml"]
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./infra/vector/vector.toml:/etc/vector/vector.toml:ro
      - syslog_data:/var/log/syslog-ng:ro
    env_file:
      - ./.env
    networks: [observability]
    depends_on:
      - loki
      - api
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:8686/health || exit 1"]
      interval: 10s
      timeout: 3s
      retries: 10
    ports:
      # Vector API(디버깅/메트릭/헬스)
      - "8686:8686"

  loki:
    image: grafana/loki:3.6.5
    container_name: loki
    restart: unless-stopped
    command: -config.file=/etc/loki/config.yaml
    volumes:
      - ./infra/loki/loki-config.yaml:/etc/loki/config.yaml:ro
      - loki_data:/loki
    networks: [observability]
    ports:
      - "3100:3100"   # 테스트 중이면 열어두는 게 편함(원하면 제거 가능)
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:3100/ready || exit 1"]
      interval: 10s
      timeout: 3s
      retries: 20

  grafana:
    image: grafana/grafana:11.5.2
    container_name: grafana
    restart: unless-stopped
    ports:
      - "3030:3000"
    volumes:
      - grafana_data:/var/lib/grafana
      # provisioning은 ro 권장
      - ./infra/grafana/provisioning/datasources:/etc/grafana/provisioning/datasources:ro
      - ./infra/grafana/provisioning/dashboards:/etc/grafana/provisioning/dashboards:ro
      - ./infra/grafana/provisioning/alerting:/etc/grafana/provisioning/alerting:ro
      # dashboards 실제 JSON은 별도 경로로(중복 마운트 방지)
      - ./infra/grafana/dashboards:/var/lib/grafana/dashboards:ro
    env_file:
      - ./.env
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=${GF_SECURITY_ADMIN_PASSWORD}
      - GF_INSTALL_PLUGINS=yesoreyeram-infinity-datasource
      - GF_USERS_ALLOW_SIGN_UP=false
    networks: [observability]
    depends_on:
      - loki
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:3000/api/health || exit 1"]
      interval: 10s
      timeout: 3s
      retries: 20

networks:
  observability:
    driver: bridge
    ipam:
      config:
        - subnet: 10.250.0.0/24
          gateway: 10.250.0.1

volumes:
  syslog_data:
  loki_data:
  grafana_data:
