Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# Logs
logs/
*.log

# Environment files
.env
.venv/
venv/
ENV/

# IDE
.idea/
.vscode/
*.swp
*.swo

# OS generated files
.DS_Store
Thumbs.db

# Git
.git/
.gitignore

# Docker
.dockerignore

# Readme file (if not needed in container)
README.md
53 changes: 53 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# 构建阶段
FROM python:3.11 as builder

WORKDIR /app

# 安装构建依赖
RUN apt-get update && apt-get install -y \
gcc \
python3-dev \
&& rm -rf /var/lib/apt/lists/*

# 复制依赖文件
COPY requirements.txt .

# 安装依赖到系统路径
RUN pip install -r requirements.txt

# 生产阶段
FROM python:3.11-slim

WORKDIR /app

# 安装运行时依赖
RUN apt-get update && apt-get install -y \
gcc \
python3-dev \
&& rm -rf /var/lib/apt/lists/*

# 复制已安装的Python包
COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin

# 设置PATH以包含新复制的bin目录
ENV PATH=/usr/local/bin:$PATH

# 复制项目文件
COPY . .

# 创建必要的目录
RUN mkdir -p logs

# 暴露端口
EXPOSE 8000

# 设置非root用户
RUN useradd --create-home --shell /bin/bash app && chown -R app:app /app
USER app

# 初始化数据库
RUN python -c "from database import engine, Base; Base.metadata.create_all(bind=engine); print('✅ 数据库初始化完成')"

# 启动命令
CMD ["python", "start.py", "--skip-install"]
8 changes: 7 additions & 1 deletion database.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
import json
import hashlib
import secrets
import os

# 确保数据库文件路径在Docker容器中可写
DATABASE_URL = "sqlite:///./api_records.db"

engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
# 添加连接参数以适应Docker环境
engine = create_engine(DATABASE_URL, connect_args={
"check_same_thread": False,
"timeout": 30
})
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()
Expand Down
16 changes: 16 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: '3.8'

services:
redwolf:
build: .
container_name: redwolf-app
ports:
- "8000:8000"
volumes:
- redwolf_data:/app
environment:
- DEBUG_MODE=false
command: python start.py --skip-install

volumes:
redwolf_data:
31 changes: 27 additions & 4 deletions start.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@
import os
import argparse

# 初始化数据库
def init_database():
"""初始化数据库表"""
try:
from database import engine, Base
from database import (
APIRecord, PlatformConfig, ModelConfig, RoutingConfig, RoutingScene,
SystemConfig, ClaudeCodeServer, UserAuth, LoginSession
)

print("正在初始化数据库...")
# 创建所有表
Base.metadata.create_all(bind=engine)
print("✅ 数据库初始化完成")
except Exception as e:
print(f"❌ 数据库初始化失败: {e}")

def install_dependencies():
"""安装依赖包"""
print("正在安装Python依赖包...")
Expand All @@ -21,7 +38,7 @@ def install_dependencies():
def start_server(debug=False):
"""启动服务器"""
print("正在启动API Hook监控系统...")
print("服务地址: http://127.0.0.1:8000")
print("服务地址: http://0.0.0.0:8000")
if debug:
print("🐛 DEBUG模式已启用 - 将显示详细调试信息")
os.environ['DEBUG_MODE'] = 'true'
Expand All @@ -32,7 +49,8 @@ def start_server(debug=False):
print("-" * 50)

try:
subprocess.run([sys.executable, "-m", "uvicorn", "main:app", "--host", "127.0.0.1", "--port", "8000", "--reload"])
# 修改host为0.0.0.0以在Docker容器中可访问
subprocess.run([sys.executable, "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"])
except KeyboardInterrupt:
print("\n服务已停止")

Expand All @@ -50,7 +68,12 @@ def start_server(debug=False):
print("❌ 请在项目根目录运行此脚本")
sys.exit(1)

# 安装依赖并启动服务
# 初始化数据库
init_database()

# 如果没有跳过安装,则安装依赖
if not args.skip_install:
install_dependencies()
start_server(debug=args.debug)

# 启动服务器
start_server(args.debug)