diff --git a/backend/app/__init__.py b/backend/app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/app/config.py b/backend/app/config.py new file mode 100644 index 0000000..ab4e5f2 --- /dev/null +++ b/backend/app/config.py @@ -0,0 +1,18 @@ +"""应用配置 — 从环境变量 / .env 读取。""" +from pydantic_settings import BaseSettings + + +class Settings(BaseSettings): + database_url: str = "postgresql+asyncpg://hive_admin:changeme@localhost:5432/hive_manage_v3" + jwt_secret: str = "change-me" + jwt_algorithm: str = "HS256" + jwt_expire_minutes: int = 1440 + hermes_cli_path: str = "/home/enzo/.local/bin/hermes" + hermes_profiles_root: str = "/home/enzo/.hermes/profiles" + admin_email: str = "admin@zhiwei.local" + admin_password: str = "admin123" + + model_config = {"env_file": ".env"} + + +settings = Settings() diff --git a/backend/app/database.py b/backend/app/database.py new file mode 100644 index 0000000..f86c5f6 --- /dev/null +++ b/backend/app/database.py @@ -0,0 +1,17 @@ +"""异步 SQLAlchemy 引擎 + session 工厂。""" +from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession +from sqlalchemy.orm import DeclarativeBase + +from app.config import settings + +engine = create_async_engine(settings.database_url, echo=False) +async_session = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False) + + +class Base(DeclarativeBase): + pass + + +async def get_db() -> AsyncSession: + async with async_session() as session: + yield session diff --git a/backend/app/main.py b/backend/app/main.py new file mode 100644 index 0000000..4f8c78f --- /dev/null +++ b/backend/app/main.py @@ -0,0 +1,34 @@ +"""FastAPI 应用入口。""" +from contextlib import asynccontextmanager +from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware + +from app.database import engine, Base + + +@asynccontextmanager +async def lifespan(app: FastAPI): + async with engine.begin() as conn: + await conn.run_sync(Base.metadata.create_all) + yield + await engine.dispose() + + +app = FastAPI( + title="Hive Manage v3", + version="0.1.0", + lifespan=lifespan, +) + +app.add_middleware( + CORSMiddleware, + allow_origins=["*"], + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + + +@app.get("/api/v1/health") +async def health_check(): + return {"status": "ok"}