将框架从 Flask 迁移到 FastAPI
主要改动: - 更新依赖:使用 fastapi、uvicorn 替代 Flask - 数据库层:从 Flask-SQLAlchemy 迁移到纯 SQLAlchemy - 新增 database.py 管理数据库连接和会话 - 路由层:从 Blueprint 迁移到 APIRouter,添加 Pydantic 模型验证 - 应用层:使用 FastAPI 中间件替代 Flask 插件 - 启动方式:使用 uvicorn 替代 Flask 开发服务器 - 更新 Docker 配置以支持 FastAPI 优势: - 更高的性能和异步支持 - 自动生成 OpenAPI 文档 - 更好的类型安全和数据验证 - 所有 API 端点保持向后兼容 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -5,16 +5,26 @@
|
||||
用于创建管理员账户
|
||||
"""
|
||||
|
||||
from backend.app import create_app
|
||||
from backend.models import db, User
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 将backend目录添加到Python路径
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'backend'))
|
||||
|
||||
from database import SessionLocal, init_db
|
||||
from models import User
|
||||
|
||||
def create_initial_user():
|
||||
"""创建初始用户"""
|
||||
app = create_app()
|
||||
# 初始化数据库
|
||||
init_db()
|
||||
|
||||
with app.app_context():
|
||||
# 创建会话
|
||||
db = SessionLocal()
|
||||
|
||||
try:
|
||||
# 检查是否已有用户
|
||||
existing_user = User.query.first()
|
||||
existing_user = db.query(User).first()
|
||||
|
||||
if existing_user:
|
||||
print(f"用户已存在: {existing_user.username}")
|
||||
@@ -27,12 +37,14 @@ def create_initial_user():
|
||||
user = User(username=username)
|
||||
user.set_password(password)
|
||||
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
db.add(user)
|
||||
db.commit()
|
||||
|
||||
print(f"用户创建成功!")
|
||||
print(f"用户名: {username}")
|
||||
print(f"请妥善保管密码!")
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
create_initial_user()
|
||||
|
||||
Reference in New Issue
Block a user