version: '3'
services:
  {{web_cache_name}}:
    image: ghcr.io/freva-org/freva-redis:latest
{% if (ansible_become == true and ansible_become_user == 'root') or ansible_user == 'root' %}
    user: {{ uid }}:{{ gid }}
{% else %}
    user: root:0
{% endif %}
    environment:
      - REDIS_PASSWORD={{ web_redis_password }}
      - REDIS_USERNAME={{ web_redis_username }}
    hostname: {{ web_cache_name }}
    container_name: {{ web_cache_name }}
    volumes:
        - logs:/data/logs:z
    tty: true

  {{web_name}}:
    user: root:0
    image: ghcr.io/freva-org/freva-web:{{web_version}}
    container_name: {{ web_name }}
    hostname: {{ web_name }}
    environment:
      - EVALUATION_SYSTEM_CONFIG_FILE={{ core_root_dir|regex_replace('^~', ansible_facts.env.HOME) }}/freva/evaluation_system.conf
      - DJANGO_SUPERUSER_PASSWORD={{ root_passwd }}
      - ALLOWED_HOSTS={{web_allowed_hosts | join(',')}}
      - FREVA_WEB_CONFIG_FILE={{ web_config_file }}
      - SCHEDULER_HOST={{ web_scheduler_host | join(',') }}
      - REDIS_HOST={{ web_cache_name }}
      - CSRF_TRUSTED_ORIGINS={{ web_csrf_trusted_origins | join(',') }}
      - FREVA_BIN={{ web_freva_bin }}
      - COLUMNS=140
      - FREVA_REST_URL=http://{{web_freva_rest_host.replace("http://", "")}}
      - REDIS_PASSWD={{ web_redis_password }}
      - REDIS_USER={{ web_redis_username }}
      - OIDC_DISCOVERY_URL={{ web_oidc_url }}
      - OIDC_CLIENT_SECRET={{ web_oidc_client_secret }}
      - OIDC_CLIENT_ID={{ web_oidc_client }}
      - CHATBOT_HOST={{ web_chatbot_host.replace("http://", "") }}
      - STAC_BROWSER=1

    volumes:
      - /var/lib/sss/pipes:/var/lib/sss/pipes:ro
      - {{ core_root_dir | regex_replace('^~', ansible_facts.env.HOME)}}:{{ core_root_dir | regex_replace('^~', ansible_facts.env.HOME)}}:ro
      - {{ core_base_dir_location | regex_replace('^~', ansible_facts.env.HOME)}}:{{ core_base_dir_location | regex_replace('^~', ansible_facts.env.HOME)}}:ro
      - {{ core_scheduler_output_dir|regex_replace('^~', ansible_facts.env.HOME)}}:{{ core_scheduler_output_dir | regex_replace('^~', ansible_facts.env.HOME)}}:ro
      - django-static:/opt/freva_web/static:z
      - logs:/data/logs:z
    tty: true
    depends_on:
      - {{ web_cache_name }}

  {{web_proxy_name}}:
    user: root:0
    image: ghcr.io/freva-org/freva-nginx:{{ proxy_version or "latest" }}
    container_name: {{web_proxy_name}}
    hostname: {{web_proxy_name}}
    volumes:
        - django-static:/srv/static:z
        - logs:/data/logs:z
        - {{ core_preview_path | regex_replace('^~', ansible_facts.env.HOME)}}:/srv/static/preview:ro
    environment:
        - SERVER_ROOT=/srv/static
        - VAULT_HOST={{ web_vault_host.replace("http://", "") }}:5002
        - FREVA_REST_HOST={{ web_freva_rest_host.replace("http://", "") }}
        - EVALUATION_SYSTEM_CONFIG_FILE={{ core_root_dir|regex_replace('^~', ansible_facts.env.HOME) }}/freva/evaluation_system.conf
        - CHATBOT_HOST={{ web_chatbot_host.replace("http://", "") }}
        - WEB_SERVER_NAME={{ web_name }}
        - WEB_SERVER_PORT=8000
        - SERVER_CERT={{ web_cert_content }}
        - SERVER_KEY={{ web_key_content }}
        - REST_PROXY_HOST_NAME={{ web_chatbot_rest_api_url }}
        - FILL_COLOR={{ web_main_color }}
        - PROXY_DNS={{ current_nameservers | default('8.8.8.8') }}
{% if (ansible_become == true and ansible_become_user == 'root') or ansible_user == 'root' %}
        - PROXY_USER=nobody
{% else %}
        - PROXY_USER=root
{% endif %}
        - PROJECT_NAME={{ project_name }}
        - PORT_HTTPSD={{ web_port_httpsd }}
        - WEB_HOST={{ web_project_website.replace("http://", "").replace("https://", "").replace("www.","")  }}
    ports:
      - {{web_port_httpd}}:{{web_port_httpd}}
      - {{web_port_httpsd}}:{{web_port_httpsd}}
    depends_on:
      - {{ web_name }}

networks:
  {{web_name}}:
    driver: bridge

volumes:
    django-static:
    logs:
