tanbushi commited on
Commit
edb0bce
·
1 Parent(s): b8b4353
Files changed (2) hide show
  1. Dockerfile +3 -12
  2. main.py +34 -0
Dockerfile CHANGED
@@ -7,9 +7,6 @@ RUN apt-get update && apt-get install -y git curl cron && rm -rf /var/lib/apt/li
7
  # ---------- 3. 跳过用户创建,使用 root 用户 ----------
8
  # 直接使用 root 用户运行所有服务
9
 
10
- # ---------- 4. 设置环境变量 ----------
11
- ENV EXPECTED_STARTUP_PASSWORD=startup_secret_123
12
-
13
  # ---------- 5. 全局安装 opencode-ai ----------
14
  # 安装完先验证二进制是否存在,若不存在就手动解压
15
  RUN npm install -g opencode-ai@latest && \
@@ -27,18 +24,12 @@ RUN if [ -f package.json ]; then npm ci --only=production; fi
27
  # ---------- 8. 启动脚本 ----------
28
  RUN mkdir -p /usr/local/bin
29
  RUN printf '#!/bin/sh\n\
30
- echo "Starting FastAPI Web Server ..."\n\
31
- # 检查启动密码\n\
32
- if [ "$STARTUP_PASSWORD" != "$EXPECTED_STARTUP_PASSWORD" ]; then\n\
33
- echo "Error: Invalid startup password"\n\
34
- exit 1\n\
35
- fi\n\
36
- echo "Password verified, starting services..."\n\
37
  # 启动 cron 服务\n\
38
  cron && \
39
  echo "Cron started successfully"\n\
40
- # 启动 FastAPI 服务\n\
41
- exec python /app/main.py\n' > /usr/local/bin/start.sh && \
42
  chmod +x /usr/local/bin/start.sh
43
 
44
  # ---------- 9. 端口与健康检查 ----------
 
7
  # ---------- 3. 跳过用户创建,使用 root 用户 ----------
8
  # 直接使用 root 用户运行所有服务
9
 
 
 
 
10
  # ---------- 5. 全局安装 opencode-ai ----------
11
  # 安装完先验证二进制是否存在,若不存在就手动解压
12
  RUN npm install -g opencode-ai@latest && \
 
24
  # ---------- 8. 启动脚本 ----------
25
  RUN mkdir -p /usr/local/bin
26
  RUN printf '#!/bin/sh\n\
27
+ echo "Starting OpenCode AI Web Server ..."\n\
 
 
 
 
 
 
28
  # 启动 cron 服务\n\
29
  cron && \
30
  echo "Cron started successfully"\n\
31
+ # 启动 opencode 服务\n\
32
+ exec /usr/local/bin/opencode serve --hostname 0.0.0.0 --port 7860\n' > /usr/local/bin/start.sh && \
33
  chmod +x /usr/local/bin/start.sh
34
 
35
  # ---------- 9. 端口与健康检查 ----------
main.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException, Depends
2
+ from fastapi.responses import JSONResponse
3
+ import os
4
+
5
+ app = FastAPI(title="OpenCode API")
6
+
7
+ # 从环境变量获取预期的 token
8
+ EXPECTED_TOKEN = os.getenv("ACCESS_TOKEN", "default_token")
9
+
10
+ # Token 认证依赖
11
+ def verify_token(access_token: str):
12
+ if access_token != EXPECTED_TOKEN:
13
+ raise HTTPException(status_code=401, detail="Invalid access token")
14
+ return access_token
15
+
16
+ @app.get("/")
17
+ async def root():
18
+ return {"message": "OpenCode API is running"}
19
+
20
+ @app.get("/health")
21
+ async def health_check():
22
+ return {"status": "healthy"}
23
+
24
+ @app.get("/{access_token}/api/data")
25
+ async def get_data(access_token: str = Depends(verify_token)):
26
+ return {"data": "This is protected data"}
27
+
28
+ @app.get("/{access_token}/api/info")
29
+ async def get_info(access_token: str = Depends(verify_token)):
30
+ return {"info": "Protected information", "token": access_token}
31
+
32
+ @app.get("/{access_token}/")
33
+ async def protected_root(access_token: str = Depends(verify_token)):
34
+ return {"message": "Welcome to protected area"}