OpenCode Deployer commited on
Commit ·
c73bf16
1
Parent(s): b3bc6dd
refactor: 分离 FastAPI 代码到独立文件
Browse files- 创建 main.py 文件,将 FastAPI 应用代码从 Dockerfile 中分离
- 使用标准 COPY 指令替代 heredoc 语法
- 添加新的 /info 端点展示服务信息
- 遵循 Docker 和 FastAPI 最佳实践
- 使用 --no-cache-dir 优化镜像大小
🎯 改进:
✅ 关注点分离:代码与构建配置分离
✅ 可维护性:独立文件便于编辑和版本控制
✅ 可读性:清晰的项目结构
✅ 最佳实践:遵循 FastAPI 官方推荐模式
- Dockerfile +2 -14
- main.py +20 -0
Dockerfile
CHANGED
|
@@ -35,21 +35,9 @@ RUN python3.12 -m venv /opt/venv \
|
|
| 35 |
ENV PATH="/opt/venv/bin:$PATH"
|
| 36 |
|
| 37 |
COPY requirements.txt .
|
| 38 |
-
RUN pip install -r requirements.txt
|
| 39 |
|
| 40 |
-
COPY
|
| 41 |
-
from fastapi import FastAPI
|
| 42 |
-
|
| 43 |
-
app = FastAPI(title="FastAPI 服务", version="1.0.0")
|
| 44 |
-
|
| 45 |
-
@app.get("/")
|
| 46 |
-
async def root():
|
| 47 |
-
return {"message": "Hello World"}
|
| 48 |
-
|
| 49 |
-
@app.get("/health")
|
| 50 |
-
async def health_check():
|
| 51 |
-
return {"status": "healthy"}
|
| 52 |
-
EOF
|
| 53 |
|
| 54 |
EXPOSE 7860
|
| 55 |
|
|
|
|
| 35 |
ENV PATH="/opt/venv/bin:$PATH"
|
| 36 |
|
| 37 |
COPY requirements.txt .
|
| 38 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 39 |
|
| 40 |
+
COPY main.py .
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
EXPOSE 7860
|
| 43 |
|
main.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
|
| 3 |
+
app = FastAPI(title="FastOC - FastAPI + Node.js + Python", version="1.0.0")
|
| 4 |
+
|
| 5 |
+
@app.get("/")
|
| 6 |
+
async def root():
|
| 7 |
+
return {"message": "Hello World from FastOC!", "tech_stack": "FastAPI + Node.js 22.x + Python 3.12"}
|
| 8 |
+
|
| 9 |
+
@app.get("/health")
|
| 10 |
+
async def health_check():
|
| 11 |
+
return {"status": "healthy", "service": "FastOC", "version": "1.0.0"}
|
| 12 |
+
|
| 13 |
+
@app.get("/info")
|
| 14 |
+
async def get_info():
|
| 15 |
+
return {
|
| 16 |
+
"service": "FastOC",
|
| 17 |
+
"description": "多语言开发环境",
|
| 18 |
+
"features": ["FastAPI", "Node.js 22.x", "Python 3.12"],
|
| 19 |
+
"endpoints": ["/", "/health", "/info"]
|
| 20 |
+
}
|