services:
  ipfs:
    container_name: ipfs
    image: ipfs/kubo:release
    ports:
      - "4001:4001"  # swarm
      - "8080:8080"  # gateway (IP mode only)
      - "127.0.0.1:5001:5001"  # api access from localhost
    volumes:
      - ./data/ipfs:/data/ipfs
      - ./ipfs.config.json:/data/ipfs/config:ro

  cluster:
    container_name: cluster
    image: ipfs/ipfs-cluster:${IPFS_CLUSTER_VERSION:-latest}
    depends_on:
      - ipfs
    environment:
      CLUSTER_PEERNAME: ${PEERNAME}
      CLUSTER_IPFSHTTP_NODEMULTIADDRESS: /dns4/ipfs/tcp/5001
      CLUSTER_IPFSHTTP_HTTPLISTENMULTIADDRESS: /ip4/ipfs/tcp/5001
      CLUSTER_IPFSPROXY_NODEMULTIADDRESS: /dns4/ipfs/tcp/5001
      CLUSTER_IPFSPROXY_LISTENMULTIADDRESS: /ip4/0.0.0.0/tcp/9095
      CLUSTER_RESTAPI_HTTPLISTENMULTIADDRESS: /ip4/0.0.0.0/tcp/9094
      CLUSTER_CRDT_TRUSTEDPEERS: '*'
      CLUSTER_SECRET: ${SECRET}
      CLUSTER_PEERADDRESSES: ${PEERADDRESSES}
    ports:
      - "127.0.0.1:9094:9094"  # Only accessible from localhost
      - "127.0.0.1:9095:9095"  # Only accessible from localhost
      - "9096:9096"
    volumes:
      - ./data/ipfs-cluster:/data/ipfs-cluster
      - ./service.json:/data/ipfs-cluster/service.json
      - ./identity.json:/data/ipfs-cluster/identity.json
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.cluster.rule=Path(`/api/v0/add`) || Path(`/api/v0/cat`) || Path(`/api/v0/pin/ls`) || Path(`/api/v0/pin/add`)"
      - "traefik.http.routers.cluster.entrypoints=web"
      - "traefik.http.routers.cluster.middlewares=upload-auth@docker,connection-headers@docker"
      - "traefik.http.services.cluster.loadbalancer.server.port=9095"
      - "traefik.http.middlewares.connection-headers.headers.customresponseheaders.Connection=close"

  traefik:
    image: traefik:v2.10
    command:
      - "--api=true"
      - "--api.dashboard=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:5555"
      - "--log.level=INFO"
    ports:
      - "5555:5555"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./auth/admin-htpasswd:/etc/traefik/admin-htpasswd:ro
      - ./auth/user-htpasswd:/etc/traefik/user-htpasswd:ro
    labels:
      - "traefik.enable=true"
      # Dashboard specific config
      - "traefik.http.routers.api.rule=Host(`${TRAEFIK_DOMAIN:-traefik.localhost}`)"
      - "traefik.http.routers.api.service=api@internal"
      - "traefik.http.routers.api.entrypoints=web"
      - "traefik.http.routers.api.middlewares=dashboard-auth"
      - "traefik.http.middlewares.dashboard-auth.basicauth.usersfile=/etc/traefik/admin-htpasswd"
      # Upload auth middleware
      - "traefik.http.middlewares.upload-auth.basicauth.usersfile=/etc/traefik/user-htpasswd"