hue-portal-backend / test_ngrok.sh
Davidtran99
chore: sync with main repo
3718c84
#!/bin/bash
# Script test ngrok tunnel và kiểm tra PostgreSQL connection
# Usage: ./test_ngrok.sh [ngrok_host] [ngrok_port]
set -e
# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m'
log() {
echo -e "${BLUE}[TEST]${NC} $1"
}
success() {
echo -e "${GREEN}${NC} $1"
}
warn() {
echo -e "${YELLOW}⚠️${NC} $1"
}
error() {
echo -e "${RED}${NC} $1"
}
# Default values
NGROK_API="http://192.168.1.212:4040"
DB_USER="${POSTGRES_USER:-hue_remote}"
DB_PASSWORD="${POSTGRES_PASSWORD:-huepass123}"
DB_NAME="${POSTGRES_DB:-hue_portal}"
echo -e "${BLUE}============================================================${NC}"
echo -e "${BLUE}Test Ngrok Tunnel và PostgreSQL Connection${NC}"
echo -e "${BLUE}============================================================${NC}"
echo ""
# Step 1: Check ngrok API
log "Bước 1: Kiểm tra ngrok API ($NGROK_API)..."
if command -v curl &> /dev/null; then
if curl -s "$NGROK_API/api/tunnels" &> /dev/null; then
success "Ngrok API đang chạy"
else
error "Không thể kết nối ngrok API"
echo ""
echo "💡 Kiểm tra:"
echo " 1. Ngrok container có đang chạy không?"
echo " 2. Port 4040 có được expose không?"
echo " 3. Thử truy cập: http://192.168.1.212:4040"
exit 1
fi
else
warn "Không có curl, bỏ qua kiểm tra API"
fi
# Step 2: Get ngrok tunnel URL
log "Bước 2: Lấy ngrok tunnel URL..."
if command -v curl &> /dev/null && command -v python3 &> /dev/null; then
response=$(curl -s "$NGROK_API/api/tunnels" 2>/dev/null || echo "")
if [ -n "$response" ]; then
ngrok_url=$(echo "$response" | python3 -c "
import sys, json
try:
data = json.load(sys.stdin)
tunnels = data.get('tunnels', [])
if not tunnels:
print('NO_TUNNELS')
sys.exit(0)
for tunnel in tunnels:
if tunnel.get('proto') == 'tcp':
public_url = tunnel.get('public_url', '')
if public_url.startswith('tcp://'):
url = public_url.replace('tcp://', '')
print(url)
sys.exit(0)
print('NO_TCP_TUNNEL')
except Exception as e:
print('ERROR: ' + str(e))
" 2>/dev/null)
if [ "$ngrok_url" = "NO_TUNNELS" ]; then
error "Không tìm thấy tunnel nào!"
echo ""
echo "💡 Kiểm tra:"
echo " 1. Ngrok có đang chạy command 'tcp' không?"
echo " 2. Toggle switch có được BẬT (ON) không?"
echo " 3. Xem logs: docker logs ngrok"
exit 1
elif [ "$ngrok_url" = "NO_TCP_TUNNEL" ]; then
error "Không tìm thấy TCP tunnel!"
echo ""
echo "💡 Ngrok có thể đang chạy HTTP tunnel, cần TCP tunnel cho PostgreSQL"
exit 1
elif [[ "$ngrok_url" == ERROR* ]]; then
error "Lỗi parse JSON: $ngrok_url"
exit 1
else
ngrok_host=$(echo "$ngrok_url" | cut -d':' -f1)
ngrok_port=$(echo "$ngrok_url" | cut -d':' -f2)
success "Tìm thấy ngrok URL: tcp://$ngrok_host:$ngrok_port"
fi
else
error "Không thể lấy thông tin từ ngrok API"
exit 1
fi
else
# Manual input
if [ -n "$1" ] && [ -n "$2" ]; then
ngrok_host="$1"
ngrok_port="$2"
warn "Dùng ngrok URL từ tham số: tcp://$ngrok_host:$ngrok_port"
else
error "Cần cung cấp ngrok host và port"
echo ""
echo "Usage: $0 [ngrok_host] [ngrok_port]"
echo "Ví dụ: $0 0.tcp.ap.ngrok.io 13432"
exit 1
fi
fi
echo ""
log "Thông tin kết nối:"
echo " Ngrok Host: $ngrok_host"
echo " Ngrok Port: $ngrok_port"
echo " Database User: $DB_USER"
echo " Database Name: $DB_NAME"
echo ""
# Step 3: Test PostgreSQL connection
log "Bước 3: Test kết nối PostgreSQL qua ngrok..."
export PGPASSWORD="$DB_PASSWORD"
if command -v psql &> /dev/null; then
if psql -h "$ngrok_host" -p "$ngrok_port" -U "$DB_USER" -d "$DB_NAME" -c "SELECT 1;" &>/dev/null; then
success "Kết nối PostgreSQL thành công!"
echo ""
# Get PostgreSQL version
VERSION=$(psql -h "$ngrok_host" -p "$ngrok_port" -U "$DB_USER" -d "$DB_NAME" -t -c "SELECT version();" 2>/dev/null | head -1 | xargs)
log "PostgreSQL version: ${VERSION:0:60}..."
# Count tables
TABLE_COUNT=$(psql -h "$ngrok_host" -p "$ngrok_port" -U "$DB_USER" -d "$DB_NAME" -t -c "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public';" 2>/dev/null | xargs)
success "Số lượng tables: $TABLE_COUNT"
echo ""
# Check data
echo -e "${BLUE}📊 Kiểm tra dữ liệu:${NC}"
echo "============================================================"
TOTAL_DOCS=0
for table in core_legaldocument core_fine core_procedure core_office core_advisory; do
COUNT=$(psql -h "$ngrok_host" -p "$ngrok_port" -U "$DB_USER" -d "$DB_NAME" -t -c "SELECT COUNT(*) FROM $table;" 2>/dev/null | xargs || echo "0")
if [ "$COUNT" != "0" ] && [ -n "$COUNT" ]; then
TOTAL_DOCS=$((TOTAL_DOCS + COUNT))
success "$table: $(printf "%'d" $COUNT) records"
else
warn "$table: 0 records (trống)"
fi
done
echo "============================================================"
success "Tổng số documents: $(printf "%'d" $TOTAL_DOCS)"
echo ""
# Show DATABASE_URL
echo -e "${BLUE}============================================================${NC}"
success "✅ NGROK TUNNEL HOẠT ĐỘNG TỐT!"
if [ "$TOTAL_DOCS" -gt 0 ]; then
success "Có $(printf "%'d" $TOTAL_DOCS) documents trong database"
else
warn "Database trống, chưa có dữ liệu"
fi
echo ""
echo -e "${BLUE}📝 DATABASE_URL để dùng:${NC}"
echo "postgres://$DB_USER:$DB_PASSWORD@$ngrok_host:$ngrok_port/$DB_NAME"
echo -e "${BLUE}============================================================${NC}"
else
error "KHÔNG KẾT NỐI ĐƯỢC PostgreSQL!"
echo ""
echo -e "${BLUE}💡 Kiểm tra:${NC}"
echo " 1. PostgreSQL có đang chạy trên 192.168.1.212:5432 không?"
echo " 2. Ngrok tunnel có đang chạy đúng command 'tcp 192.168.1.212:5432' không?"
echo " 3. Toggle switch có được BẬT (ON) không?"
echo " 4. Firewall có block không?"
echo ""
echo "Thử test trực tiếp:"
echo " psql -h $ngrok_host -p $ngrok_port -U $DB_USER -d $DB_NAME"
exit 1
fi
else
warn "Không có psql, bỏ qua test PostgreSQL"
echo ""
echo -e "${BLUE}📝 DATABASE_URL để dùng:${NC}"
echo "postgres://$DB_USER:$DB_PASSWORD@$ngrok_host:$ngrok_port/$DB_NAME"
fi