#!/bin/bash
source ../environment.sh

# 检查必要的环境变量
if [ -z "$NETWORK" ] || [ -z "$KAFKA_UI_PORT" ] || [ -z "$KAFKA_UI_NAME" ] || [ -z "$KAFKA_UI_IMAGE" ] || [ -z "$KAFKA_UI_MEMORY" ]; then
    echo "Error: Required environment variables are not set"
    exit 1
fi

# 停止并删除已存在的容器
docker stop ${KAFKA_UI_NAME} 2>/dev/null
docker rm ${KAFKA_UI_NAME} 2>/dev/null

# 启动 Kafka UI
docker run -d \
    --name ${KAFKA_UI_NAME} \
    --network ${NETWORK} \
    -p ${KAFKA_UI_PORT}:8080 \
    -e KAFKA_CLUSTERS_0_NAME=local \
    -e KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS=${KAFKA_NAME}:${KAFKA_PORT} \
    -e JAVA_TOOL_OPTIONS="-Xmx${KAFKA_UI_MEMORY}" \
    ${KAFKA_UI_IMAGE}

echo "Kafka UI started at http://localhost:${KAFKA_UI_PORT}" 