## this is a docker compose file used to test all scenarios
## do not use it in production
x-shared:
  speedtest-service: &speedtest-service
    environment:
      - PASSWORD=gimmeTheStats1337
      - GDPR_EMAIL=test@example.com
      - ENABLE_ID_OBFUSCATION=true
      - MODE=standalone
      - REDACT_IP_ADDRESSES=true
      - TELEMETRY=true

services:
  ###################### POSTGRESQL ################################
  pg:
    image: postgres:alpine
    environment:
      POSTGRES_PASSWORD: Not4SecureProduction
      POSTGRES_USER: librespeed
    volumes:
      # mount init script from source to create database tables
      - ../../results/telemetry_postgresql.sql:/docker-entrypoint-initdb.d/01-init.sql

  speedtest-debian-pg:
    build:
      context: ../..
      dockerfile: Dockerfile
    environment: &env_vars_pg
      - PASSWORD=gimmeTheStats1337
      - DB_HOSTNAME=pg
      - DB_NAME=librespeed
      - DB_PASSWORD=Not4SecureProduction
      - DB_TYPE=postgresql
      - DB_USERNAME=librespeed
      - GDPR_EMAIL=test@example.com
      - ENABLE_ID_OBFUSCATION=true
      - MODE=standalone
      - REDACT_IP_ADDRESSES=true
      - TELEMETRY=true
    ports:
      - 9123:8080

  speedtest-alpine-pg:
    build:
      context: ../..
      dockerfile: Dockerfile.alpine
    environment: *env_vars_pg
    ports:
      - 9124:8080

  ####################### MYSQL ##############################

  mysql:
    image: mysql:lts
    environment:
      MYSQL_ROOT_PASSWORD: Not4SecureProduction
      MYSQL_USER: librespeed
      MYSQL_PASSWORD: Not4SecureProduction
      MYSQL_DATABASE: librespeed
    volumes:
      # mount init script from source to create database tables
      - ../../results/telemetry_mysql.sql:/docker-entrypoint-initdb.d/01-init.sql

  speedtest-debian-mysql:
    # check at http://localhost:9125/results/sanitycheck.php
    build:
      context: ../..
      dockerfile: Dockerfile
    environment: &env_vars_mysql
      - PASSWORD=gimmeTheStats1337
      - DB_HOSTNAME=mysql
      - DB_NAME=librespeed
      - DB_PASSWORD=Not4SecureProduction
      - DB_TYPE=mysql
      - DB_USERNAME=librespeed
      - GDPR_EMAIL=test@example.com
      - ENABLE_ID_OBFUSCATION=true
      - MODE=standalone
      - REDACT_IP_ADDRESSES=true
      - TELEMETRY=true
    ports:
      - 9125:8080
  speedtest-alpine-mysql:
    build:
      context: ../..
      dockerfile: Dockerfile.alpine
    environment: *env_vars_mysql
    ports:
      - 9126:8080

  ####################### MSSQL ##############################

  mssql:
    image: mcr.microsoft.com/mssql/server:2022-latest
    environment:
      - ACCEPT_EULA=Y
      - SA_PASSWORD=Not4SecureProduction1337
      - MSSQL_PID=Express
    healthcheck:
      test:
        ["CMD-SHELL", "/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P $$SA_PASSWORD -C -Q 'SELECT 1' || exit 1"]
      interval: 10s
      retries: 5
      start_period: 30s

  mssql-init:
    image: mcr.microsoft.com/mssql/server:2022-latest
    depends_on:
      mssql:
        condition: service_healthy
    environment:
      - SA_PASSWORD=Not4SecureProduction1337
    volumes:
      - ../../results/telemetry_mssql.sql:/docker-entrypoint-initdb.d/01-init.sql:ro
    command: >
      bash -c '
        echo "Waiting for MSSQL to be ready...";
        until /opt/mssql-tools18/bin/sqlcmd -S mssql -U sa -P "$$SA_PASSWORD" -C -Q "SELECT 1"; do
          sleep 1;
        done;
        echo "Running init scripts...";
        for f in /docker-entrypoint-initdb.d/*.sql; do
          echo "Executing $$f...";
          /opt/mssql-tools18/bin/sqlcmd -S mssql -U sa -P "$$SA_PASSWORD" -C -i "$$f";
        done;
        echo "Initialization complete!";
      '

  speedtest-debian-mssql:
    build:
      context: ../..
      dockerfile: Dockerfile
    environment: &env_vars_mssql
      - PASSWORD=gimmeTheStats1337
      - DB_HOSTNAME=mssql
      - DB_NAME=librespeed
      - DB_PASSWORD=Not4SecureProduction1337
      - DB_TYPE=mssql
      - DB_USERNAME=sa
      - GDPR_EMAIL=test@example.com
      - ENABLE_ID_OBFUSCATION=true
      - MODE=standalone
      - REDACT_IP_ADDRESSES=true
      - TELEMETRY=true
    ports:
      - 9130:8080

  speedtest-alpine-mssql:
    build:
      context: ../..
      dockerfile: Dockerfile.alpine
    environment: *env_vars_mssql
    ports:
      - 9131:8080

  ################ SQLITE ####################################

  speedtest-debian-sqlite:
    # check at http://localhost:9125/results/sanitycheck.php
    build:
      context: ../..
      dockerfile: Dockerfile
    environment: &env_vars_sqlite
      - PASSWORD=gimmeTheStats1337
      - GDPR_EMAIL=test@example.com
      - ENABLE_ID_OBFUSCATION=true
      - MODE=standalone
      - REDACT_IP_ADDRESSES=true
      - TELEMETRY=true
    ports:
      - 9127:8080

  speedtest-alpine-sqlite:
    build:
      context: ../..
      dockerfile: Dockerfile.alpine
    environment: *env_vars_sqlite
    ports:
      - 9128:8080

  speedtest-alpine-sqlite-dual:
    <<: *speedtest-service
    build:
      context: ../..
      dockerfile: Dockerfile.alpine
    environment:
      - MODE=dual

    volumes:
      - ./servers.json:/servers.json:ro
    ports:
      - 9129:8080

  ##############  TEST CONTAINER  ###############################################################
  test-container:
    image: alpine
    depends_on:
      - pg
      - mysql
      - mssql-init
      - speedtest-alpine-mysql
      - speedtest-alpine-pg
      - speedtest-alpine-mssql
    volumes:
      - ./test-script.sh:/test-script.sh
    command:
      - sh
      - /test-script.sh
