update account admin luôn tồn tại

This commit is contained in:
2026-04-02 11:04:40 +07:00
parent b35b201b77
commit 51c599e040
3 changed files with 38 additions and 0 deletions
+5
View File
@@ -18,6 +18,11 @@ ACCESS_TOKEN_EXPIRE_MINUTES=60
# Port exposed on the host for the Next.js frontend
FRONTEND_PORT=3011
# ── Default admin account (created automatically on first start) ─────────────
ADMIN_USERNAME=admin
ADMIN_PASSWORD=Admin@12345
ADMIN_EMAIL=admin@lms.local
# ── Docker Hub images (optional — leave blank to build locally) ───────────────
# Set these on the target machine so docker compose pull works without building
# BACKEND_IMAGE=your_dockerhub_username/lms-backend:latest
+30
View File
@@ -1,15 +1,45 @@
from contextlib import asynccontextmanager
import os
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from .routers import admin, annotations, auth, backup, pdfs, ws
from .database import SessionLocal
from .models import User, UserRole, UserStatus
from .security import hash_password
def _seed_admin() -> None:
"""Create the default admin account if it does not exist yet."""
username = os.getenv("ADMIN_USERNAME", "admin")
password = os.getenv("ADMIN_PASSWORD", "Admin@12345")
email = os.getenv("ADMIN_EMAIL", "admin@lms.local")
db = SessionLocal()
try:
exists = db.query(User).filter(User.username == username).first()
if not exists:
db.add(User(
username = username,
email = email,
password_hash = hash_password(password),
role = UserRole.admin,
status = UserStatus.approved,
))
db.commit()
print(f"[seed] Admin account '{username}' created.")
else:
print(f"[seed] Admin account '{username}' already exists — skipped.")
finally:
db.close()
@asynccontextmanager
async def lifespan(app: FastAPI):
# Tables are managed by Alembic migrations.
# Run: alembic upgrade head (done by docker-compose entrypoint)
_seed_admin()
yield
+3
View File
@@ -50,6 +50,9 @@ services:
ACCESS_TOKEN_EXPIRE_MINUTES: ${ACCESS_TOKEN_EXPIRE_MINUTES:-60}
UPLOAD_DIR: /uploads
REDIS_URL: redis://redis:6379/0
ADMIN_USERNAME: ${ADMIN_USERNAME:-admin}
ADMIN_PASSWORD: ${ADMIN_PASSWORD:-Admin@12345}
ADMIN_EMAIL: ${ADMIN_EMAIL:-admin@lms.local}
volumes:
- pdf_uploads:/uploads
expose: