BookSystem/backend/auth/main.py
jayhgq f1da855793 feat(auth): 实现用户认证与授权功能
- 添加用户登录、登出及令牌管理功能
- 实现基于JWT和Redis的认证系统
- 完善用户和角色管理API
- 添加密码加密与验证功能
- 配置数据库连接和Redis客户端
- 实现中间件进行权限验证
- 初始化管理员和测试用户数据
- 更新模型和接口文档
- 添加测试API和配置文件
2026-02-28 18:08:53 +08:00

51 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from fastapi import FastAPI
from contextlib import asynccontextmanager
from apps.urls import api_router
from database import engine, get_db
from models import Base
from init_data import init_all_data
@asynccontextmanager
async def lifespan(app: FastAPI):
# 启动时初始化数据库
async with engine.begin() as conn:
# 创建所有表
await conn.run_sync(Base.metadata.create_all)
# 初始化数据
async for db in get_db():
await init_all_data(db)
break
yield
# 关闭时的清理工作
await engine.dispose()
# 实例化FastAPI
app = FastAPI(
title="Book System API",
description="图书系统API",
version="1.0.0",
lifespan=lifespan
)
# 包含路由
app.include_router(api_router)
# 声明装饰器方法和路径
@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, reload=True, reload_dirs=["_"])