mirror of
https://git.victorphan.net/basketballcantho/ten-project.git
synced 2026-08-05 09:03:11 +07:00
update account admin luôn tồn tại
This commit is contained in:
@@ -18,6 +18,11 @@ ACCESS_TOKEN_EXPIRE_MINUTES=60
|
|||||||
# Port exposed on the host for the Next.js frontend
|
# Port exposed on the host for the Next.js frontend
|
||||||
FRONTEND_PORT=3011
|
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) ───────────────
|
# ── Docker Hub images (optional — leave blank to build locally) ───────────────
|
||||||
# Set these on the target machine so docker compose pull works without building
|
# Set these on the target machine so docker compose pull works without building
|
||||||
# BACKEND_IMAGE=your_dockerhub_username/lms-backend:latest
|
# BACKEND_IMAGE=your_dockerhub_username/lms-backend:latest
|
||||||
|
|||||||
@@ -1,15 +1,45 @@
|
|||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
|
import os
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
|
||||||
from .routers import admin, annotations, auth, backup, pdfs, ws
|
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
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
# Tables are managed by Alembic migrations.
|
# Tables are managed by Alembic migrations.
|
||||||
# Run: alembic upgrade head (done by docker-compose entrypoint)
|
# Run: alembic upgrade head (done by docker-compose entrypoint)
|
||||||
|
_seed_admin()
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -50,6 +50,9 @@ services:
|
|||||||
ACCESS_TOKEN_EXPIRE_MINUTES: ${ACCESS_TOKEN_EXPIRE_MINUTES:-60}
|
ACCESS_TOKEN_EXPIRE_MINUTES: ${ACCESS_TOKEN_EXPIRE_MINUTES:-60}
|
||||||
UPLOAD_DIR: /uploads
|
UPLOAD_DIR: /uploads
|
||||||
REDIS_URL: redis://redis:6379/0
|
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:
|
volumes:
|
||||||
- pdf_uploads:/uploads
|
- pdf_uploads:/uploads
|
||||||
expose:
|
expose:
|
||||||
|
|||||||
Reference in New Issue
Block a user