# ignore: true
# documentation: https://pterodactyl.io/
# slogan: Pterodactyl is a free, open-source game server management panel
# tags: game, game server, management, panel, minecraft
# logo: svgs/pterodactyl.png
# port: 80

services:
  mariadb:
    image: mariadb:10.5
    restart: unless-stopped
    command: --default-authentication-plugin=mysql_native_password
    healthcheck:
      test:
        ["CMD-SHELL", "healthcheck.sh --connect --innodb_initialized || exit 1"]
      start_period: 10s
      interval: 10s
      timeout: 1s
      retries: 3
    environment:
      - SERVICE_PASSWORD_MYSQL
      - MYSQL_ROOT_PASSWORD=$SERVICE_PASSWORD_MYSQLROOT
      - MYSQL_DATABASE=panel
      - MYSQL_USER=pterodactyl
      - MYSQL_PASSWORD=$SERVICE_PASSWORD_MYSQL
    volumes:
      - pterodactyl-db:/var/lib/mysql

  redis:
    image: redis:alpine
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "redis-cli ping || exit 1"]
      interval: 10s
      timeout: 1s
      retries: 3

  pterodactyl:
    image: ghcr.io/pterodactyl/panel:latest
    restart: unless-stopped
    volumes:
      - "panel-var:/app/var/"
      - "panel-nginx:/etc/nginx/http.d/"
      - "panel-certs:/etc/letsencrypt/"
      - type: bind
        source: ./etc/entrypoint.sh
        target: /entrypoint.sh
        mode: "0755"
        content: |
          #!/bin/sh
          set -e

          echo "Waiting for services to be ready..."
          sleep 30

          echo "Setting logs permissions..."
          chown -R nginx: /app/storage/logs/

          if ! php artisan p:user:list | grep -q "$ADMIN_EMAIL"; then
            echo "Creating admin user..."
            php artisan p:user:make --no-interaction \
              --admin=1 \
              --email="$ADMIN_EMAIL" \
              --username="$ADMIN_USERNAME" \
              --name-first="$ADMIN_FIRSTNAME" \
              --name-last="$ADMIN_LASTNAME" \
              --password="$ADMIN_PASSWORD"
            echo "Admin user created"
          else
            echo "Admin user already exists, skipping creation"
          fi

           exec supervisord -c --nodaemon

    command: ["/entrypoint.sh"]
    healthcheck:
      test: ["CMD-SHELL", "curl -sf http://localhost:80 || exit 1"]
      interval: 10s
      timeout: 1s
      retries: 3
    environment:
      - SERVICE_FQDN_PTERODACTYL
      - ADMIN_EMAIL=${ADMIN_EMAIL:-admin@example.com}
      - ADMIN_USERNAME=${SERVICE_USER_ADMIN}
      - ADMIN_FIRSTNAME=${ADMIN_FIRSTNAME:-Admin}
      - ADMIN_LASTNAME=${ADMIN_LASTNAME:-User}
      - ADMIN_PASSWORD=${SERVICE_PASSWORD_ADMIN}
      - PTERODACTYL_HTTPS=${PTERODACTYL_HTTPS:-false}
      - APP_ENV=production
      - APP_ENVIRONMENT_ONLY=false
      - APP_URL=${PTERODACTYL_PUBLIC_FQDN:-$SERVICE_FQDN_PTERODACTYL}
      - APP_TIMEZONE=${TIMEZONE:-UTC}
      - APP_SERVICE_AUTHOR=${APP_SERVICE_AUTHOR:-author@example.com}
      - LOG_LEVEL=${LOG_LEVEL:-debug}
      - CACHE_DRIVER=redis
      - SESSION_DRIVER=redis
      - QUEUE_DRIVER=redis
      - REDIS_HOST=redis
      - DB_HOST=mariadb
      - DB_PORT=3306
      - DB_PASSWORD=$SERVICE_PASSWORD_MYSQL
      - MAIL_FROM=$MAIL_FROM
      - MAIL_DRIVER=$MAIL_DRIVER
      - MAIL_HOST=$MAIL_HOST
      - MAIL_PORT=$MAIL_PORT
      - MAIL_USERNAME=$MAIL_USERNAME
      - MAIL_PASSWORD=$MAIL_PASSWORD
      - MAIL_ENCRYPTION=$MAIL_ENCRYPTION

  wings:
    image: ghcr.io/pterodactyl/wings:latest
    restart: unless-stopped
    environment:
      - SERVICE_FQDN_WINGS_8080
      - TZ=${TIMEZONE:-UTC}
      - WINGS_USERNAME=pterodactyl
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
      - "/var/lib/docker/containers/:/var/lib/docker/containers/"
      - "/var/lib/pterodactyl/:/var/lib/pterodactyl/" # See https://discord.com/channels/122900397965705216/493443725012500490/1272195151309045902
      - "/tmp/pterodactyl/:/tmp/pterodactyl/" # See https://discord.com/channels/122900397965705216/493443725012500490/1272195151309045902
      - "wings-logs:/var/log/pterodactyl/"

      - type: bind
        source: ./etc/config.yml
        target: /etc/pterodactyl/config.yml
        content: |
          docker:
            network:
              interface: 172.28.0.1
              dns:
              - 1.1.1.1
              - 1.0.0.1
              name: pterodactyl_nw
              ispn: false
              driver: ""
              network_mode: pterodactyl_nw
              is_internal: false
              enable_icc: true
              network_mtu: 1500
              interfaces:
                v4:
                  subnet: 172.28.0.0/16
                  gateway: 172.28.0.1
                v6:
                  subnet: fdba:17c8:6c94::/64
                  gateway: fdba:17c8:6c94::1011
