services:
  traefik:
    image: traefik:latest
    container_name: traefik
    restart: always
    command:
      - --api.insecure=true
      - --providers.docker
      - --providers.docker.exposedbydefault=false
      - --entrypoints.web.address=:80
    ports:
      - 80:80
      - 8080:8080
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      - traefik-network
    labels:
      - traefik.enable=true
      - traefik.http.routers.traefik.rule=Host(`traefik.${DOMAIN}`)
      - traefik.http.routers.traefik.entrypoints=web
      - traefik.http.services.traefik.loadbalancer.server.port=8080

  db:
    image: postgres:latest
    container_name: db
    restart: always
    environment:
      TZ: Europe/Madrid
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    volumes:
      - postgres-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 10s
      timeout: 5s
      retries: 5
    command: postgres -c listen_addresses='*'
    ports:
      - 5432
    networks:
      - traefik-network

  app:
    build: .
    container_name: app
    restart: always
    ports:
      - 3000:3000
    env_file:
      - .env
    environment:
      TZ: Europe/Madrid
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_HOST: ${POSTGRES_HOST}
      POSTGRES_PORT: ${POSTGRES_PORT}
      POSTGRES_DB: ${POSTGRES_DB}
      NODE_ENV: production
      DOMAIN: ${DOMAIN}
      AUTH_TRUST_HOST: ${AUTH_TRUST_HOST}
      AUTH_SECRET: ${AUTH_SECRET}
    depends_on:
      - db
    networks:
      - traefik-network
    labels:
      - traefik.enable=true
      - traefik.http.routers.app.rule=Host(`${DOMAIN}`)
      - traefik.http.routers.app.entrypoints=web
      - traefik.http.services.app.loadbalancer.server.port=3000

volumes:
  postgres-data:

networks:
  traefik-network:
    driver: bridge