# --------------------------------------
# (c) cavaliba.com - docker-compose.yml
# --------------------------------------
# 3.15.10


networks:
    default:
      name: ${CAVALIBA_TENANT:-cavaliba}

services:

  cavaliba_nginx:
    image: nginx:1.25.2
    container_name: ${CAVALIBA_TENANT:-cavaliba}_nginx
    restart: always
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
      #- ./htpasswd:/etc/nginx/htpasswd
      #- ./log/:/var/log/nginx/
      - ./files/logo:/logo
      - static_volume:/static_collect
    ports:
      - ${CAVALIBA_PORT:-80}:80
      #- 443:443
    env_file:
      - ./.env
    # depends_on:
    #   - cavaliba_app
    depends_on:
        cavaliba_app:
          condition: service_healthy


  cavaliba_app:
    #build: 
    #   context: ./source
    #   dockerfile: Dockerfile
    image: ${CAVALIBA_DOCKER_IMAGE}
    container_name: ${CAVALIBA_TENANT:-cavaliba}_app
    restart: always
    command: /docker-entrypoint.sh
    #command: python manage.py runserver 0.0.0.0:8001
    #command: gunicorn mysite.wsgi:application --bind 0.0.0.0:8001 --log-level info --access-logfile '-' --error-logfile '-' --graceful-timeout 3 --workers 2
    volumes:
      - static_volume:/static_collect
      # dev mode - map source/ for auto-reload
      #- ./source/:/app
      - ./files:/files
      - ./files:/app/files
      - ./demo:/app/demo
    expose:
      - 8001
    environment:
      DJANGO_SUPERUSER_PASSWORD: ${CAVALIBA_ADMIN_PASSWORD}
      DJANGO_SUPERUSER_EMAIL: ${CAVALIBA_ADMIN_EMAIL}
    env_file:
      - ./.env
    healthcheck:
        test: curl http://localhost:8001/status/
        interval: 5s
        timeout: 20s
        retries: 20
    depends_on:
        cavaliba_redis:
          condition: service_healthy
        cavaliba_db:
          condition: service_healthy


  cavaliba_celery:
    #build: 
    #   context: ./source
    #   dockerfile: Dockerfile    
    image: ${CAVALIBA_DOCKER_IMAGE}
    container_name: ${CAVALIBA_TENANT:-cavaliba}_celery
    restart: always
    working_dir: /app
    command: celery --workdir /app -A core worker -B -s /tmp/celery --loglevel=info --concurrency 3
    volumes:
      - ./files:/files
      - ./files:/app/files
      - ./demo:/app/demo
      - static_volume:/static_collect
    env_file:
      - ./.env
    depends_on:
        cavaliba_redis:
          condition: service_healthy
        cavaliba_db:
          condition: service_healthy
        cavaliba_app:
          condition: service_healthy


# Dedicated Worker for cron tasks (Celery beat scheduler) if multiple nodes
  # cavaliba_beat:
  #   #build: 
  #   #   context: ./source
  #   #   dockerfile: Dockerfile    
  #   #image: cavaliba/cavaliba:3.10
  #   image: ${CAVALIBA_DOCKER_IMAGE}
  #   container_name: ${CAVALIBA_TENANT}_beat   
  #   restart: always
  #   working_dir: /app
  #   command: celery --workdir /app -A core beat -s /tmp/celery --loglevel=info
  #   volumes:
  #     #- ./source/:/app
  #     - ./files:/files      
  #     # - ./web/logo:/logo
  #     - static_volume:/static_collect
  #   environment:
  #     DJANGO_SQL_ENGINE: ${CAVALIBA_DB_ENGINE}
  #     DJANGO_SQL_HOST: ${CAVALIBA_DB_HOST}
  #     DJANGO_SQL_PORT: ${CAVALIBA_DB_PORT}
  #     DJANGO_SQL_DATABASE: ${CAVALIBA_DB_DATABASE}
  #     DJANGO_SQL_USER: ${CAVALIBA_DB_USER}
  #     DJANGO_SQL_PASSWORD: ${CAVALIBA_DB_PASSWORD}
  #   env_file:
  #     - ./.env
  #   depends_on:
  #       cavaliba_redis:
  #         condition: service_healthy
  #       cavaliba_db:
  #         condition: service_healthy



# Uncomment to enable oauth2_proxy authentication (see doc)

  # cavaliba_oa2p:
  #   image: bitnami/oauth2-proxy:7.4.0
  #   container_name: ${CAVALIBA_TENANT:-cavaliba}_oa2p
  #   restart: always
  #   env_file:
  #     - ./.env
  #   expose:
  #     - 4180
  #   depends_on:
  #       cavaliba_redis:
  #         condition: service_healthy

  # MariaDB - uncomment to use / comment to switch to PostGreSQL
  cavaliba_db:
    image: mariadb:10.10
    container_name: ${CAVALIBA_TENANT:-cavaliba}_db
    restart: always
    volumes:
      - ./db:/var/lib/mysql
    expose:
      - 3306
    environment:
      MARIADB_DATABASE: ${CAVALIBA_DB_DATABASE}
      MARIADB_USER: ${CAVALIBA_DB_USER}
      MARIADB_PASSWORD: ${CAVALIBA_DB_PASSWORD}
      MARIADB_ROOT_PASSWORD: ${CAVALIBA_DB_ROOT_PASSWORD}
    # env_file:
    #   - ./.env
    command: --innodb-buffer-pool-size=512M
    healthcheck:
        test: mysqladmin ping -h 127.0.0.1 -u $$MARIADB_USER --password=$$MARIADB_PASSWORD
        interval: 2s
        timeout: 20s
        retries: 20

# postgres  - uncomment to use / comment to switch to MariaDB
#  cavaliba_db:
#    container_name: cavaliba_db
#    image: postgres:17.2
#    restart: always
#    environment:
#      POSTGRES_USER: ${CAVALIBA_DB_USER:-postgres}
#      POSTGRES_PASSWORD: ${CAVALIBA_DB_PASSWORD:-changeme}
#      PGDATA: /data/postgres
#    volumes:
#       - ./db:/data/postgres
#    expose:
#      - 5432
#    healthcheck:
#      test: ["CMD-SHELL", "pg_isready", "-d", "cavaliba"]
#      interval: 30s
#      timeout: 60s
#      retries: 5
#      start_period: 80s  


  cavaliba_redis:
    image: redis:7.0.2-alpine3.16
    #container_name: cavaliba_redis
    container_name: ${CAVALIBA_TENANT:-cavaliba}_redis
    expose:
      - 6379
    env_file:
      - ./.env
    command: ["redis-server", "--maxmemory", "500mb", "--maxmemory-policy", "allkeys-lru"]
    healthcheck:
        test: ["CMD", "redis-cli","ping"]
        interval: 2s
        timeout: 20s
        retries: 20

volumes:
  static_volume:
