File size: 2,588 Bytes
4854e2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Startup and initialization script for the Face Inference Platform."""
import logging
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent))

from app.core.database import SessionLocal, init_db
from app.core.config import settings
from app.core.security import hash_password
from app.models import Tenant, User, UserRole
from app.services.auth_service import AuthService, TenantService

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def init_application():
    """Create database tables and default development admin."""
    logger.info("Initializing Face Inference Platform...")

    logger.info("Initializing database...")
    init_db()

    db = SessionLocal()
    try:
        tenant = db.query(Tenant).filter(Tenant.name == "development").first()
        if not tenant:
            logger.info("Creating default development tenant...")
            tenant = TenantService.create_tenant(
                db=db,
                name="development",
                plan="enterprise",
            )

        admin_email = settings.ADMIN_DEFAULT_EMAIL
        admin_password = settings.ADMIN_DEFAULT_PASSWORD

        admin = (
            db.query(User)
            .filter(
                User.tenant_id == tenant.id,
                User.role == UserRole.ADMIN,
            )
            .order_by(User.created_at.asc())
            .first()
        )

        if not admin:
            logger.info("Creating default admin user...")
            admin = AuthService.register_user(
                db=db,
                email=admin_email,
                password=admin_password,
                full_name="Admin User",
                tenant_id=tenant.id,
                role=UserRole.ADMIN,
            )
            logger.info("Default admin user created: %s", admin.email)
        else:
            admin.email = admin_email
            admin.password_hash = hash_password(admin_password)
            admin.role = UserRole.ADMIN
            admin.is_active = True
            db.commit()
            db.refresh(admin)
            logger.info("Default admin user reset: %s", admin.email)

        logger.info("Default credentials for testing:")
        logger.info("  Email: %s", admin_email)
        logger.info("  Password: %s", admin_password)
        logger.info("Application ready. API docs: http://localhost:8000/docs")
    except Exception as exc:
        logger.error("Error initializing application: %s", exc)
        raise
    finally:
        db.close()


if __name__ == "__main__":
    init_application()