services:
  n8n:
    image: ${N8N_IMAGE:-docker.n8n.io/n8nio/n8n}
    container_name: ${N8N_COMPOSE_PROJECT_NAME}_app
    restart: unless-stopped
    ports:
      - "${N8N_PORT:-5678}:5678"
    volumes:
      - n8n_data:/home/node/.n8n
    environment:
      TZ: ${TIMEZONE:-Europe/Berlin}
      GENERIC_TIMEZONE: ${TIMEZONE:-Europe/Berlin}
      N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS: "true"
      N8N_RUNNERS_ENABLED: "true"
      DB_TYPE: postgresdb
      DB_POSTGRESDB_DATABASE: ${N8N_DB_NAME}
      DB_POSTGRESDB_HOST: ${CORE_DB_HOST}
      DB_POSTGRESDB_PORT: ${CORE_DB_PORT}
      DB_POSTGRESDB_USER: ${DB_USER}
      DB_POSTGRESDB_PASSWORD: ${DB_PASSWORD}
      DB_POSTGRESDB_SCHEMA: ${N8N_DB_SCHEMA:-public}
      N8N_HOST: ${N8N_TRAEFIK_HOST}
      N8N_PORT: 5678
      N8N_PROTOCOL: https
      WEBHOOK_URL: https://${N8N_TRAEFIK_HOST}/
    depends_on:
      - postgres
    networks:
      - compose_network
    labels:
      - 'traefik.enable=${N8N_TRAEFIK_ENABLED}'
      - 'traefik.http.middlewares.${N8N_COMPOSE_PROJECT_NAME}-n8n-redirect-web-secure.redirectscheme.scheme=https'
      - 'traefik.http.routers.${N8N_COMPOSE_PROJECT_NAME}-n8n-web.middlewares=${N8N_COMPOSE_PROJECT_NAME}-n8n-redirect-web-secure'
      - 'traefik.http.routers.${N8N_COMPOSE_PROJECT_NAME}-n8n-web.rule=Host(`${N8N_TRAEFIK_HOST}`)'
      - 'traefik.http.routers.${N8N_COMPOSE_PROJECT_NAME}-n8n-web.entrypoints=web'
      - 'traefik.http.routers.${N8N_COMPOSE_PROJECT_NAME}-n8n-web-secure.rule=Host(`${N8N_TRAEFIK_HOST}`)'
      - 'traefik.http.routers.${N8N_COMPOSE_PROJECT_NAME}-n8n-web-secure.tls.certresolver=resolver'
      - 'traefik.http.routers.${N8N_COMPOSE_PROJECT_NAME}-n8n-web-secure.entrypoints=web-secure'
      - 'traefik.http.middlewares.${N8N_COMPOSE_PROJECT_NAME}-n8n-web-secure-compress.compress=true'
      - 'traefik.http.routers.${N8N_COMPOSE_PROJECT_NAME}-n8n-web-secure.middlewares=${N8N_COMPOSE_PROJECT_NAME}-n8n-web-secure-compress'
      - 'traefik.http.services.${N8N_COMPOSE_PROJECT_NAME}-n8n-web-secure.loadbalancer.server.port=5678'
      - 'traefik.docker.network=${NETWORK_NAME}'
      - 'com.centurylinklabs.watchtower.enable=${WATCHTOWER_LABEL_ENABLE}'

volumes:
  n8n_data:
    name: ${N8N_COMPOSE_PROJECT_NAME}_n8n_data

networks:
  compose_network:
    name: ${NETWORK_NAME}
    external: true
