commit 57245cc288fd104a7fb65b9e707a9774ea8329f7 Author: bwstudio Date: Thu May 21 20:39:14 2026 +0800 feat(api): project initialization diff --git a/api/app/__init__.py b/api/app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/app/config.py b/api/app/config.py new file mode 100644 index 0000000..43b9cd4 --- /dev/null +++ b/api/app/config.py @@ -0,0 +1,13 @@ +import os + +# Database configuration +DATABASE_URL = "sqlite:///./joke.db" + +# JWT configuration +JWT_SECRET_KEY = os.getenv("JWT_SECRET_KEY", "your-secret-key-change-in-production") +JWT_ALGORITHM = "HS256" +JWT_EXPIRATION_HOURS = 24 + +# API configuration +API_TITLE = "笑话大全 API" +API_VERSION = "1.0.0" \ No newline at end of file diff --git a/api/app/database.py b/api/app/database.py new file mode 100644 index 0000000..3a5deb0 --- /dev/null +++ b/api/app/database.py @@ -0,0 +1,26 @@ +from sqlalchemy import create_engine +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker + +from app.config import DATABASE_URL + +# Create engine +engine = create_engine( + DATABASE_URL, + connect_args={"check_same_thread": False} +) + +# Create SessionLocal class +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) + +# Create Base class +Base = declarative_base() + + +def get_db(): + """Dependency to get database session.""" + db = SessionLocal() + try: + yield db + finally: + db.close() \ No newline at end of file diff --git a/api/main.py b/api/main.py new file mode 100644 index 0000000..2e6bc8c --- /dev/null +++ b/api/main.py @@ -0,0 +1,26 @@ +from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware + +from app.config import API_TITLE, API_VERSION + +# Create FastAPI application +app = FastAPI(title=API_TITLE, version=API_VERSION) + +# Configure CORS +app.add_middleware( + CORSMiddleware, + allow_origins=["*"], + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + + +@app.get("/") +def root(): + return {"message": "欢迎使用笑话大全 API"} + + +@app.get("/health") +def health_check(): + return {"status": "healthy"} \ No newline at end of file diff --git a/api/requirements.txt b/api/requirements.txt new file mode 100644 index 0000000..db02c77 --- /dev/null +++ b/api/requirements.txt @@ -0,0 +1,7 @@ +fastapi==0.109.0 +uvicorn[standard]==0.27.0 +sqlalchemy==2.0.25 +pydantic==2.5.3 +python-jose[cryptography]==3.3.0 +passlib[bcrypt]==1.7.4 +python-multipart==0.0.6 \ No newline at end of file