File size: 2,314 Bytes
680fa2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from fastapi import FastAPI, Depends, APIRouter
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy.orm import Session

from backend.app.core.config import settings
from backend.app.core.database import SessionLocal, engine, get_db
from backend.app.core.seeding import seed_db
from backend.app.models.base import Base

# Import Routers
from backend.app.routers import (
    auth_router,
    workers_router,
    jobs_router,
    bookings_router,
    wallet_router,
    roster_router,
    admin_router
)

# Initialize database tables
# This is a safe fallback; in production migrations are preferred,
# but for the BCA viva, ensuring tables exist automatically is a lifesaver.
Base.metadata.create_all(bind=engine)

# Trigger Database Seeding on startup
db = SessionLocal()
try:
    seed_db(db)
finally:
    db.close()

app = FastAPI(
    title=settings.PROJECT_NAME,
    description="ConstructHire location-aware labor marketplace backend",
    version="1.0.0",
    docs_url="/docs",
    redoc_url="/redoc"
)

# CORS configuration
# Allow local Vite dev server and production netlify deployments
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"], # For BCA viva simplicity; in strict prod specify origins
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Register API Routers
api_router = APIRouter(prefix="/api")
api_router.include_router(auth_router)
api_router.include_router(workers_router)
api_router.include_router(jobs_router)
api_router.include_router(bookings_router)
api_router.include_router(wallet_router)
api_router.include_router(roster_router)
api_router.include_router(admin_router)

app.include_router(api_router)

@app.get("/")
def read_root():
    """Root endpoint to check backend status."""
    return {
        "status": "online",
        "service": settings.PROJECT_NAME,
        "docs": "/docs",
        "api_prefix": settings.API_V1_STR
    }

@app.post("/api/admin/reset-db")
def reset_database(db: Session = Depends(get_db)):
    """Convenience endpoint to clear and re-seed the database with demo values."""
    # Drop all tables and recreate them
    Base.metadata.drop_all(bind=engine)
    Base.metadata.create_all(bind=engine)
    seed_db(db)
    return {"message": "Database successfully reset and re-seeded."}