xiaoyukkkk commited on
Commit
8cc0fda
·
unverified ·
1 Parent(s): 1107f10

Upload 10 files

Browse files
Files changed (8) hide show
  1. .gitignore +1 -1
  2. Dockerfile +62 -31
  3. LICENSE +1 -1
  4. docker-compose.yml +14 -0
  5. main.py +5 -0
  6. requirements.txt +2 -2
  7. setup.bat +185 -0
  8. setup.sh +172 -0
.gitignore CHANGED
@@ -44,4 +44,4 @@ static/
44
  # OS
45
  .DS_Store
46
  Thumbs.db
47
- old_version.py
 
44
  # OS
45
  .DS_Store
46
  Thumbs.db
47
+ old_version.py
Dockerfile CHANGED
@@ -1,31 +1,62 @@
1
- FROM python:3.11-slim
2
- WORKDIR /app
3
-
4
- ENV PYTHONDONTWRITEBYTECODE=1 \
5
- PYTHONUNBUFFERED=1
6
-
7
- # Install Python dependencies
8
- COPY requirements.txt .
9
- RUN apt-get update && apt-get install -y --no-install-recommends \
10
- gcc \
11
- && pip install --no-cache-dir -r requirements.txt \
12
- && apt-get purge -y gcc \
13
- && apt-get autoremove -y \
14
- && rm -rf /var/lib/apt/lists/*
15
-
16
- # Copy backend code
17
- COPY main.py .
18
- COPY core ./core
19
- COPY util ./util
20
-
21
- # Copy prebuilt static assets
22
- COPY static ./static
23
-
24
- # Create data directory (local + HF Spaces Pro)
25
- RUN mkdir -p ./data
26
-
27
- # Declare data volume
28
- VOLUME ["/app/data"]
29
-
30
- # Start service
31
- CMD ["python", "-u", "main.py"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Stage 1: 构建前端
2
+ FROM node:20-slim AS frontend-builder
3
+ WORKDIR /app/frontend
4
+
5
+ # 先复制 package 文件利用 Docker 缓存
6
+ COPY frontend/package.json frontend/package-lock.json ./
7
+ RUN npm install --silent
8
+
9
+ # 复制前端源码并构建
10
+ COPY frontend/ ./
11
+ RUN npm run build
12
+
13
+ # Stage 2: 最终运行时镜像
14
+ FROM python:3.11-slim
15
+ WORKDIR /app
16
+
17
+ ENV PYTHONDONTWRITEBYTECODE=1 \
18
+ PYTHONUNBUFFERED=1 \
19
+ TZ=Asia/Shanghai
20
+
21
+ # 安装 Python 依赖和浏览器依赖(合并为单一 RUN 指令以减少层数)
22
+ COPY requirements.txt .
23
+ RUN apt-get update && \
24
+ apt-get install -y --no-install-recommends \
25
+ gcc \
26
+ curl \
27
+ tzdata \
28
+ chromium chromium-driver \
29
+ dbus dbus-x11 \
30
+ libglib2.0-0 libnss3 libnspr4 libatk1.0-0 libatk-bridge2.0-0 \
31
+ libcups2 libdrm2 libxkbcommon0 libxcomposite1 libxdamage1 \
32
+ libxfixes3 libxrandr2 libgbm1 libasound2 libpango-1.0-0 \
33
+ libcairo2 fonts-liberation fonts-noto-cjk && \
34
+ ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \
35
+ pip install --no-cache-dir -r requirements.txt && \
36
+ apt-get purge -y gcc && \
37
+ apt-get autoremove -y && \
38
+ rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
39
+
40
+ # 复制后端代码
41
+ COPY main.py .
42
+ COPY core ./core
43
+ COPY util ./util
44
+
45
+ # 从 builder 阶段只复制构建好的静态文件
46
+ COPY --from=frontend-builder /app/static ./static
47
+
48
+ # 创建数据目录
49
+ RUN mkdir -p ./data
50
+
51
+ # 声明数据卷
52
+ VOLUME ["/app/data"]
53
+
54
+ # 声明端口
55
+ EXPOSE 7860
56
+
57
+ # 健康检查
58
+ HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
59
+ CMD curl -f http://localhost:7860/admin/health || exit 1
60
+
61
+ # 启动服务
62
+ CMD ["python", "-u", "main.py"]
LICENSE CHANGED
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
 
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
docker-compose.yml CHANGED
@@ -1,6 +1,7 @@
1
  services:
2
  gemini-api:
3
  build: .
 
4
  ports:
5
  - "7860:7860"
6
  volumes:
@@ -8,3 +9,16 @@ services:
8
  env_file:
9
  - .env
10
  restart: unless-stopped
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  services:
2
  gemini-api:
3
  build: .
4
+ container_name: gemini-business2api
5
  ports:
6
  - "7860:7860"
7
  volumes:
 
9
  env_file:
10
  - .env
11
  restart: unless-stopped
12
+ # 健康检查
13
+ healthcheck:
14
+ test: ["CMD", "curl", "-f", "http://localhost:7860/admin/health"]
15
+ interval: 30s
16
+ timeout: 10s
17
+ retries: 3
18
+ start_period: 10s
19
+ # 日志限制
20
+ logging:
21
+ driver: json-file
22
+ options:
23
+ max-size: "10m"
24
+ max-file: "3"
main.py CHANGED
@@ -428,6 +428,11 @@ async def serve_logo():
428
  return FileResponse(logo_path)
429
  raise HTTPException(404, "Not Found")
430
 
 
 
 
 
 
431
  # ---------- Session 中间件配置 ----------
432
  from starlette.middleware.sessions import SessionMiddleware
433
  app.add_middleware(
 
428
  return FileResponse(logo_path)
429
  raise HTTPException(404, "Not Found")
430
 
431
+ @app.get("/admin/health")
432
+ async def health_check():
433
+ """健康检查端点,用于 Docker HEALTHCHECK"""
434
+ return {"status": "ok"}
435
+
436
  # ---------- Session 中间件配置 ----------
437
  from starlette.middleware.sessions import SessionMiddleware
438
  app.add_middleware(
requirements.txt CHANGED
@@ -1,6 +1,6 @@
1
  fastapi==0.110.0
2
  uvicorn[standard]==0.29.0
3
- httpx==0.27.0
4
  pydantic==2.7.0
5
  aiofiles==24.1.0
6
  python-dotenv==1.0.1
@@ -8,7 +8,7 @@ itsdangerous==2.1.2
8
  python-multipart==0.0.6
9
  pyyaml>=6.0
10
  jinja2>=3.1.0
11
- requests==2.32.3
12
  DrissionPage==4.0.5.6
13
  undetected-chromedriver>=3.5.5
14
  selenium>=4.15.0
 
1
  fastapi==0.110.0
2
  uvicorn[standard]==0.29.0
3
+ httpx[socks]==0.27.0
4
  pydantic==2.7.0
5
  aiofiles==24.1.0
6
  python-dotenv==1.0.1
 
8
  python-multipart==0.0.6
9
  pyyaml>=6.0
10
  jinja2>=3.1.0
11
+ requests[socks]==2.32.3
12
  DrissionPage==4.0.5.6
13
  undetected-chromedriver>=3.5.5
14
  selenium>=4.15.0
setup.bat ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @echo off
2
+ REM Gemini Business2API Setup Script
3
+ REM Handles both installation and updates automatically
4
+ REM Uses uv for Python environment management
5
+ REM Usage: setup.bat
6
+
7
+ setlocal enabledelayedexpansion
8
+
9
+ echo ==========================================
10
+ echo Gemini Business2API Setup Script
11
+ echo ==========================================
12
+ echo.
13
+
14
+ REM Color codes for output (using echo instead of ANSI codes for better Windows compatibility)
15
+ set GREEN=[92m
16
+ set RED=[91m
17
+ set YELLOW=[93m
18
+ set BLUE=[94m
19
+ set NC=[0m
20
+
21
+ REM Function to print colored messages (simplified for Windows)
22
+ set "PRINT_SUCCESS=echo [SUCCESS]"
23
+ set "PRINT_ERROR=echo [ERROR]"
24
+ set "PRINT_INFO=echo [INFO]"
25
+ set "PRINT_STEP=echo [STEP]"
26
+
27
+ REM Check if git is installed
28
+ where git >nul 2>nul
29
+ if %errorlevel% neq 0 (
30
+ echo [ERROR] Git is not installed. Please install git first.
31
+ exit /b 1
32
+ )
33
+
34
+ REM Step 1: Install or update uv
35
+ echo [STEP] Step 1: Installing/Updating uv...
36
+
37
+ where uv >nul 2>nul
38
+ if %errorlevel% neq 0 (
39
+ echo [INFO] uv not found, installing...
40
+ REM Install uv using pipx or pip
41
+ pipx install uv 2>nul
42
+ if %errorlevel% neq 0 (
43
+ pip install --user uv 2>nul
44
+ if %errorlevel% neq 0 (
45
+ REM Fallback: download and install uv binary
46
+ curl -LsSf https://astral.sh/uv/install.bat | cmd
47
+ )
48
+ )
49
+ if %errorlevel% equ 0 (
50
+ echo [SUCCESS] uv installed successfully
51
+ ) else (
52
+ echo [ERROR] Failed to install uv
53
+ exit /b 1
54
+ )
55
+ ) else (
56
+ echo [INFO] Updating uv to latest version...
57
+ uv pip install --upgrade uv
58
+ echo [SUCCESS] uv updated
59
+ )
60
+ echo.
61
+
62
+ REM Step 2: Ensure Python 3.11 is available
63
+ echo [STEP] Step 2: Ensuring Python 3.11 is available...
64
+ uv python list | findstr /C:"3.11" >nul
65
+ if %errorlevel% neq 0 (
66
+ echo [INFO] Python 3.11 not found, installing...
67
+ uv python install 3.11
68
+ if %errorlevel% neq 0 (
69
+ echo [ERROR] Failed to install Python 3.11
70
+ exit /b 1
71
+ )
72
+ echo [SUCCESS] Python 3.11 installed
73
+ ) else (
74
+ echo [SUCCESS] Python 3.11 is already available
75
+ )
76
+ echo.
77
+
78
+ REM Step 3: Pull latest code from git
79
+ echo [STEP] Step 3: Syncing code from repository...
80
+ echo [INFO] Fetching latest changes...
81
+ git fetch origin
82
+
83
+ echo [INFO] Pulling latest code...
84
+ git pull origin main 2>nul || git pull origin master 2>nul
85
+ if %errorlevel% equ 0 (
86
+ echo [SUCCESS] Code synchronized successfully
87
+ ) else (
88
+ echo [INFO] No remote changes to pull
89
+ )
90
+ echo.
91
+
92
+ REM Step 4: Setup .env file if it doesn't exist
93
+ echo [STEP] Step 4: Checking configuration...
94
+ if exist .env (
95
+ echo [INFO] .env file exists
96
+ ) else (
97
+ if exist .env.example (
98
+ copy .env.example .env >nul
99
+ echo [SUCCESS] .env file created from .env.example
100
+ echo [INFO] Please edit .env and configure your ADMIN_KEY
101
+ ) else (
102
+ echo [ERROR] .env.example not found
103
+ exit /b 1
104
+ )
105
+ )
106
+ echo.
107
+
108
+ REM Step 5: Setup Python virtual environment
109
+ echo [STEP] Step 5: Setting up Python environment...
110
+ if exist .venv (
111
+ echo [INFO] Virtual environment already exists
112
+ ) else (
113
+ echo [INFO] Creating virtual environment with Python 3.11...
114
+ uv venv --python 3.11 .venv
115
+ if %errorlevel% neq 0 (
116
+ echo [ERROR] Failed to create virtual environment
117
+ exit /b 1
118
+ )
119
+ echo [SUCCESS] Virtual environment created
120
+ )
121
+ echo.
122
+
123
+ REM Step 6: Install/Update Python dependencies
124
+ echo [STEP] Step 6: Installing Python dependencies...
125
+ echo [INFO] Using uv to install dependencies (this may take a moment)...
126
+ .venv\Scripts\python.exe -m pip install --upgrade pip --quiet
127
+ uv pip install -r requirements.txt
128
+ if %errorlevel% neq 0 (
129
+ echo [ERROR] Failed to install Python dependencies
130
+ exit /b 1
131
+ )
132
+ echo [SUCCESS] Python dependencies installed
133
+ echo.
134
+
135
+ REM Step 7: Setup frontend
136
+ echo [STEP] Step 7: Setting up frontend...
137
+ if exist frontend (
138
+ cd frontend
139
+
140
+ REM Check if npm is installed
141
+ where npm >nul 2>nul
142
+ if %errorlevel% equ 0 (
143
+ echo [INFO] Installing dependencies...
144
+ npm install
145
+
146
+ echo [INFO] Building frontend...
147
+ npm run build
148
+ echo [SUCCESS] Frontend built successfully
149
+ ) else (
150
+ echo [ERROR] npm is not installed. Please install Node.js and npm first.
151
+ cd ..
152
+ exit /b 1
153
+ )
154
+
155
+ cd ..
156
+ ) else (
157
+ echo [ERROR] Frontend directory not found. Are you in the project root?
158
+ exit /b 1
159
+ )
160
+ echo.
161
+
162
+ REM Step 8: Show completion message
163
+ echo ==========================================
164
+ echo [SUCCESS] Setup completed successfully!
165
+ echo ==========================================
166
+ echo.
167
+
168
+ if exist .env (
169
+ echo [INFO] Next steps:
170
+ echo.
171
+ echo 1. Edit .env file if needed:
172
+ echo notepad .env
173
+ echo.
174
+ echo 2. Start the service:
175
+ echo .venv\Scripts\python.exe main.py
176
+ echo.
177
+ echo 3. Access the admin panel:
178
+ echo http://localhost:7860/
179
+ echo.
180
+ echo [INFO] To activate virtual environment later, run:
181
+ echo .venv\Scripts\activate.bat
182
+ )
183
+ echo.
184
+
185
+ endlocal
setup.sh ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # Gemini Business2API Setup Script
4
+ # Handles both installation and updates automatically
5
+ # Uses uv for Python environment management
6
+ # Usage: ./setup.sh
7
+
8
+ set -e # Exit on error
9
+
10
+ echo "=========================================="
11
+ echo "Gemini Business2API Setup Script"
12
+ echo "=========================================="
13
+ echo ""
14
+
15
+ # Color codes for output
16
+ RED='\033[0;31m'
17
+ GREEN='\033[0;32m'
18
+ YELLOW='\033[1;33m'
19
+ BLUE='\033[0;34m'
20
+ NC='\033[0m' # No Color
21
+
22
+ # Function to print colored messages
23
+ print_success() {
24
+ echo -e "${GREEN}✓ $1${NC}"
25
+ }
26
+
27
+ print_error() {
28
+ echo -e "${RED}✗ $1${NC}"
29
+ }
30
+
31
+ print_info() {
32
+ echo -e "${YELLOW}→ $1${NC}"
33
+ }
34
+
35
+ print_step() {
36
+ echo -e "${BLUE}[STEP] $1${NC}"
37
+ }
38
+
39
+ # Check if git is installed
40
+ if ! command -v git &> /dev/null; then
41
+ print_error "Git is not installed. Please install git first."
42
+ exit 1
43
+ fi
44
+
45
+ # Step 1: Install or update uv
46
+ print_step "Step 1: Installing/Updating uv..."
47
+ if ! command -v uv &> /dev/null; then
48
+ print_info "uv not found, installing..."
49
+ # Install uv using pipx or pip
50
+ if command -v pipx &> /dev/null; then
51
+ pipx install uv
52
+ elif command -v pip &> /dev/null; then
53
+ pip install --user uv
54
+ else
55
+ # Fallback: download and install uv binary
56
+ curl -LsSf https://astral.sh/uv/install.sh | sh
57
+ export PATH="$HOME/.local/bin:$PATH"
58
+ fi
59
+ print_success "uv installed successfully"
60
+ else
61
+ print_info "Updating uv to latest version..."
62
+ uv pip install --upgrade uv
63
+ print_success "uv updated"
64
+ fi
65
+ echo ""
66
+
67
+ # Step 2: Ensure Python 3.11 is available
68
+ print_step "Step 2: Ensuring Python 3.11 is available..."
69
+ if ! uv python list | grep -q "3.11"; then
70
+ print_info "Python 3.11 not found, installing..."
71
+ uv python install 3.11
72
+ print_success "Python 3.11 installed"
73
+ else
74
+ print_success "Python 3.11 is already available"
75
+ fi
76
+ echo ""
77
+
78
+ # Step 3: Pull latest code from git
79
+ print_step "Step 3: Syncing code from repository..."
80
+ print_info "Fetching latest changes..."
81
+ git fetch origin
82
+
83
+ print_info "Pulling latest code..."
84
+ if git pull origin main 2>/dev/null || git pull origin master 2>/dev/null; then
85
+ print_success "Code synchronized successfully"
86
+ else
87
+ print_info "No remote changes to pull"
88
+ fi
89
+ echo ""
90
+
91
+ # Step 4: Setup .env file if it doesn't exist
92
+ print_step "Step 4: Checking configuration..."
93
+ if [ -f ".env" ]; then
94
+ print_info ".env file exists"
95
+ else
96
+ if [ -f ".env.example" ]; then
97
+ cp .env.example .env
98
+ print_success ".env file created from .env.example"
99
+ print_info "Please edit .env and configure your ADMIN_KEY"
100
+ else
101
+ print_error ".env.example not found"
102
+ exit 1
103
+ fi
104
+ fi
105
+ echo ""
106
+
107
+ # Step 5: Setup Python virtual environment
108
+ print_step "Step 5: Setting up Python environment..."
109
+ if [ -d ".venv" ]; then
110
+ print_info "Virtual environment already exists"
111
+ else
112
+ print_info "Creating virtual environment with Python 3.11..."
113
+ uv venv --python 3.11 .venv
114
+ print_success "Virtual environment created"
115
+ fi
116
+ echo ""
117
+
118
+ # Step 6: Install/Update Python dependencies
119
+ print_step "Step 6: Installing Python dependencies..."
120
+ print_info "Using uv to install dependencies (this may take a moment)..."
121
+ uv pip install --python .venv/bin/python -r requirements.txt --system
122
+ print_success "Python dependencies installed"
123
+ echo ""
124
+
125
+ # Step 7: Setup frontend
126
+ print_step "Step 7: Setting up frontend..."
127
+ if [ -d "frontend" ]; then
128
+ cd frontend
129
+
130
+ # Check if npm is installed
131
+ if command -v npm &> /dev/null; then
132
+ print_info "Installing dependencies..."
133
+ npm install
134
+
135
+ print_info "Building frontend..."
136
+ npm run build
137
+ print_success "Frontend built successfully"
138
+ else
139
+ print_error "npm is not installed. Please install Node.js and npm first."
140
+ cd ..
141
+ exit 1
142
+ fi
143
+
144
+ cd ..
145
+ else
146
+ print_error "Frontend directory not found. Are you in the project root?"
147
+ exit 1
148
+ fi
149
+ echo ""
150
+
151
+ # Step 8: Show completion message
152
+ echo "=========================================="
153
+ print_success "Setup completed successfully!"
154
+ echo "=========================================="
155
+ echo ""
156
+
157
+ if [ -f ".env" ]; then
158
+ print_info "Next steps:"
159
+ echo ""
160
+ echo " 1. Edit .env file if needed:"
161
+ echo " ${BLUE}nano .env${NC} or ${BLUE}vim .env${NC}"
162
+ echo ""
163
+ echo " 2. Start the service:"
164
+ echo " ${BLUE}uv run python main.py${NC}"
165
+ echo ""
166
+ echo " 3. Access the admin panel:"
167
+ echo " ${BLUE}http://localhost:7860/${NC}"
168
+ echo ""
169
+ print_info "To activate virtual environment later, run:"
170
+ echo " ${BLUE}source .venv/bin/activate${NC}"
171
+ fi
172
+ echo ""