x-defaults: &defaults
  restart: unless-stopped
  logging:
    driver: json-file
    options:
      max-size: 100m
      max-file: '3'

services:
  apisix:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}apache/apisix:${APISIX_VERSION:-3.13.0-debian}
    ports:
      - '${APISIX_HTTP_PORT_OVERRIDE:-9080}:9080'
      - '${APISIX_HTTPS_PORT_OVERRIDE:-9443}:9443'
      - '${APISIX_ADMIN_PORT_OVERRIDE:-9180}:9180'
    volumes:
      - apisix_logs:/usr/local/apisix/logs

      # Optional: Mount custom configuration
      # - ./config.yaml:/usr/local/apisix/conf/config.yaml
      # - ./apisix.yaml:/usr/local/apisix/conf/apisix.yaml
    environment:
      - TZ=${TZ:-UTC}
      - APISIX_STAND_ALONE=${APISIX_STAND_ALONE:-false}
    depends_on:
      etcd:
        condition: service_healthy
    deploy:
      resources:
        limits:
          cpus: ${APISIX_CPU_LIMIT:-1.0}
          memory: ${APISIX_MEMORY_LIMIT:-1G}
        reservations:
          cpus: ${APISIX_CPU_RESERVATION:-0.25}
          memory: ${APISIX_MEMORY_RESERVATION:-256M}
    healthcheck:
      test:
        [CMD-SHELL, 'curl -f http://localhost:9080/apisix/status || exit 1']
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 10s

  etcd:
    <<: *defaults
    image: quay.io/coreos/etcd:${ETCD_VERSION:-v3.6.0}
    ports:
      - '${ETCD_CLIENT_PORT_OVERRIDE:-2379}:2379'
    volumes:
      - etcd_data:/etcd-data
    environment:
      - TZ=${TZ:-UTC}
      - ETCD_NAME=apisix-etcd
      - ETCD_DATA_DIR=/etcd-data
      - ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
      - ETCD_ADVERTISE_CLIENT_URLS=http://etcd:2379
      - ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
      - ETCD_INITIAL_ADVERTISE_PEER_URLS=http://etcd:2380
      - ETCD_INITIAL_CLUSTER=apisix-etcd=http://etcd:2380
      - ETCD_INITIAL_CLUSTER_STATE=new
      - ETCD_INITIAL_CLUSTER_TOKEN=apisix-etcd-cluster
      - ETCD_AUTO_COMPACTION_RETENTION=1
      - ETCD_QUOTA_BACKEND_BYTES=2147483648
      - ETCD_HEARTBEAT_INTERVAL=100
      - ETCD_ELECTION_TIMEOUT=1000
      - ETCD_ENABLE_V2=false
    command:
      - etcd
      - --name=apisix-etcd
      - --data-dir=/etcd-data
      - --listen-client-urls=http://0.0.0.0:2379
      - --advertise-client-urls=http://etcd:2379
      - --listen-peer-urls=http://0.0.0.0:2380
      - --initial-advertise-peer-urls=http://etcd:2380
      - --initial-cluster=apisix-etcd=http://etcd:2380
      - --initial-cluster-state=new
      - --initial-cluster-token=apisix-etcd-cluster
      - --auto-compaction-retention=1
      - --quota-backend-bytes=2147483648
      - --heartbeat-interval=100
      - --election-timeout=1000
      - --enable-v2=false
    deploy:
      resources:
        limits:
          cpus: ${ETCD_CPU_LIMIT:-0.5}
          memory: ${ETCD_MEMORY_LIMIT:-512M}
        reservations:
          cpus: ${ETCD_CPU_RESERVATION:-0.1}
          memory: ${ETCD_MEMORY_RESERVATION:-128M}
    healthcheck:
      test: [CMD, etcdctl, endpoint, health]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 10s

  # Optional: APISIX Dashboard
  apisix-dashboard:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}apache/apisix-dashboard:${APISIX_DASHBOARD_VERSION:-3.0.1-alpine}
    ports:
      - '${APISIX_DASHBOARD_PORT_OVERRIDE:-9000}:9000'
    volumes:
      - dashboard_conf:/usr/local/apisix-dashboard/conf
    environment:
      - TZ=${TZ:-UTC}
      - APISIX_DASHBOARD_USER=${APISIX_DASHBOARD_USER:-admin}
      - APISIX_DASHBOARD_PASSWORD=${APISIX_DASHBOARD_PASSWORD:-admin}
    depends_on:
      apisix:
        condition: service_healthy
    profiles:
      - dashboard
    deploy:
      resources:
        limits:
          cpus: ${APISIX_DASHBOARD_CPU_LIMIT:-0.5}
          memory: ${APISIX_DASHBOARD_MEMORY_LIMIT:-512M}
        reservations:
          cpus: ${APISIX_DASHBOARD_CPU_RESERVATION:-0.1}
          memory: ${APISIX_DASHBOARD_MEMORY_RESERVATION:-128M}

volumes:
  apisix_logs:
  etcd_data:
  dashboard_conf:
