name: invariant-gateway-test-stack
services:
  traefik:
    image: traefik:v2.0
    container_name: "invariant-gateway-test-traefik"
    command:
      - --providers.docker=true
      # Enable the API handler in insecure mode,
      # which means that the Traefik API will be available directly
      # on the entry point named traefik.
      - --api.insecure=true
      # Define Traefik entry points to port [80] for http and port [443] for https.
      - --entrypoints.invariant-gateway-web-test.address=0.0.0.0:80
    networks:
      - invariant-gateway-web-test
    ports:
      - '${PORT_HTTP:-80}:80'
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.traefik-http.entrypoints=invariant-gateway-web-test"

  invariant-gateway:
    container_name: invariant-gateway-test
    build:
      context: ${GATEWAY_ROOT_PATH}
      dockerfile: ${GATEWAY_ROOT_PATH}/Dockerfile.gateway
    depends_on:
      app-api:
        condition: service_healthy
    working_dir: /srv/gateway
    env_file:
      - .env.test
    environment:
      - DEV_MODE=true
      - GUARDRAILS_FILE_PATH=${GUARDRAILS_FILE_PATH:+/srv/resources/guardrails.py}
      - ${INVARIANT_API_KEY:+INVARIANT_API_KEY=${INVARIANT_API_KEY}}
    volumes:
      - type: bind
        source: ${GATEWAY_ROOT_PATH}/gateway
        target: /srv/gateway
      - type: bind
        source: ${GUARDRAILS_FILE_PATH:-/dev/null}
        target: /srv/resources/guardrails.py
    networks:
      - invariant-gateway-web-test
    ports: []
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.invariant-gateway-api.rule=(Host(`localhost`) && PathPrefix(`/api/v1/gateway/`)) || (Host(`127.0.0.1`) && PathPrefix(`/api/v1/gateway/`))"
      - "traefik.http.routers.invariant-gateway-api.entrypoints=invariant-gateway-web-test"
      - "traefik.http.services.invariant-gateway-api.loadbalancer.server.port=8000"
      - "traefik.docker.network=invariant-gateway-web-test"
    healthcheck:
      test: curl -X GET -I http://localhost:8000/api/v1/gateway/health --fail
      interval: 1s
      timeout: 5s

  app-api:
    container_name: invariant-gateway-test-explorer-app-api
    image: ghcr.io/invariantlabs-ai/explorer/app-api:latest
    pull_policy: always
    platform: linux/amd64
    depends_on:
      database:
        condition: service_healthy
    working_dir: /srv/app
    env_file:
      - .env.test
    environment:
      - PROJECTS_DIR=/srv/projects
      - KEYCLOAK_CLIENT_ID_SECRET=local-does-not-use-keycloak
      - TZ=Europe/Berlin
      - DEV_MODE=true
      - APP_NAME=explorer-test
      - CONFIG_FILE=/config/explorer.config.yml
      - PORT_HTTP=8000
      - PORT_API=80
    networks:
      - internal
      - invariant-gateway-web-test
    volumes:
      - /tmp/invariant-gateway-test/configs/explorer.test.yml:/config/explorer.config.yml
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.invariant-test-api.rule=(Host(`localhost`) && PathPrefix(`/api/`)) || (Host(`127.0.0.1`) && PathPrefix(`/api/`))"
      - "traefik.http.routers.invariant-test-api.entrypoints=invariant-gateway-web-test"
      - "traefik.http.services.invariant-test-api.loadbalancer.server.port=8000"
      - "traefik.docker.network=invariant-gateway-web-test"
    healthcheck:
      test: curl -X GET -I http://localhost:8000/api/v1/ --fail
      interval: 1s
      timeout: 5s

  database:
    container_name: invariant-gateway-test-database
    image: postgres:16
    env_file:
      - .env.test
    networks:
      - internal
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U postgres" ]
      interval: 5s
      timeout: 5s
      retries: 5

networks:
  invariant-gateway-web-test:
    external: true
  internal:
