Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from contextlib import asynccontextmanager | |
| from database import engine, Base | |
| from migrate import run_migrations | |
| from routers import ( | |
| clients, employees, payroll, invoices, | |
| transactions, bank_accounts, revenues, expenses, auth, demo, personal_expenses | |
| ) | |
| async def lifespan(app: FastAPI): | |
| # Create tables on startup | |
| Base.metadata.create_all(bind=engine) | |
| # Run migrations for existing DB tables | |
| run_migrations() | |
| # Run db engine migrations for starting_salary | |
| from migrate import run_db_migrations | |
| run_db_migrations(engine) | |
| yield | |
| # Cleanup on shutdown | |
| pass | |
| app = FastAPI( | |
| title="Erha Technologies API", | |
| description="Backend API for Erha Technologies - Business Management System", | |
| version="1.0.0", | |
| lifespan=lifespan | |
| ) | |
| from fastapi.responses import JSONResponse | |
| import traceback | |
| async def global_exception_handler(request, exc): | |
| tb = "".join(traceback.format_exception(type(exc), exc, exc.__traceback__)) | |
| return JSONResponse( | |
| status_code=500, | |
| content={ | |
| "error": str(exc), | |
| "traceback": tb | |
| } | |
| ) | |
| # CORS configuration | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # In production, specify exact origins | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Include routers | |
| app.include_router(auth.router, prefix="/api/auth", tags=["Authentication"]) | |
| app.include_router(demo.router, prefix="/api/demo", tags=["Demo Data Seeding"]) | |
| app.include_router(clients.router, prefix="/api/clients", tags=["Clients"]) | |
| app.include_router(employees.router, prefix="/api/employees", tags=["Employees"]) | |
| app.include_router(payroll.router, prefix="/api/payroll", tags=["Payroll"]) | |
| app.include_router(invoices.router, prefix="/api/invoices", tags=["Invoices"]) | |
| app.include_router(transactions.router, prefix="/api/transactions", tags=["Transactions"]) | |
| app.include_router(bank_accounts.router, prefix="/api/bank-accounts", tags=["Bank Accounts"]) | |
| app.include_router(revenues.router, prefix="/api/revenues", tags=["Revenues"]) | |
| app.include_router(expenses.router, prefix="/api/expenses", tags=["Expenses"]) | |
| app.include_router(personal_expenses.router, prefix="/api/personal-expenses", tags=["Personal Expenses"]) | |
| async def root(): | |
| return { | |
| "message": "Erha Technologies API", | |
| "version": "1.0.0", | |
| "status": "running" | |
| } | |
| async def health_check(): | |
| return {"status": "healthy"} | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=8000) |