From f28ac0166536a2a4b68cac685a41ea667f60f7e9 Mon Sep 17 00:00:00 2001
From: yj <2077506045@qq.com>
Date: 星期三, 03 九月 2025 14:43:03 +0800
Subject: [PATCH] 兼容企业微信

---
 startup.py |  128 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 128 insertions(+), 0 deletions(-)

diff --git a/startup.py b/startup.py
new file mode 100644
index 0000000..2f02c8d
--- /dev/null
+++ b/startup.py
@@ -0,0 +1,128 @@
+"""
+鍚姩鑴氭湰 - 澶勭悊閰嶇疆鏂囦欢鍜岀洰褰曞垵濮嬪寲
+"""
+
+import os
+import sys
+import json
+import shutil
+from pathlib import Path
+
+
+def get_exe_dir():
+    """鑾峰彇exe鏂囦欢鎵�鍦ㄧ洰褰�"""
+    if getattr(sys, "frozen", False):
+        # 濡傛灉鏄墦鍖呭悗鐨別xe鏂囦欢
+        return os.path.dirname(sys.executable)
+    else:
+        # 濡傛灉鏄紑鍙戠幆澧�
+        return os.path.dirname(os.path.abspath(__file__))
+
+
+def ensure_directories():
+    """纭繚蹇呰鐨勭洰褰曞瓨鍦�"""
+    exe_dir = get_exe_dir()
+
+    # 鍒涘缓鏃ュ織鐩綍
+    logs_dir = os.path.join(exe_dir, "logs")
+    if not os.path.exists(logs_dir):
+        os.makedirs(logs_dir)
+        print(f"鍒涘缓鏃ュ織鐩綍: {logs_dir}")
+
+    return exe_dir
+
+
+def ensure_config_file():
+    """纭繚閰嶇疆鏂囦欢瀛樺湪"""
+    exe_dir = get_exe_dir()
+    config_file = os.path.join(exe_dir, "config.json")
+
+    if not os.path.exists(config_file):
+        # 濡傛灉config.json涓嶅瓨鍦紝灏濊瘯澶嶅埗鐢熶骇閰嶇疆妯℃澘
+        production_config = os.path.join(exe_dir, "config.production.json")
+        example_config = os.path.join(exe_dir, "config.example.json")
+
+        if os.path.exists(production_config):
+            shutil.copy2(production_config, config_file)
+            print(f"澶嶅埗鐢熶骇閰嶇疆鏂囦欢: {production_config} -> {config_file}")
+        elif os.path.exists(example_config):
+            shutil.copy2(example_config, config_file)
+            print(f"澶嶅埗绀轰緥閰嶇疆鏂囦欢: {example_config} -> {config_file}")
+        else:
+            # 鍒涘缓榛樿閰嶇疆鏂囦欢
+            default_config = {
+                "database": {
+                    "url": "mysql+pymysql://root:password@localhost:3306/ecloud_dify"
+                },
+                "redis": {"url": "redis://localhost:6379/0"},
+                "ecloud": {
+                    "base_url": "http://125.122.152.142:9899",
+                    "authorization": "your_ecloud_authorization_token",
+                    "w_id": "your_ecloud_w_id",
+                },
+                "dify": {
+                    "base_url": "https://api.dify.ai/v1",
+                    "api_key": "your_dify_api_key",
+                },
+                "server": {"host": "0.0.0.0", "port": 7979, "debug": False},
+                "logging": {"level": "INFO", "file": "logs/app.log"},
+                "message_processing": {
+                    "max_retry_count": 3,
+                    "retry_delay": 5,
+                    "queue_timeout": 300,
+                },
+                "customer_service": {"names": ["瀹㈡湇1", "瀹㈡湇2"]},
+            }
+
+            with open(config_file, "w", encoding="utf-8") as f:
+                json.dump(default_config, f, indent=2, ensure_ascii=False)
+            print(f"鍒涘缓榛樿閰嶇疆鏂囦欢: {config_file}")
+
+    return config_file
+
+
+def setup_environment():
+    """璁剧疆杩愯鐜"""
+    exe_dir = ensure_directories()
+    config_file = ensure_config_file()
+
+    # 璁剧疆宸ヤ綔鐩綍涓篹xe鎵�鍦ㄧ洰褰�
+    os.chdir(exe_dir)
+    print(f"璁剧疆宸ヤ綔鐩綍: {exe_dir}")
+
+    return exe_dir, config_file
+
+
+if __name__ == "__main__":
+    setup_environment()
+
+    # 瀵煎叆骞跺惎鍔ㄤ富搴旂敤
+    try:
+        from main import app
+        import uvicorn
+        from config import settings
+        from loguru import logger
+
+        # 閰嶇疆鏃ュ織
+        logger.add(
+            settings.log_file,
+            rotation="1 day",
+            retention="7 days",
+            level=settings.log_level,
+            format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {name}:{function}:{line} - {message}",
+        )
+
+        logger.info("鍚姩E浜戠瀹�-DifyAI瀵规帴鏈嶅姟")
+
+        # 鍚姩鏈嶅姟
+        uvicorn.run(
+            app,
+            host=settings.server_host,
+            port=settings.server_port,
+            log_level=settings.log_level.lower(),
+        )
+
+    except Exception as e:
+        print(f"鍚姩澶辫触: {e}")
+        input("鎸変换鎰忛敭閫�鍑�...")
+        sys.exit(1)

--
Gitblit v1.9.1