services:

# бот для получения храниния и обновления ключей сертификатов https
  certbot:
    build:
      context: ./certbot
      dockerfile: Dockerfile
    container_name: certbot
    volumes:
      - ./certbot/config:/etc/letsencrypt/config
      - certbot_data:/etc/letsencrypt
      - certbot_www:/var/www/certbot
    env_file:
      - .env
    environment:
      - EMAIL=${EMAIL}
      - DOMAINS=${ALL_DOMAINS}
      - STAGING=0
    restart: unless-stopped
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "test -f /etc/letsencrypt/live/$$(echo $${DOMAINS} | cut -d',' -f1)/fullchain.pem || exit 1",
        ]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

# service сервис для работы nginx. Тут логика такая, если все перечисленные в стартовом скрипте, сайта (домены) имеют ключи тогда этот сервис свичаеться и работает через https.
  nginx:
    build:
      context: ./nginx
      dockerfile: Dockerfile
    env_file: .env
    container_name: nginx
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - certbot_data:/etc/letsencrypt
      - certbot_www:/var/www/certbot
      - ./stubSite:/usr/share/nginx/stub/html
      - ./yalarba/serv_spa/spa/vue/dist:/usr/share/nginx/yalarba/html
      - ./valitovgaziz/html:/usr/share/nginx/valitovgaziz/html
      - ./BB/bbvue/dist:/usr/share/nginx/begushiybashkir/html
      - analytics_logs:/var/log/analytics:ro
    networks:
      - web-network
      - internal
      - app-network
      - bb-network
    depends_on:
      easysite:
        condition: service_healthy
      api_es:
        condition: service_healthy
      certbot:
        condition: service_healthy
      api_tp:
        condition: service_healthy
      api_bb:
        condition: service_healthy
      analytics:
        condition: service_healthy
      api_yal:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost/health || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

# Statistics для сайта valitovgaziz.ru server работает на node.js 22 или выше. С бд не работает
  analytics:
    build:
      context: ./valitovgaziz/analytics
      dockerfile: Dockerfile
    container_name: analytics
    restart: unless-stopped
    ports:
      - "9999:3000"
    environment:
      - NODE_ENV=production
      - PORT=3000
      - LOG_LEVEL=info
      - LOG_RETENTION_DAYS=30
    volumes:
      - analytics_logs:/app/logs
      - analytics_data:/app/data
    networks:
      - web-network
      - internal
    healthcheck:
      test: ["CMD", "wget", "--spider", "http://localhost:3000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

# REST API app on Golang (Gorm, Chi) бизнес логика приложения yalarba.ru. Работает с БД на PostgresQL db:db_tp
  api_tp:
    build:
      context: ./yalarba/api_tp
      dockerfile: Dockerfile
    ports:
      - "8888:8080"
    container_name: api_tp
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
    environment:
      # Database connection settings
      DB_HOST: db
      DB_PORT: 5432
      DB_USER: postgres
      DB_PASSWORD: postgres
      DB_NAME: mydb
      APP_PORT: 8080
    networks:
      - app-network
    healthcheck:
      test:
        [
          "CMD",
          "wget",
          "--no-verbose",
          "--tries=1",
          "--spider",
          "http://localhost:8080/health",
        ]
      interval: 30s
      timeout: 10s
      retries: 3

# PostgresQL DB база данных для храниния информации приложений Yalarba.ru && Easysite102.ru
  db:
    image: postgres:15-alpine
    restart: unless-stopped
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: mydb
    container_name: db_tp
    volumes:
      - db_tp_data:/var/lib/postgresql/data
      - ./migrations:/docker-entrypoint-initdb.d
    networks:
      - app-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 30s
      timeout: 10s
      retries: 5

# REST API on Golang (Gorm, Chi) логика обработки информации для сайта БегущийБашкир Работает с БД db_bb on PostgresQL
  api_bb:
    build:
      context: ./BB/api_bb
      dockerfile: Dockerfile
    ports:
      - "7777:8080"
    container_name: api_bb
    restart: unless-stopped
    depends_on:
      db_bb:
        condition: service_healthy
    env_file:
      - ./BB/api_bb/.env
    volumes:
      - api_bb_uploads:/app/uploads
    environment:
      # Database connection settings
      DB_HOST: db_bb
      DB_PORT: 5432
      DB_USER: postgres
      DB_PASSWORD: postgres
      DB_NAME: bb_db
      APP_PORT: 8080
    networks:
      - bb-network
    healthcheck:
      test:
        [
          "CMD",
          "wget",
          "--no-verbose",
          "--tries=1",
          "--spider",
          "http://localhost:8080/api/health",
        ]
      interval: 30s
      timeout: 10s
      retries: 3

# PostgresQL DB база данных для работы сайта Бегущий Башкир
  db_bb:
    image: postgres:15-alpine
    restart: unless-stopped
    ports:
      - "5433:5432"
    container_name: db_bb
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: bb_db
    volumes:
      - db_bb_data:/var/lib/postgresql/data
    networks:
      - bb-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 30s
      timeout: 10s
      retries: 5

# SPA app прилжение выполнено на nuxt.js интерфейс для туристического бизнеса. Хранение информации в api_es REST API app
  easysite:
    build:
      context: ./yalarba/easySite/easySite
      dockerfile: Dockerfile
    container_name: easysite
    restart: unless-stopped
    ports:
      - "3000:3000"
    environment:
      NODE_ENV: production
      HOST: 0.0.0.0
      PORT: 3000
    networks:
      - web-network
      - app-network
    healthcheck:
      test: ["CMD", "wget", "--spider", "http://localhost:3000/api/health"]
      interval: 30s
      timeout: 10s
      retries: 3

# REST API приложение для easysite102.ru тут бизнес логика и система для обращения к PostgresQL БД (тоже сервис db:db_tp)
  api_es:
    build:
      context: ./yalarba/api_es
      dockerfile: Dockerfile
    container_name: api_es
    restart: unless-stopped
    env_file:
      - ./yalarba/api_es/.env
    depends_on:
      db:
        condition: service_healthy
    environment:
      DB_HOST: db
      DB_PORT: 5432
      DB_USER: postgres
      DB_PASSWORD: postgres
      DB_NAME: mydb
      APP_PORT: ${API_ES_APP_PORT}
    networks:
      - app-network
      - web-network
    healthcheck:
      test: ["CMD", "wget", "--spider", "http://localhost:8088/health"]
      interval: 30s
      timeout: 10s
      retries: 3

# REST API app on Golang для api_yal сервиса
  api_yal:
    build:
      context: ./yalarba/api_yal  # Укажите правильный путь к вашему проекту
      dockerfile: Dockerfile
    container_name: api_yal
    restart: unless-stopped
    env_file:
      - ./yalarba/api_yal/.env
    depends_on:
      db:
        condition: service_healthy
    networks:
      - web-network
      - app-network
    healthcheck:
      test:
        [
          "CMD",
          "wget",
          "--no-verbose",
          "--tries=1",
          "--spider",
          "http://localhost:8787/health",
        ]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s


volumes:
  certbot_data:   # volume для данных Certbot
  certbot_www:    # volume для данных Certbot
  db_tp_data:     # Volume для данных БД yalarba.ru
  db_bb_data:     # Volume для данных БД Бегущий башкир
  api_bb_uploads: # Volume для загружаемых файлов бегущий башкир
  analytics_logs: # Volume для логов аналитики
  analytics_data: # Volume для данных аналитики

networks:
  web-network:
    driver: bridge
  internal:
    driver: bridge
  app-network:
    driver: bridge
  bb-network:
    driver: bridge

# Эта опция автоматически удаляет orphans (Не используемые контейнеры)
x-remove-orphans: true