name: ecom01

services:
  php:
    container_name: ecom01_backend
    build:
      context: .
      dockerfile: docker/php/Dockerfile
    restart: unless-stopped
    working_dir: /var/www/admin
    env_file:
      - ./admin/.env
    environment:
      PHP_OPCACHE_VALIDATE_TIMESTAMPS: "0"
      PHP_OPCACHE_REVALIDATE_FREQ: "0"
    volumes:
      - ./admin:/var/www/admin
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_started
    networks:
      - internal

  nginx:
    container_name: ecom01_nginx
    image: nginx:stable-alpine
    restart: unless-stopped
    volumes:
      - ./admin:/var/www/admin:ro
      - ./admin/storage/app/public:/var/www/cdn:ro
      - ./docker/nginx/production/default.conf:/etc/nginx/conf.d/default.conf:ro
    depends_on:
      - php
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=${TRAEFIK_NETWORK:-traefik-public}"
      - "traefik.http.routers.ecom01-admin.rule=Host(`${ADMIN_DOMAIN:-admin.ecom01.rs}`)"
      - "traefik.http.routers.ecom01-admin.entrypoints=websecure"
      - "traefik.http.routers.ecom01-admin.tls=true"
      - "traefik.http.routers.ecom01-admin.tls.certresolver=${TRAEFIK_CERTRESOLVER:-letsencrypt}"
      - "traefik.http.routers.ecom01-admin.service=ecom01-admin"
      - "traefik.http.routers.ecom01-admin-http.rule=Host(`${ADMIN_DOMAIN:-admin.ecom01.rs}`)"
      - "traefik.http.routers.ecom01-admin-http.entrypoints=web"
      - "traefik.http.routers.ecom01-admin-http.middlewares=ecom01-https-redirect"
      - "traefik.http.routers.ecom01-admin-http.service=ecom01-admin"
      - "traefik.http.services.ecom01-admin.loadbalancer.server.port=80"
      - "traefik.http.routers.ecom01-cdn.rule=Host(`${CDN_DOMAIN:-cdn.ecom01.rs}`)"
      - "traefik.http.routers.ecom01-cdn.entrypoints=websecure"
      - "traefik.http.routers.ecom01-cdn.tls=true"
      - "traefik.http.routers.ecom01-cdn.tls.certresolver=${TRAEFIK_CERTRESOLVER:-letsencrypt}"
      - "traefik.http.routers.ecom01-cdn.service=ecom01-cdn"
      - "traefik.http.routers.ecom01-cdn-http.rule=Host(`${CDN_DOMAIN:-cdn.ecom01.rs}`)"
      - "traefik.http.routers.ecom01-cdn-http.entrypoints=web"
      - "traefik.http.routers.ecom01-cdn-http.middlewares=ecom01-https-redirect"
      - "traefik.http.routers.ecom01-cdn-http.service=ecom01-cdn"
      - "traefik.http.services.ecom01-cdn.loadbalancer.server.port=80"
      - "traefik.http.middlewares.ecom01-https-redirect.redirectscheme.scheme=https"
      - "traefik.http.middlewares.ecom01-https-redirect.redirectscheme.permanent=true"
    networks:
      - internal
      - traefik

  frontend:
    container_name: ecom01_frontend
    build:
      context: .
      dockerfile: docker/node/Dockerfile
      target: runner
      args:
        NEXT_PUBLIC_APP_URL: ${NEXT_PUBLIC_APP_URL:-https://www.ecom01.rs}
        NEXT_PUBLIC_ADMIN_URL: ${NEXT_PUBLIC_ADMIN_URL:-https://admin.ecom01.rs}
        NEXT_PUBLIC_CDN_URL: ${NEXT_PUBLIC_CDN_URL:-https://cdn.ecom01.rs}
        NEXT_PUBLIC_API_URL: ${NEXT_PUBLIC_API_URL:-https://admin.ecom01.rs}
        NEXT_PUBLIC_GA4_ID: ${NEXT_PUBLIC_GA4_ID:-}
        NEXT_PUBLIC_CLARITY_ID: ${NEXT_PUBLIC_CLARITY_ID:-wtphroayu7}
        NEXT_PUBLIC_API_REVALIDATE_SECONDS: ${NEXT_PUBLIC_API_REVALIDATE_SECONDS:-300}
    restart: unless-stopped
    environment:
      NODE_ENV: production
      HOSTNAME: 0.0.0.0
      PORT: 3000
      NEXT_PUBLIC_APP_URL: ${NEXT_PUBLIC_APP_URL:-https://www.ecom01.rs}
      NEXT_PUBLIC_ADMIN_URL: ${NEXT_PUBLIC_ADMIN_URL:-https://admin.ecom01.rs}
      NEXT_PUBLIC_CDN_URL: ${NEXT_PUBLIC_CDN_URL:-https://cdn.ecom01.rs}
      NEXT_PUBLIC_API_URL: ${NEXT_PUBLIC_API_URL:-https://admin.ecom01.rs}
      NEXT_PUBLIC_GA4_ID: ${NEXT_PUBLIC_GA4_ID:-}
      NEXT_PUBLIC_CLARITY_ID: ${NEXT_PUBLIC_CLARITY_ID:-wtphroayu7}
      NEXT_PUBLIC_API_REVALIDATE_SECONDS: ${NEXT_PUBLIC_API_REVALIDATE_SECONDS:-300}
      SERVER_API_URL: http://nginx
      SERVER_API_HOST: ${ADMIN_DOMAIN:-admin.ecom01.rs}
    depends_on:
      - nginx
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=${TRAEFIK_NETWORK:-traefik-public}"
      - "traefik.http.routers.ecom01-frontend.rule=Host(`${FRONTEND_DOMAIN:-www.ecom01.rs}`)"
      - "traefik.http.routers.ecom01-frontend.entrypoints=websecure"
      - "traefik.http.routers.ecom01-frontend.tls=true"
      - "traefik.http.routers.ecom01-frontend.tls.certresolver=${TRAEFIK_CERTRESOLVER:-letsencrypt}"
      - "traefik.http.routers.ecom01-frontend.tls.domains[0].main=${FRONTEND_DOMAIN:-www.ecom01.rs}"
      - "traefik.http.routers.ecom01-frontend.tls.domains[0].sans=${APEX_DOMAIN:-ecom01.rs}"
      - "traefik.http.routers.ecom01-frontend.service=ecom01-frontend"
      - "traefik.http.routers.ecom01-frontend-http.rule=Host(`${FRONTEND_DOMAIN:-www.ecom01.rs}`)"
      - "traefik.http.routers.ecom01-frontend-http.entrypoints=web"
      - "traefik.http.routers.ecom01-frontend-http.middlewares=ecom01-https-redirect"
      - "traefik.http.routers.ecom01-frontend-http.service=ecom01-frontend"
      - "traefik.http.routers.ecom01-apex.rule=Host(`${APEX_DOMAIN:-ecom01.rs}`)"
      - "traefik.http.routers.ecom01-apex.entrypoints=websecure"
      - "traefik.http.routers.ecom01-apex.tls=true"
      - "traefik.http.routers.ecom01-apex.tls.certresolver=${TRAEFIK_CERTRESOLVER:-letsencrypt}"
      - "traefik.http.routers.ecom01-apex.middlewares=ecom01-apex-redirect"
      - "traefik.http.routers.ecom01-apex.service=ecom01-frontend"
      - "traefik.http.routers.ecom01-apex-http.rule=Host(`${APEX_DOMAIN:-ecom01.rs}`)"
      - "traefik.http.routers.ecom01-apex-http.entrypoints=web"
      - "traefik.http.routers.ecom01-apex-http.middlewares=ecom01-apex-redirect"
      - "traefik.http.routers.ecom01-apex-http.service=ecom01-frontend"
      - "traefik.http.middlewares.ecom01-apex-redirect.redirectregex.regex=^https?://${APEX_DOMAIN:-ecom01.rs}/(.*)"
      - "traefik.http.middlewares.ecom01-apex-redirect.redirectregex.replacement=https://${FRONTEND_DOMAIN:-www.ecom01.rs}/$${1}"
      - "traefik.http.middlewares.ecom01-apex-redirect.redirectregex.permanent=true"
      - "traefik.http.services.ecom01-frontend.loadbalancer.server.port=3000"
    networks:
      - internal
      - traefik

  db:
    container_name: ecom01_db
    image: mysql:8.0
    restart: unless-stopped
    command:
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_0900_ai_ci
    environment:
      MYSQL_DATABASE: ${DB_DATABASE:-ecom01}
      MYSQL_USER: ${DB_USERNAME:-ecom01}
      MYSQL_PASSWORD: ${DB_PASSWORD:?DB_PASSWORD is required}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD:?DB_ROOT_PASSWORD is required}
    volumes:
      - ecom01_mysql_data:/var/lib/mysql
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h localhost -uroot -p\"$${MYSQL_ROOT_PASSWORD}\""]
      interval: 10s
      timeout: 5s
      retries: 12
    networks:
      - internal

  redis:
    container_name: ecom01_redis
    image: redis:alpine
    restart: unless-stopped
    volumes:
      - ecom01_redis_data:/data
    networks:
      - internal

networks:
  internal:
    name: ecom01_internal
    driver: bridge
  traefik:
    external: true
    name: ${TRAEFIK_NETWORK:-traefik-public}

volumes:
  ecom01_mysql_data:
    name: ecom01_mysql_data
  ecom01_redis_data:
    name: ecom01_redis_data
