# documentation: https://firefly-iii.org
# slogan: A personal finances manager that can help you save money.
# category: finance
# tags: finance, money, personal, manager
# logo: svgs/firefly.svg
# port: 8080

services:
  firefly:
    image: fireflyiii/core:latest
    environment:
      - SERVICE_URL_FIREFLY_8080
      - APP_KEY=$SERVICE_BASE64_APPKEY
      - DB_HOST=mysql
      - DB_PORT=3306
      - DB_CONNECTION=mysql
      - DB_DATABASE=${MYSQL_DATABASE:-firefly}
      - DB_USERNAME=$SERVICE_USER_MYSQL
      - DB_PASSWORD=$SERVICE_PASSWORD_MYSQL
      - STATIC_CRON_TOKEN=$SERVICE_BASE64_CRONTOKEN
      - TRUSTED_PROXIES=*
    volumes:
      - firefly-upload:/var/www/html/storage/upload
    healthcheck:
      test: ["CMD", "curl", "-f", "http://127.0.0.1:8080/health"]
      interval: 5s
      timeout: 20s
      retries: 10
    depends_on:
      mysql:
        condition: service_healthy
  mysql:
    image: mariadb:11
    environment:
      - MYSQL_USER=${SERVICE_USER_MYSQL}
      - MYSQL_PASSWORD=${SERVICE_PASSWORD_MYSQL}
      - MYSQL_DATABASE=${MYSQL_DATABASE:-firefly}
      - MYSQL_ROOT_PASSWORD=${SERVICE_PASSWORD_MYSQLROOT}
    healthcheck:
      test:
        [
          "CMD",
          "mariadb-admin",
          "ping",
          "-h",
          "127.0.0.1",
          "-uroot",
          "-p${SERVICE_PASSWORD_MYSQLROOT}",
        ]
      interval: 5s
      timeout: 20s
      retries: 10
    volumes:
      - firefly-mysql-data:/var/lib/mysql
  cron:
    image: alpine
    entrypoint: ["/entrypoint.sh"]
    volumes:
      - type: bind
        source: ./entrypoint.sh
        target: /entrypoint.sh
        content: |
          #!/bin/sh
          # Substitute the environment variable into the cron command
          CRON_COMMAND="0 3 * * * wget -qO- http://firefly:8080/api/v1/cron/${STATIC_CRON_TOKEN}"
          # Add the cron command to the crontab
          echo "$CRON_COMMAND" | crontab -
          # Start the cron daemon in the foreground with logging to stdout
          crond -f -L /dev/stdout
    environment:
      - STATIC_CRON_TOKEN=$SERVICE_BASE64_CRONTOKEN
    healthcheck:
      test: ["CMD-SHELL", "ls /entrypoint.sh || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
