a
This commit is contained in:
parent
8a89592cb5
commit
639d9eb510
|
|
@ -28,7 +28,11 @@ cd backend
|
||||||
# 安装依赖
|
# 安装依赖
|
||||||
pip install -r requirements.txt
|
pip install -r requirements.txt
|
||||||
|
|
||||||
# 设置环境变量
|
# 配置数据库(方式1:使用.env文件,推荐)
|
||||||
|
cp .env.example .env
|
||||||
|
# 编辑 .env 文件,设置数据库连接信息
|
||||||
|
|
||||||
|
# 或方式2:使用环境变量
|
||||||
export DB_HOST=localhost
|
export DB_HOST=localhost
|
||||||
export DB_PORT=3306
|
export DB_PORT=3306
|
||||||
export DB_USER=root
|
export DB_USER=root
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,24 @@ pip install -r requirements.txt
|
||||||
|
|
||||||
## 数据库配置
|
## 数据库配置
|
||||||
|
|
||||||
设置环境变量:
|
### 方式1:使用.env文件(推荐)
|
||||||
|
|
||||||
|
在 `backend/` 目录或项目根目录创建 `.env` 文件:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 复制示例文件
|
||||||
|
cp .env.example .env
|
||||||
|
|
||||||
|
# 编辑.env文件
|
||||||
|
DB_HOST=localhost
|
||||||
|
DB_PORT=3306
|
||||||
|
DB_USER=root
|
||||||
|
DB_PASSWORD=your_password
|
||||||
|
DB_NAME=auto_trade_sys
|
||||||
|
CORS_ORIGINS=http://localhost:3000,http://localhost:5173
|
||||||
|
```
|
||||||
|
|
||||||
|
### 方式2:使用环境变量
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
export DB_HOST=localhost
|
export DB_HOST=localhost
|
||||||
|
|
@ -26,8 +43,11 @@ export DB_PORT=3306
|
||||||
export DB_USER=root
|
export DB_USER=root
|
||||||
export DB_PASSWORD=your_password
|
export DB_PASSWORD=your_password
|
||||||
export DB_NAME=auto_trade_sys
|
export DB_NAME=auto_trade_sys
|
||||||
|
export CORS_ORIGINS=http://localhost:3000,http://localhost:5173
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**注意**:系统会优先从 `.env` 文件读取配置,然后才是环境变量。
|
||||||
|
|
||||||
## 初始化数据库
|
## 初始化数据库
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,24 @@ from fastapi import FastAPI
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from api.routes import config, trades, stats, dashboard
|
from api.routes import config, trades, stats, dashboard
|
||||||
import os
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
# 加载.env文件
|
||||||
|
try:
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
backend_dir = Path(__file__).parent.parent
|
||||||
|
project_root = backend_dir.parent
|
||||||
|
env_file = backend_dir / '.env'
|
||||||
|
if not env_file.exists():
|
||||||
|
env_file = project_root / '.env'
|
||||||
|
if env_file.exists():
|
||||||
|
load_dotenv(env_file)
|
||||||
|
else:
|
||||||
|
load_dotenv(project_root / '.env', override=False)
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
title="Auto Trade System API",
|
title="Auto Trade System API",
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,26 @@ import sys
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
# 加载.env文件
|
||||||
|
try:
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
backend_dir = Path(__file__).parent
|
||||||
|
project_root = backend_dir.parent
|
||||||
|
env_files = [
|
||||||
|
backend_dir / '.env',
|
||||||
|
project_root / '.env',
|
||||||
|
]
|
||||||
|
for env_file in env_files:
|
||||||
|
if env_file.exists():
|
||||||
|
load_dotenv(env_file, override=True)
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
load_dotenv(project_root / '.env', override=False)
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
# 添加项目根目录到路径
|
# 添加项目根目录到路径
|
||||||
project_root = Path(__file__).parent.parent
|
project_root = Path(__file__).parent.parent
|
||||||
sys.path.insert(0, str(project_root))
|
sys.path.insert(0, str(project_root))
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,40 @@ import pymysql
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# 尝试加载.env文件
|
||||||
|
try:
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
# 从backend目录或项目根目录查找.env文件
|
||||||
|
backend_dir = Path(__file__).parent.parent
|
||||||
|
project_root = backend_dir.parent
|
||||||
|
|
||||||
|
# 按优先级查找.env文件
|
||||||
|
env_files = [
|
||||||
|
backend_dir / '.env', # backend/.env (优先)
|
||||||
|
project_root / '.env', # 项目根目录/.env
|
||||||
|
]
|
||||||
|
|
||||||
|
loaded = False
|
||||||
|
for env_file in env_files:
|
||||||
|
if env_file.exists():
|
||||||
|
load_dotenv(env_file, override=True)
|
||||||
|
logger.info(f"已加载环境变量文件: {env_file}")
|
||||||
|
loaded = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if not loaded:
|
||||||
|
# 如果都不存在,尝试自动查找(不报错)
|
||||||
|
load_dotenv(project_root / '.env', override=False)
|
||||||
|
except ImportError:
|
||||||
|
# 如果没有安装python-dotenv,跳过
|
||||||
|
logger.debug("python-dotenv未安装,跳过.env文件加载")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"加载.env文件失败: {e}")
|
||||||
|
|
||||||
|
|
||||||
class Database:
|
class Database:
|
||||||
"""数据库连接类"""
|
"""数据库连接类"""
|
||||||
|
|
@ -19,6 +50,9 @@ class Database:
|
||||||
self.password = os.getenv('DB_PASSWORD', '')
|
self.password = os.getenv('DB_PASSWORD', '')
|
||||||
self.database = os.getenv('DB_NAME', 'auto_trade_sys')
|
self.database = os.getenv('DB_NAME', 'auto_trade_sys')
|
||||||
|
|
||||||
|
# 记录配置信息(不显示密码)
|
||||||
|
logger.debug(f"数据库配置: host={self.host}, port={self.port}, user={self.user}, database={self.database}")
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def get_connection(self):
|
def get_connection(self):
|
||||||
"""获取数据库连接(上下文管理器)"""
|
"""获取数据库连接(上下文管理器)"""
|
||||||
|
|
|
||||||
|
|
@ -4,12 +4,29 @@
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
# 添加项目根目录
|
# 添加项目根目录和trading_system目录
|
||||||
project_root = Path(__file__).parent.parent
|
project_root = Path(__file__).parent.parent
|
||||||
sys.path.insert(0, str(project_root))
|
trading_system_path = project_root / 'trading_system'
|
||||||
|
|
||||||
|
# 确保trading_system在路径中
|
||||||
|
if str(trading_system_path) not in sys.path:
|
||||||
|
sys.path.insert(0, str(trading_system_path))
|
||||||
|
|
||||||
|
# 确保backend在路径中(用于导入database模块)
|
||||||
|
backend_path = Path(__file__).parent
|
||||||
|
if str(backend_path) not in sys.path:
|
||||||
|
sys.path.insert(0, str(backend_path))
|
||||||
|
|
||||||
|
# 导入数据库模型
|
||||||
from database.models import TradingConfig
|
from database.models import TradingConfig
|
||||||
import config
|
|
||||||
|
# 导入交易系统配置(现在在trading_system目录下)
|
||||||
|
try:
|
||||||
|
import config
|
||||||
|
except ImportError:
|
||||||
|
# 如果直接导入失败,尝试从trading_system导入
|
||||||
|
sys.path.insert(0, str(trading_system_path))
|
||||||
|
import config
|
||||||
|
|
||||||
def init_configs():
|
def init_configs():
|
||||||
"""将config.py中的配置初始化到数据库"""
|
"""将config.py中的配置初始化到数据库"""
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,9 @@ python-multipart==0.0.6
|
||||||
pymysql==1.1.0
|
pymysql==1.1.0
|
||||||
sqlalchemy==2.0.23
|
sqlalchemy==2.0.23
|
||||||
|
|
||||||
|
# 环境变量管理
|
||||||
|
python-dotenv==1.0.0
|
||||||
|
|
||||||
# 交易系统依赖(如果需要)
|
# 交易系统依赖(如果需要)
|
||||||
python-binance==1.0.19
|
python-binance==1.0.19
|
||||||
websocket-client==1.6.1
|
websocket-client==1.6.1
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user