117 lines
3.6 KiB
Python
117 lines
3.6 KiB
Python
"""FastAPI application entry point."""
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.staticfiles import StaticFiles
|
|
import os
|
|
|
|
from app.core.config import settings
|
|
from app.core.database import init_db, engine
|
|
from sqlmodel import Session, select
|
|
from app.models.taxi import Taxi
|
|
from app.api.routes import router as routes_router
|
|
from app.api.bus_stops import router as bus_stops_router
|
|
from app.api.schedules import router as schedules_router
|
|
from app.api.coupons import router as coupons_router
|
|
from app.api.taxis import router as taxis_router
|
|
from app.api.auth import router as auth_router
|
|
from app.api.users import router as users_router
|
|
from app.api.favorites import router as favorites_router
|
|
from app.api.telemetry import router as telemetry_router
|
|
from app.api.businesses import router as businesses_router
|
|
from app.api.analytics import router as analytics_router
|
|
from app.api.reports import router as reports_router
|
|
from app.api.shuttles import router as shuttles_router
|
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
# Initialize database
|
|
init_db()
|
|
|
|
# Seed sample data if empty
|
|
with Session(engine) as session:
|
|
taxi_count = session.exec(select(Taxi)).first()
|
|
if not taxi_count:
|
|
sample_taxis = [
|
|
Taxi(
|
|
owner_name="Don José (Sibu Demo)",
|
|
phone_number="+507 6000-0001",
|
|
license_plate="T-001-SIBU",
|
|
corregimiento="Boquete",
|
|
shift="dia",
|
|
rating=4.9,
|
|
english_speaking=True
|
|
),
|
|
Taxi(
|
|
owner_name="María C. (Sibu Demo)",
|
|
phone_number="+507 6000-0002",
|
|
license_plate="T-002-SIBU",
|
|
corregimiento="Boquete",
|
|
shift="tarde",
|
|
rating=5.0,
|
|
english_speaking=False
|
|
)
|
|
]
|
|
session.add_all(sample_taxis)
|
|
session.commit()
|
|
yield
|
|
|
|
app = FastAPI(
|
|
title="SIBU Transportation API",
|
|
description="API for SIBU public transportation system",
|
|
version="1.0.0",
|
|
debug=settings.debug,
|
|
lifespan=lifespan
|
|
)
|
|
|
|
# CORS middleware
|
|
origins = [
|
|
"http://localhost:5173",
|
|
"http://127.0.0.1:5173",
|
|
"https://sibu2-0-transport-2026.web.app",
|
|
"https://sibu2-0-transport-2026.firebaseapp.com",
|
|
]
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Ensure upload directories exist
|
|
for sub in ["profiles", "vehicles", "businesses"]:
|
|
os.makedirs(os.path.join("uploads", sub), exist_ok=True)
|
|
|
|
# Mount static files
|
|
app.mount("/uploads", StaticFiles(directory="uploads"), name="uploads")
|
|
|
|
# Include routers
|
|
app.include_router(routes_router)
|
|
app.include_router(bus_stops_router)
|
|
app.include_router(schedules_router)
|
|
app.include_router(coupons_router)
|
|
app.include_router(taxis_router)
|
|
app.include_router(auth_router)
|
|
app.include_router(users_router)
|
|
app.include_router(favorites_router)
|
|
app.include_router(telemetry_router)
|
|
app.include_router(businesses_router)
|
|
app.include_router(analytics_router, prefix="/api/analytics", tags=["analytics"])
|
|
app.include_router(reports_router)
|
|
app.include_router(shuttles_router)
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
"""Root endpoint."""
|
|
return {"message": "SIBU Transportation API", "version": "1.0.0"}
|
|
|
|
|
|
@app.get("/health")
|
|
async def health():
|
|
"""Health check endpoint."""
|
|
return {"status": "healthy", "environment": settings.environment}
|