services:
  apipark:
    image: apipark/apipark:v1.9.0-beta
    container_name: ${CONTAINER_NAME}
    restart: always
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
      influxdb:
        condition: service_healthy
      nsq:
        condition: service_started
    environment:
      - MYSQL_USER_NAME=root
      - MYSQL_PWD=${PANEL_DB_ROOT_PASSWORD}
      - MYSQL_IP=mysql
      - MYSQL_PORT=3306
      - MYSQL_DB=${PANEL_DB_NAME}
      - ERROR_DIR=work/logs
      - ERROR_FILE_NAME=error.log
      - ERROR_LOG_LEVEL=${LOG_LEVEL}
      - ERROR_EXPIRE=7d
      - ERROR_PERIOD=day
      - REDIS_ADDR=redis:6379
      - REDIS_PWD=${PANEL_REDIS_ROOT_PASSWORD}
      - ADMIN_PASSWORD=${ADMIN_PASSWORD}
      - Init=true
      - InfluxdbToken=${INFLUXDB_TOKEN}
      - NSQ_ADDR=nsq:4150
      - NSQ_TOPIC_PREFIX=apipark
    ports:
      - "${PANEL_APP_PORT_HTTP}:8288"
    volumes:
      - ./data/apipark:/app/work
    networks:
      - 1panel-network
    labels:
      createdBy: "Apps"

  mysql:
    image: mysql:8.0.37
    container_name: ${CONTAINER_NAME}_mysql
    restart: always
    command:
      - "--character-set-server=utf8mb4"
      - "--collation-server=utf8mb4_unicode_ci"
    environment:
      - MYSQL_ROOT_PASSWORD=${PANEL_DB_ROOT_PASSWORD}
      - MYSQL_DATABASE=${PANEL_DB_NAME}
      - TZ=Asia/Shanghai
    expose:
      - "3306"
    volumes:
      - ./data/mysql:/var/lib/mysql
    networks:
      - 1panel-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 5
    labels:
      createdBy: "Apps"

  redis:
    image: redis:7.2.4
    container_name: ${CONTAINER_NAME}_redis
    restart: always
    command: redis-server --requirepass ${PANEL_REDIS_ROOT_PASSWORD} --appendonly no --logfile redis.log
    expose:
      - "6379"
    volumes:
      - ./data/redis:/data
    networks:
      - 1panel-network
    healthcheck:
      test: ["CMD", "redis-cli", "-a", "${PANEL_REDIS_ROOT_PASSWORD}", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    labels:
      createdBy: "Apps"

  influxdb:
    image: influxdb:2.6
    container_name: ${CONTAINER_NAME}_influxdb
    restart: always
    environment:
      - DOCKER_INFLUXDB_INIT_USERNAME=admin
      - DOCKER_INFLUXDB_INIT_PASSWORD=${INFLUXDB_ADMIN_PASSWORD}
      - DOCKER_INFLUXDB_INIT_ORG=apipark
      - DOCKER_INFLUXDB_INIT_BUCKET=apinto
      - DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=${INFLUXDB_TOKEN}
      - DOCKER_INFLUXDB_INIT_MODE=setup
    expose:
      - "8086"
    volumes:
      - ./data/influxdb:/var/lib/influxdb2
    networks:
      - 1panel-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8086/api/v2/health"]
      interval: 30s
      timeout: 10s
      retries: 10
      start_period: 60s
    labels:
      createdBy: "Apps"

  loki:
    image: grafana/loki:3.2.1
    container_name: ${CONTAINER_NAME}_loki
    restart: always
    user: root
    expose:
      - "3100"
    volumes:
      - ./data/loki:/tmp/loki
    entrypoint:
      - sh
      - -euc
      - |
        mkdir -p /mnt/config
        cat <<EOF > /mnt/config/loki-config.yaml
        ---
        auth_enabled: false
        server:
          http_listen_port: 3100
          grpc_listen_port: 9096
        common:
          instance_addr: 127.0.0.1
          path_prefix: /tmp/loki
          storage:
            filesystem:
              chunks_directory: /tmp/loki/chunks
              rules_directory: /tmp/loki/rules
          replication_factor: 1
          ring:
            kvstore:
              store: inmemory
        query_range:
          results_cache:
            cache:
              embedded_cache:
                enabled: true
                max_size_mb: 100
        schema_config:
          configs:
            - from: 2020-10-24
              store: tsdb
              object_store: filesystem
              schema: v13
              index:
                prefix: index_
                period: 24h
        limits_config:
          max_query_length: 90d
        ruler:
          alertmanager_url: http://localhost:9093
        table_manager:
          retention_period: 90d
        EOF
        /usr/bin/loki -config.file=/mnt/config/loki-config.yaml
    networks:
      - 1panel-network
    labels:
      createdBy: "Apps"

  nsq:
    image: nsqio/nsq:latest
    container_name: ${CONTAINER_NAME}_nsq
    restart: always
    command: /nsqd --broadcast-address=nsq --lookupd-tcp-address=nsqlookupd:4160
    depends_on:
      - nsqlookupd
    expose:
      - "4150"
      - "4151"
    networks:
      - 1panel-network
    labels:
      createdBy: "Apps"

  nsqlookupd:
    image: nsqio/nsq:latest
    container_name: ${CONTAINER_NAME}_nsqlookupd
    restart: always
    command: /nsqlookupd
    expose:
      - "4160"
      - "4161"
    networks:
      - 1panel-network
    labels:
      createdBy: "Apps"

  grafana-init:
    image: busybox:latest
    container_name: ${CONTAINER_NAME}_grafana_init
    user: root
    command: |
      sh -c '
        chown -R 472:472 /var/lib/grafana
        chmod -R 755 /var/lib/grafana
      '
    volumes:
      - ./data/grafana:/var/lib/grafana
    networks:
      - 1panel-network
    labels:
      createdBy: "Apps"

  grafana:
    image: grafana/grafana:11.3.2
    container_name: ${CONTAINER_NAME}_grafana
    restart: always
    user: "472:472"
    environment:
      - GF_PATHS_PROVISIONING=/etc/grafana/provisioning
      - GF_AUTH_ANONYMOUS_ENABLED=true
      - GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
      - GF_SECURITY_ADMIN_PASSWORD=${ADMIN_PASSWORD}
      - GF_INSTALL_PLUGINS=
    depends_on:
      - loki
      - grafana-init
    entrypoint:
      - sh
      - -euc
      - |
        mkdir -p /etc/grafana/provisioning/datasources
        cat <<EOF > /etc/grafana/provisioning/datasources/ds.yaml
        apiVersion: 1
        datasources:
          - name: Loki
            type: loki
            access: proxy
            url: http://loki:3100
        EOF
        /run.sh
    expose:
      - "3000"
    volumes:
      - ./data/grafana:/var/lib/grafana
    networks:
      - 1panel-network
    healthcheck:
      test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:3000/api/health || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 5
    labels:
      createdBy: "Apps"

networks:
  1panel-network:
    external: true