Songyou commited on
Commit
98b1ab2
·
1 Parent(s): 36f8047
Files changed (3) hide show
  1. Dockerfile +7 -8
  2. static/index.html → index.html +0 -0
  3. main.py +9 -27
Dockerfile CHANGED
@@ -2,17 +2,16 @@
2
  FROM python:3.8-slim
3
 
4
  # 设置工作目录
5
- RUN useradd -m -u 1000 user
6
  WORKDIR /app
7
 
8
- COPY --chown=user ./requirements.txt requirements.txt
9
- RUN pip install --no-cache-dir --upgrade -r requirements.txt
10
 
11
- COPY --chown=user . /app
 
12
 
13
- USER user
 
14
 
15
- ENV HOME=/home/user \
16
- PATH=/home/user/.local/bin:$PATH
17
  # 启动 FastAPI 应用
18
- CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
 
2
  FROM python:3.8-slim
3
 
4
  # 设置工作目录
 
5
  WORKDIR /app
6
 
7
+ # 复制当前目录中的所有文件到容器内
8
+ COPY . /app
9
 
10
+ # 安装应用的依赖
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
 
13
+ # 开放端口 8000
14
+ EXPOSE 8000
15
 
 
 
16
  # 启动 FastAPI 应用
17
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
static/index.html → index.html RENAMED
File without changes
main.py CHANGED
@@ -1,38 +1,20 @@
1
  from fastapi import FastAPI
2
- from pydantic import BaseModel
3
  from fastapi.responses import JSONResponse, HTMLResponse
4
- from fastapi.staticfiles import StaticFiles
5
- from fastapi.responses import FileResponse
6
- from fastapi.middleware.cors import CORSMiddleware
7
  import logging
8
- # 创建 FastAPI 实例
9
  app = FastAPI()
10
- # 设置日志配置
11
- logging.basicConfig(level=logging.INFO)
12
- logger = logging.getLogger(__name__)
13
- # 请求体的模型
14
- class UserInput(BaseModel):
15
- user_input: str
16
 
17
- app.mount("/", StaticFiles(directory="static", html=True), name="static")
 
 
 
 
18
 
19
 
20
- # 添加 CORS 中间件
21
- app.add_middleware(
22
- CORSMiddleware,
23
- allow_origins=["*"], # 允许所有来源访问,或者你可以根据需要限制为特定来源
24
- allow_credentials=True,
25
- allow_methods=["*"], # 允许所有 HTTP 方法
26
- allow_headers=["*"], # 允许所有头部
27
- )
28
-
29
- @app.get("/")
30
- def index() -> FileResponse:
31
- return FileResponse(path="/app/static/index.html", media_type="text/html")
32
-
33
  @app.post("/submit")
34
- async def submit_input(input_data: UserInput):
35
  # 处理用户输入并返回响应
36
- logger.info("input coming")
37
 
38
  return JSONResponse(content={"message": input_data.user_input})
 
 
 
1
  from fastapi import FastAPI
 
2
  from fastapi.responses import JSONResponse, HTMLResponse
 
 
 
3
  import logging
 
4
  app = FastAPI()
 
 
 
 
 
 
5
 
6
+ @app.get("/", response_class=HTMLResponse)
7
+ async def read_root():
8
+ # 提供前端 HTML 文件
9
+ with open("index.html", "r") as file:
10
+ return file.read()
11
 
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  @app.post("/submit")
14
+ async def submit_input(input_data: str):
15
  # 处理用户输入并返回响应
16
+ logging.info("input coming")
17
 
18
  return JSONResponse(content={"message": input_data.user_input})
19
+
20
+