from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from .database import engine, Base from .routers import violations, debts, rewards, stats Base.metadata.create_all(bind=engine) app = FastAPI(title="自律公约执行系统") # CORS app.add_middleware( CORSMiddleware, allow_origins=["*"], # 生产环境应限制域名 allow_methods=["*"], allow_headers=["*"], ) app.include_router(violations.router, prefix="/api/violations", tags=["violations"]) app.include_router(debts.router, prefix="/api/debts", tags=["debts"]) app.include_router(rewards.router, prefix="/api/rewards", tags=["rewards"]) app.include_router(stats.router, prefix="/api/stats", tags=["stats"]) @app.get("/") def root(): return {"status": "ok"} @app.on_event("startup") def init_users(): db = next(get_db()) if not db.query(User).first(): db.add_all([ User(username="parent", hashed_password=pwd_context.hash("family2026"), role="parent"), User(username="child", hashed_password=pwd_context.hash("self2026"), role="child") ]) db.commit()