# {{ project_name }} - Docker Compose Service Definition
# Generated by typedialog-prov-gen

version: '3.8'

services:
  app:
    image: "{{ docker_image | default(value=project_name ~ ":latest") }}"
    container_name: "{{ project_name }}-app"
    restart: unless-stopped

    ports:
      - "${APP_PORT:-8080}:8080"

    environment:
      - RUST_LOG=${RUST_LOG:-info}
      - APP_ENV=${APP_ENV:-production}
{% if infrastructure.database %}
      - DATABASE_URL=${DATABASE_URL}
{% endif %}

{% if infrastructure.database %}
  database:
    image: "{% if infrastructure.database.db_type == "postgres" %}postgres:15-alpine
{%- elif infrastructure.database.db_type == "mysql" %}mysql:8.0
{%- elif infrastructure.database.db_type == "redis" %}redis:7-alpine
{%- else %}sqlite:latest{% endif %}"
    container_name: "{{ project_name }}-db"
    restart: unless-stopped

    {% if infrastructure.database.db_type == "postgres" %}
    environment:
      - POSTGRES_DB={{ project_name }}
      - POSTGRES_USER=${DB_USER:-{{ project_name }}}
      - POSTGRES_PASSWORD=${DB_PASSWORD}
    {% elif infrastructure.database.db_type == "mysql" %}
    environment:
      - MYSQL_DATABASE={{ project_name }}
      - MYSQL_USER=${DB_USER:-{{ project_name }}}
      - MYSQL_PASSWORD=${DB_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
    {% endif %}

    volumes:
      - db_data:/var/lib/{% if infrastructure.database.db_type == "postgres" %}postgresql/data
      {%- elif infrastructure.database.db_type == "mysql" %}mysql
      {%- elif infrastructure.database.db_type == "redis" %}redis{% endif %}

    healthcheck:
      {% if infrastructure.database.db_type == "postgres" %}
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-{{ project_name }}}"]
      {% elif infrastructure.database.db_type == "mysql" %}
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      {% elif infrastructure.database.db_type == "redis" %}
      test: ["CMD", "redis-cli", "ping"]
      {% endif %}
      interval: 10s
      timeout: 5s
      retries: 5
{% endif %}

  nginx:
    image: nginx:alpine
    container_name: "{{ project_name }}-nginx"
    restart: unless-stopped

    ports:
      - "80:80"
      - "443:443"

    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - ./certs:/etc/nginx/certs:ro

    depends_on:
      - app

volumes:
{% if infrastructure.database %}
  db_data:
    driver: local
{% endif %}

networks:
  default:
    name: {{ project_name }}_network
