diff --git a/.gitignore b/.gitignore index 6bc2ebd..1152784 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .trae/documents/add_traefik_config_plan.md redis/data/dump.rdb redis/logs/redis-server.log +backend/auth/__pycache__/app.cpython-313.pyc diff --git a/backend/auth/Dockerfile b/backend/auth/Dockerfile deleted file mode 100644 index e69de29..0000000 diff --git a/backend/auth/app.py b/backend/auth/app.py new file mode 100644 index 0000000..cb4c96a --- /dev/null +++ b/backend/auth/app.py @@ -0,0 +1,18 @@ +from fastapi import FastAPI + +#实例化FastAPI +app = FastAPI() + +# 声明装饰器方法和路径 +@app.get("/") +# 声明装饰器函数 +async def home(): + return {"message": "Hello World"} + +# 如果使用命令行启动,使用uvicorn 文件名:app --reload启动即可,下面命令就不用写 +# 如果写下面的命令,就不需要命令行启动了,直接用IDE运行即可 +if __name__ == "__main__": + import uvicorn + import os + name = f"{os.path.splitext(os.path.basename(os.path.abspath(__file__)))[0]}:app" + uvicorn.run(name, host="0.0.0.0", port=8000) \ No newline at end of file diff --git a/backend/auth/dockerfile/.dockerignore b/backend/auth/dockerfile/.dockerignore new file mode 100644 index 0000000..de6b79f --- /dev/null +++ b/backend/auth/dockerfile/.dockerignore @@ -0,0 +1 @@ +../.venv/ \ No newline at end of file diff --git a/backend/auth/dockerfile/Dockerfile b/backend/auth/dockerfile/Dockerfile new file mode 100644 index 0000000..b9bc4c7 --- /dev/null +++ b/backend/auth/dockerfile/Dockerfile @@ -0,0 +1,12 @@ +FROM python:3.13-slim + +WORKDIR /app + +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +COPY .. . + +EXPOSE 8000 + +CMD ["python", "app.py"] diff --git a/backend/auth/requirements.txt b/backend/auth/requirements.txt new file mode 100644 index 0000000..886b969 --- /dev/null +++ b/backend/auth/requirements.txt @@ -0,0 +1,10 @@ +# FastAPI核心 +fastapi>=0.133.0 +uvicorn[standard]>=0.41.0 + +# PostgreSQL + SQLAlchemy ORM(包含异步支持) +sqlalchemy>=2.0.46 +asyncpg>=0.31.0 + +# Redis异步客户端 +redis>=7.2.0 \ No newline at end of file diff --git a/docker-compose/docker-compose.yml b/docker-compose/docker-compose.yml index 891b038..64c41aa 100644 --- a/docker-compose/docker-compose.yml +++ b/docker-compose/docker-compose.yml @@ -100,6 +100,44 @@ services: networks: - bs-network + # Auth 服务 + auth: + build: + context: ../backend/auth/dockerfile + dockerfile: Dockerfile + container_name: bs-auth + restart: always + ports: + - "${AUTH_PORT}:8000" + volumes: + # 挂载宿主机代码目录到容器,实现实时同步 + - ../backend/auth:/app + environment: + - DB_HOST=postgres + - DB_PORT=5432 + - DB_USER=${DB_USER} + - DB_PASSWORD=${DB_PASSWORD} + - DB_NAME=${DB_NAME} + - REDIS_HOST=redis + - REDIS_PORT=6379 + - REDIS_PASSWORD=${REDIS_PASSWORD} + depends_on: + - postgres + - redis + labels: + # Traefik 配置 + - "traefik.enable=true" + - "traefik.docker.network=bs-network" + - "traefik.http.routers.auth.rule=Host(`localhost`) && PathPrefix(`/auth`)" + - "traefik.http.services.auth.loadbalancer.server.port=8000" + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:8000/health"] + interval: 30s + timeout: 10s + retries: 3 + networks: + - bs-network + networks: bs-network: driver: bridge \ No newline at end of file