version: '2.1'

networks:
  kong-net:
  
services:

  #######################################
  # Postgres: The database used by Kong
  #######################################
  kong-database:
    image: postgres:latest
    container_name: kong-db
    environment:
      POSTGRES_DB: kong
      POSTGRES_USER: kong
      POSTGRES_PASSWORD: kong
    networks:
      - kong-net
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "kong"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: on-failure

  #######################################
  # Kong database migration
  #######################################
  kong-migrations:
    image: kong:latest
    command: kong migrations bootstrap && kong migrations up && kong migrations finish
    depends_on:
      kong-database:
        condition: service_healthy
    environment:
      KONG_DATABASE: postgres
      KONG_PG_HOST: kong-db
      KONG_PG_USER: kong
      KONG_PG_PASSWORD: kong
      KONG_PG_DATABASE: kong
    networks:
      - kong-net
    restart: on-failure

  #######################################
  # Kong: The API Gateway
  #######################################
  kong:
    image: kong:latest
    container_name: kong-api
    depends_on:
      kong-database:
        condition: service_healthy
    environment:
      KONG_DATABASE: postgres
      KONG_PG_HOST: kong-db
      KONG_PG_USER: kong
      KONG_PG_PASSWORD: kong
      KONG_PG_DATABASE: kong
      KONG_ADMIN_LISTEN: 0.0.0.0:8001
    networks:
      - kong-net
    ports:
      - "8000:8000"
      - "8001:8001"
    healthcheck:
      test: ["CMD", "kong", "health"]
      interval: 10s
      timeout: 10s
      retries: 10
    restart: on-failure
      
  #######################################
  # Example Microservice
  #######################################
  app:
    build: .
    container_name: kong-app
    networks:
      - kong-net
    restart: on-failure


  #######################################
  # User Microservice (Auth, etc.)
  #######################################
  # user:
  #   build: .
  #   container_name: user-service
  #   networks:
  #     - kong-net
  #   ports:
  #     - "3001:3000"  # Optional: for external testing