From d83ae39541187e1832690937831fd6fb86777b86 Mon Sep 17 00:00:00 2001 From: yangdx Date: Wed, 26 Mar 2025 21:08:24 +0800 Subject: [PATCH] feat: add .env file check on startup - Show warning if .env is missing - For CLI startup: prompt for user confirmation - For service startup: display warning only --- lightrag/api/lightrag_server.py | 6 ++++++ lightrag/api/run_with_gunicorn.py | 6 +++++- lightrag/api/utils_api.py | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lightrag/api/lightrag_server.py b/lightrag/api/lightrag_server.py index 030a73d8..fe0e416e 100644 --- a/lightrag/api/lightrag_server.py +++ b/lightrag/api/lightrag_server.py @@ -22,7 +22,9 @@ from lightrag.api.utils_api import ( parse_args, get_default_host, display_splash_screen, + check_env_file, ) +import sys from lightrag import LightRAG, __version__ as core_version from lightrag.api import __api_version__ from lightrag.types import GPTKeywordExtractionFormat @@ -595,6 +597,10 @@ def main(): print("Running under Gunicorn - worker management handled by Gunicorn") return + # Check .env file + if not check_env_file(): + sys.exit(1) + # Check and install dependencies check_and_install_dependencies() diff --git a/lightrag/api/run_with_gunicorn.py b/lightrag/api/run_with_gunicorn.py index 35724e8b..8d71ba36 100644 --- a/lightrag/api/run_with_gunicorn.py +++ b/lightrag/api/run_with_gunicorn.py @@ -7,7 +7,7 @@ import os import sys import signal import pipmaster as pm -from lightrag.api.utils_api import parse_args, display_splash_screen +from lightrag.api.utils_api import parse_args, display_splash_screen, check_env_file from lightrag.kg.shared_storage import initialize_share_data, finalize_share_data from dotenv import load_dotenv @@ -47,6 +47,10 @@ def signal_handler(sig, frame): def main(): + # Check .env file + if not check_env_file(): + sys.exit(1) + # Check and install dependencies check_and_install_dependencies() diff --git a/lightrag/api/utils_api.py b/lightrag/api/utils_api.py index 5c2ca26d..cc0d1341 100644 --- a/lightrag/api/utils_api.py +++ b/lightrag/api/utils_api.py @@ -6,6 +6,7 @@ import os import argparse from typing import Optional, List, Tuple import sys +from ascii_colors import ASCIIColors import logging from ascii_colors import ASCIIColors from lightrag.api import __api_version__ @@ -16,6 +17,23 @@ from starlette.status import HTTP_403_FORBIDDEN from .auth import auth_handler from ..prompt import PROMPTS +def check_env_file(): + """ + Check if .env file exists and handle user confirmation if needed. + Returns True if should continue, False if should exit. + """ + if not os.path.exists(".env"): + warning_msg = "Warning: .env file not found. Some features may not work properly." + ASCIIColors.yellow(warning_msg) + + # Check if running in interactive terminal + if sys.stdin.isatty(): + response = input("Do you want to continue? (yes/no): ") + if response.lower() != "yes": + ASCIIColors.red("Server startup cancelled") + return False + return True + # Load environment variables load_dotenv()