做了文件结构的调整,新建了service文件夹存放内部服务。添加了git ignore
This commit is contained in:
11
services/auth.py
Normal file
11
services/auth.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from passlib.context import CryptContext
|
||||
|
||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
|
||||
def get_password_hash(password: str) -> str:
|
||||
"""Generate password hash using bcrypt"""
|
||||
return pwd_context.hash(password)
|
||||
|
||||
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
||||
"""Verify password against stored hash"""
|
||||
return pwd_context.verify(plain_password, hashed_password)
|
26
services/init_db.py
Normal file
26
services/init_db.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from sqlalchemy.ext.asyncio import AsyncEngine
|
||||
from ..models.user import Base, User, UserRole
|
||||
from sqlalchemy import select
|
||||
from config import SYSTEM_ADMIN_CONFIG
|
||||
from services.auth import get_password_hash
|
||||
|
||||
async def init_db(engine: AsyncEngine):
|
||||
"""Initialize database"""
|
||||
async with engine.begin() as conn:
|
||||
# Create all tables
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
|
||||
# Check if system admin exists
|
||||
result = await conn.execute(
|
||||
select(User).where(User.role == UserRole.SYSTEM_ADMIN)
|
||||
)
|
||||
if not result.scalars().first():
|
||||
# Create default system admin
|
||||
admin = User(
|
||||
username=SYSTEM_ADMIN_CONFIG['username'],
|
||||
password=get_password_hash(SYSTEM_ADMIN_CONFIG['password']),
|
||||
role=UserRole.SYSTEM_ADMIN,
|
||||
description=SYSTEM_ADMIN_CONFIG['description']
|
||||
)
|
||||
conn.add(admin)
|
||||
await conn.commit()
|
Reference in New Issue
Block a user