version: '3.8'

services:
  relay-server:
    build:
      context: ./server
      dockerfile: Dockerfile
    env_file:
      - server/.env
    environment:
      SERVER_PORT: ${SERVER_PORT}
      JWT_SECRET: ${JWT_SECRET}
      ORIGIN: ${ORIGIN}
      PG_USER: ${PG_USER}
      PG_PASSWORD: ${PG_PASSWORD}
      PG_DATABASE: ${PG_DATABASE}
      PG_HOST: db
    volumes:
      - attachments:/usr/src/app/attachments
    ports:
      - "3000"
    depends_on:
      db:
        condition: service_healthy
    networks:
      - relay-network

  relay-client:
    build:
      context: ./client
      dockerfile: Dockerfile
    ports:
      - ${CLIENT_PORT}:80
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - relay-server
    networks:
      - relay-network

  db:
    image: postgres:latest
    restart: always
    user: postgres
    env_file:
      - server/.env
    volumes:
      - postgres-data:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: ${PG_USER}
      POSTGRES_DB: ${PG_DATABASE}
      POSTGRES_PASSWORD: ${PG_PASSWORD}
    expose:
      - 5432
    healthcheck:
      test: [ "CMD", "pg_isready", "-U", "${PG_USER}" ]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - relay-network

networks:
  relay-network:
    driver: bridge

volumes:
  postgres-data:
  attachments: