source ../environment.sh

# 获取 NGINX 容器的 IP 地址
NGINX_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${NGINX_NAME})
if [ -z "$NGINX_IP" ]; then
    echo "错误：无法获取 ${NGINX_NAME} 容器的 IP 地址"
fi
echo "获取到 ${NGINX_NAME} 容器的 IP 地址: ${NGINX_IP}"

if docker ps -a | grep -q ${OIDC_SERVER_NAME}; then
    echo "停止并删除已存在的 OIDC_SERVER_NAME 容器..."
    docker stop ${OIDC_SERVER_NAME} >/dev/null 2>&1
    docker rm ${OIDC_SERVER_NAME} >/dev/null 2>&1
fi


docker run --pull always -d \
--name ${OIDC_SERVER_NAME} \
--network ${NETWORK} \
--env SW_APP_NAME=${OIDC_SERVER_NAME} \
--env SW_AGENT_NAME=${OIDC_SERVER_NAME} \
--env SW_AGENT_COLLECTOR_BACKEND_SERVICES=${SKYWALKING_OAP_NAME}:11800 \
--env SPRING_CLOUD_CONSUL_HOST=${CONSUL_NAME} \
--env SPRING_CLOUD_CONSUL_PORT=8500 \
--env SPRING_CLOUD_CONSUL_DISCOVERY_HOSTNAME=${OIDC_SERVER_NAME} \
--env XXL_JOB_ADMIN_ADDRESSES=http://${XXLJOB_NAME}:8080/xxl-job-admin \
--env XXL_JOB_ACCESSTOKEN=${XXLJOB_ACCESS_TOKEN} \
--env XXL_ENABLE=false \
--env SPRING_REDIS_HOST=${REDIS_NAME} \
--env SPRING_REDIS_PORT=6379 \
--env SPRING_DATASOURCE_URL="jdbc:mysql://${MYSQL_NAME}:3306/tuoheng_oidc?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8&useSSL=true&tinyInt1isBit=false" \
--env SPRING_DATASOURCE_USERNAME=root \
--env SPRING_DATASOURCE_PASSWORD=${MYSQL_ROOT_PASSWORD} \
--env THIRD_TOKEN_ISSUER=http://${OIDC_SERVER_NAME}:8090 \
--env OAUTH2_TOKEN_ISSUER=https://${OIDC_DOMAIN_FULL} \
--env SPRING_SECURITY_OAUTH2_RESOURCE-SERVER_JWT_ISSUER-URI=https://${OIDC_DOMAIN_FULL} \
--env TZ=Asia/Shanghai \
--env SPRING_KAFKA_BOOTSTRAP_SERVERS=${KAFKA_NAME}:${KAFKA_PORT} \
--add-host ${OIDC_SERVER_DOMAIN}:host-gateway \
--mount type=bind,source=/etc/localtime,target=/etc/localtime,readonly \
--memory ${OIDC_SERVER_MEMORY} \
--restart unless-stopped \
${OIDC_SERVER_IMAGE}


#--add-host ${OIDC_SERVER_DOMAIN}:${NGINX_IP} \
#--add-host ${OIDC_SERVER_DOMAIN}:host-gateway \