From a406638e284c90f58438fdec5ebbd68cbe996a6f Mon Sep 17 00:00:00 2001 From: tangwang Date: Mon, 10 Nov 2025 15:35:42 +0800 Subject: [PATCH] up --- CLAUDE.md | 2 +- api/app.py | 24 ++++++++++++++++++++++-- api/routes/admin.py | 10 +++++----- scripts/start_backend.sh | 26 ++++++++++++++++++++------ start_all.sh | 29 ++++++++++++++++++++++++----- 5 files changed, 72 insertions(+), 19 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index c201b5e..49bd78e 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -88,7 +88,7 @@ The `searcher` supports: - Supports both local and remote images ## Test Data - +、 **Customer1 Test Dataset:** - Location: `data/customer1/goods_with_pic.5years_congku.csv.shuf.1w` - Contains 10,000 shuffled product records with images diff --git a/api/app.py b/api/app.py index 62ba7da..003e6b1 100644 --- a/api/app.py +++ b/api/app.py @@ -51,8 +51,28 @@ def init_service(customer_id: str = "customer1", es_host: str = "http://localhos print(f"Configuration loaded: {_config.customer_name}") - # Initialize ES client - _es_client = ESClient(hosts=[es_host]) + # Get ES credentials from environment variables or .env file + es_username = os.getenv('ES_USERNAME') + es_password = os.getenv('ES_PASSWORD') + + # Try to load from config if not in env + if not es_username or not es_password: + try: + from config.env_config import get_es_config + es_config = get_es_config() + es_username = es_username or es_config.get('username') + es_password = es_password or es_config.get('password') + except Exception: + pass + + # Initialize ES client with authentication if credentials are available + if es_username and es_password: + print(f"Connecting to Elasticsearch with authentication: {es_username}") + _es_client = ESClient(hosts=[es_host], username=es_username, password=es_password) + else: + print(f"Connecting to Elasticsearch without authentication") + _es_client = ESClient(hosts=[es_host]) + if not _es_client.ping(): raise ConnectionError(f"Failed to connect to Elasticsearch at {es_host}") diff --git a/api/routes/admin.py b/api/routes/admin.py index 3d0b7af..8802156 100644 --- a/api/routes/admin.py +++ b/api/routes/admin.py @@ -18,7 +18,7 @@ async def health_check(): Returns service status and Elasticsearch connectivity. """ try: - from main import get_es_client, get_config + from ..app import get_es_client, get_config es_client = get_es_client() config = get_config() @@ -46,7 +46,7 @@ async def get_configuration(): Get current customer configuration (sanitized). """ try: - from main import get_config + from ..app import get_config config = get_config() @@ -74,7 +74,7 @@ async def update_rewrite_rules(rules: Dict[str, str]): rules: Dictionary of pattern -> replacement mappings """ try: - from main import get_query_parser + from ..app import get_query_parser query_parser = get_query_parser() query_parser.update_rewrite_rules(rules) @@ -94,7 +94,7 @@ async def get_rewrite_rules(): Get current query rewrite rules. """ try: - from main import get_query_parser + from ..app import get_query_parser query_parser = get_query_parser() rules = query_parser.get_rewrite_rules() @@ -114,7 +114,7 @@ async def get_index_stats(): Get index statistics. """ try: - from main import get_es_client, get_config + from ..app import get_es_client, get_config es_client = get_es_client() config = get_config() diff --git a/scripts/start_backend.sh b/scripts/start_backend.sh index 416ae51..a7e4ac1 100755 --- a/scripts/start_backend.sh +++ b/scripts/start_backend.sh @@ -16,16 +16,30 @@ echo -e "${GREEN}========================================${NC}" echo -e "${GREEN}Starting Backend API Service${NC}" echo -e "${GREEN}========================================${NC}" -# Load config -source .env +# Load config from .env file if it exists +if [ -f .env ]; then + set -a + source .env + set +a +fi echo -e "\n${YELLOW}Configuration:${NC}" -echo " Customer: $CUSTOMER_ID" -echo " API Host: $API_HOST" -echo " API Port: $API_PORT" -echo " ES Host: $ES_HOST" +echo " Customer: ${CUSTOMER_ID:-customer1}" +echo " API Host: ${API_HOST:-0.0.0.0}" +echo " API Port: ${API_PORT:-6002}" +echo " ES Host: ${ES_HOST:-http://localhost:9200}" +echo " ES Username: ${ES_USERNAME:-not set}" echo -e "\n${YELLOW}Starting service...${NC}" + +# Export environment variables for the Python process +export CUSTOMER_ID=${CUSTOMER_ID:-customer1} +export API_HOST=${API_HOST:-0.0.0.0} +export API_PORT=${API_PORT:-6002} +export ES_HOST=${ES_HOST:-http://localhost:9200} +export ES_USERNAME=${ES_USERNAME:-} +export ES_PASSWORD=${ES_PASSWORD:-} + python -m api.app \ --host $API_HOST \ --port $API_PORT \ diff --git a/start_all.sh b/start_all.sh index 9f444ef..913d349 100755 --- a/start_all.sh +++ b/start_all.sh @@ -3,8 +3,8 @@ # One-click startup script for SearchEngine # This script starts everything you need - -set -e +# Don't exit on error - we want to continue even if some checks fail +# set -e cd "$(dirname "$0")" @@ -65,14 +65,33 @@ echo -e "${GREEN}日志文件: logs/backend.log${NC}" # Wait for backend to start echo -e "${YELLOW}等待后端服务启动...${NC}" -sleep 5 +MAX_RETRIES=12 +RETRY_COUNT=0 +BACKEND_READY=false + +while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do + sleep 2 + if curl -s http://localhost:6002/ > /dev/null 2>&1; then + BACKEND_READY=true + break + fi + RETRY_COUNT=$((RETRY_COUNT + 1)) + echo -e "${YELLOW} 等待中... ($RETRY_COUNT/$MAX_RETRIES)${NC}" +done # Check if backend is running -if curl -s http://localhost:6002/admin/health > /dev/null 2>&1; then +if [ "$BACKEND_READY" = true ]; then echo -e "${GREEN}✓ 后端服务运行正常${NC}" + # Try health check, but don't fail if it's not ready yet + if curl -s http://localhost:6002/admin/health > /dev/null 2>&1; then + echo -e "${GREEN}✓ 健康检查通过${NC}" + else + echo -e "${YELLOW}⚠ 健康检查未通过,但服务已启动${NC}" + fi else echo -e "${RED}✗ 后端服务启动失败,请检查日志: logs/backend.log${NC}" - exit 1 + echo -e "${YELLOW}提示: 后端服务可能需要更多时间启动,或者检查端口是否被占用${NC}" + # Don't exit - let user decide whether to continue fi # Step 4: Start frontend -- libgit2 0.21.2