diff --git a/.gitignore b/.gitignore index a182268..150e95a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea .env .agent +app/data/app.db diff --git a/README.md b/README.md index 5cb3c9f..ab2eb6f 100644 --- a/README.md +++ b/README.md @@ -50,70 +50,32 @@ docker-compose up -d ## 💻 本地开发 -### 前置要求 - -- Python 3.13 -- Node.js 24 -- npm - -### 环境配置 - -创建 `api/.env` 文件配置环境变量: - -```env -# 数据库配置 -DATABASE_URL=sqlite+aiosqlite:///./data/app.db -# DATABASE_URL=postgresql+asyncpg://user:password@localhost:5432/dbname - -# JWT 配置 -SECRET_KEY=your-secret-key-here -ACCESS_TOKEN_EXPIRE_MINUTES=30 -REFRESH_TOKEN_EXPIRE_DAYS=7 -ALGORITHM=HS256 +```sh +# 启动(带日志) +docker compose -f docker-compose.dev.yml up ``` -### 后端设置 +更多命令 -```bash -cd api +```sh +# 后台启动 +docker compose -f docker-compose.dev.yml up -d -# 创建虚拟环境 -python -m venv .venv -source .venv/bin/activate # Windows: .venv\Scripts\activate +# 查看日志 +docker compose -f docker-compose.dev.yml logs -f -# 安装依赖 -pip install uv -uv sync - -# 运行数据库迁移 -alembic upgrade head +# 停止 +docker compose -f docker-compose.dev.yml down -# 启动开发服务器 -python main.py +# 重新构建(如果修改了依赖) +docker compose -f docker-compose.dev.yml up --build ``` -后端将在 http://localhost:8000 运行 - -### 前端设置 - -```bash -cd web - -# 安装依赖 -npm install - -# 启动开发服务器 -npm run dev -``` - -前端将在 http://localhost:5173 运行 - -## 📖 API 文档 +前端开发服务器:http://localhost:5173 -启动后端服务后,访问以下地址查看自动生成的 API 文档: +后端 API:http://localhost:8000 -- Swagger UI: http://localhost:8000/docs -- ReDoc: http://localhost:8000/redoc +API 文档:http://localhost:8000/docs ## 📚 数据库迁移 diff --git a/api/app/app.py b/api/app/app.py index 3cbd1fd..3b1a8bc 100644 --- a/api/app/app.py +++ b/api/app/app.py @@ -3,6 +3,7 @@ from pathlib import Path from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import FileResponse from fastapi.staticfiles import StaticFiles @@ -29,6 +30,15 @@ async def lifespan(app: FastAPI): app = FastAPI(lifespan=lifespan) +# 配置 CORS +app.add_middleware( + CORSMiddleware, + allow_origins=["http://localhost:5173"], + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + app.include_router(auth.router) app.include_router(users.router) app.include_router(workspaces.router) diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml new file mode 100644 index 0000000..7eb496e --- /dev/null +++ b/docker-compose.dev.yml @@ -0,0 +1,52 @@ +services: + # 后端服务 + backend: + image: python:3.13-slim + container_name: archivenote-backend-dev + working_dir: /app + ports: + - "8000:8000" + volumes: + - ./api:/app + - ./app/data:/app/data + environment: + - SECRET_KEY=dev-secret-key-change-in-production + # - DATABASE_URL=sqlite+aiosqlite:///./data/app.db + - DATABASE_URL=postgresql+asyncpg://postgres:postgres@postgres:5432/archivenote + command: > + bash -c "pip install uv && + uv sync && + uv run python -m alembic upgrade head && + uv run python -m uvicorn app.app:app --host 0.0.0.0 --port 8000 --reload" + restart: unless-stopped + + # 前端服务 + frontend: + image: node:24-alpine + container_name: archivenote-frontend-dev + working_dir: /app + ports: + - "5173:5173" + volumes: + - ./web:/app + - /app/node_modules + environment: + - VITE_API_BASE_URL=http://localhost:8000/api + command: sh -c "npm install && npm run dev -- --host" + restart: unless-stopped + + postgres: + image: postgres:17 + container_name: archivenote-postgres + restart: always + ports: + - 5432:5432 + environment: + POSTGRES_DB: archivenote + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + healthcheck: + test: ['CMD', 'pg_isready', '-U', 'postgres', '-d', 'archivenote'] + interval: 5s + timeout: 10s + retries: 5