services:
  postgres:
    image: postgres:17
    container_name: postgres
    restart: always
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    ports:
      - "5432:5432" # locally, do not push to prod
    volumes:
      - pgdata:/var/lib/postgresql/data
      - ./init-scripts/init-script.sql:/docker-entrypoint-initdb.d/init-script.sql:ro
    networks:
      - appnet

  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: backend
    restart: always
    env_file:
      - ./backend/.env
    volumes:
      - ./backend/bin:/app/bin # Mount your compiled binary
    depends_on:
      - postgres
    networks:
      - appnet

  sabredav:
    image: php:8.2-fpm
    container_name: sabredav
    restart: always
    working_dir: /var/www/dav
    environment:
      POSTGRES_HOST: postgres
      POSTGRES_PORT: 5432
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      DAV_ADMIN_PASSWORD: ${DAV_ADMIN_PASSWORD:-admin}  # Must be set in production
    volumes:
      - ./sabredav:/var/www/dav
    depends_on:
      - postgres
    networks:
      - appnet
    command: >
      bash -c " apt-get update &&  apt-get install -y git unzip libzip-dev libpq-dev &&  docker-php-ext-install pdo pdo_pgsql zip &&  curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && if [ ! -f /var/www/dav/vendor/autoload.php ]; then
        cd /var/www/dav && composer install --no-dev --optimize-autoloader;
      fi && php-fpm "

  nginx:
    image: nginx:stable
    container_name: nginx
    restart: always
    ports:
      - "80:80"
      - "443:443"
      - "9000:9000" # Rustfs S3 API
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./nginx/certs:/etc/nginx/certs
      - ./frontend/build:/usr/share/nginx/html # Serve frontend
      - ./sabredav:/var/www/dav
      - rustfs_data:/data
    depends_on:
      - backend
      - sabredav
    networks:
      - appnet

  rustfs:
    image: rustfs/rustfs:latest
    container_name: rustfs
    restart: always
    ports:
      - "9000:9000"
      - "9001:9001"
    environment:
      - RUSTFS_ADMIN_ACCESS_KEY=rustfsadmin
      - RUSTFS_ADMIN_SECRET_KEY=rustfsadmin
      - RUSTFS_ACCESS_KEY=rustfsadmin
      - RUSTFS_SECRET_KEY=rustfsadmin
      - RUSTFS_BUCKET=crussell
    volumes:
      - rustfs_data:/data
    networks:
      - appnet

volumes:
  pgdata:
  rustfs_data:


networks:
  appnet:
