services:
  proxy:
    image: traefik:v2.11
    command: --providers.docker
    ports:
      - 80:80
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

  backend:
    build:
      context: ..
      dockerfile: SerpentRace_Docker/Dockerfile
      target: prod
    environment:
      POSTGRES_HOST: db
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB_1}
      DB_1: ${POSTGRES_DB_2}
      DB_2: ${POSTGRES_DB_3}
      DB_3: ${POSTGRES_DB_4}
    command: ["npx", "nodemon", "src/index.mjs"]
    develop:
      watch:
        - path: ../SerpentRace_Backend/src
          action: sync
          target: /usr/local/app/backend/src
        - path: ../SerpentRace_Backend/package.json
          action: rebuild
    labels:
      traefik.http.routers.backend.rule: Host(`localhost`) && PathPrefix(`/api`)
      traefik.http.services.backend.loadbalancer.server.port: 3000
    depends_on:
      - db

  frontend:
    build:
      context: ..
      dockerfile: SerpentRace_Docker/Dockerfile
      target: client-build
    command: ["npm", "run", "dev", "--", "--host", "0.0.0.0", "--port", "5173"]
    working_dir: /usr/local/app/frontend
    develop:
      watch:
        - path: ../SerpentRace_Frontend/src
          action: sync
          target: /usr/local/app/frontend/src
        - path: ../SerpentRace_Frontend/package.json
          action: rebuild
    labels:
      traefik.http.routers.frontend.rule: Host(`localhost`)
      traefik.http.services.frontend.loadbalancer.server.port: 5173

  db:
    image: postgres
    restart: always
    shm_size: 128mb
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB_1}
      DB_1: ${POSTGRES_DB_2}
      DB_2: ${POSTGRES_DB_3}
      DB_3: ${POSTGRES_DB_4}
    volumes:
      - ./init-multi-db.sh:/docker-entrypoint-initdb.d/init-multi-db.sh:ro
      - ./Default.sql:/docker-entrypoint-initdb.d/Default.sql:ro

  adminer:
    image: adminer
    restart: always
    ports:
      - "8080:8080"
    labels:
      traefik.http.routers.adminer.rule: Host(`db.localhost`)
      traefik.http.services.adminer.loadbalancer.server.port: 8080