main.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from fastapi import FastAPI, HTTPException, Request
  2. from fastapi.middleware.cors import CORSMiddleware
  3. from fastapi.exceptions import RequestValidationError
  4. from fastapi.responses import JSONResponse
  5. import traceback
  6. import os
  7. import locales
  8. import config
  9. from routers import auth, orders, catalog, portfolio, files, chat
  10. app = FastAPI(title="Radionica 3D API")
  11. # Configure CORS
  12. app.add_middleware(
  13. CORSMiddleware,
  14. allow_origins=["http://localhost:5173", "http://127.0.0.1:5173"],
  15. allow_credentials=True,
  16. allow_methods=["*"],
  17. allow_headers=["*"],
  18. )
  19. @app.exception_handler(RequestValidationError)
  20. async def validation_exception_handler(request: Request, exc: RequestValidationError):
  21. lang = request.query_params.get("lang", "en")
  22. errors = []
  23. for error in exc.errors():
  24. error_type = error.get("type", "unknown")
  25. ctx = error.get("ctx", {})
  26. translated_msg = locales.translate_error(error_type, lang, **ctx)
  27. loc = ".".join(str(l) for l in error.get("loc", [])[1:])
  28. errors.append({
  29. "loc": error.get("loc"),
  30. "msg": f"{loc}: {translated_msg}" if loc else translated_msg,
  31. "type": error_type
  32. })
  33. return JSONResponse(status_code=422, content={"detail": errors})
  34. @app.exception_handler(Exception)
  35. async def all_exception_handler(request: Request, exc: Exception):
  36. if config.DEBUG:
  37. return JSONResponse(
  38. status_code=500,
  39. content={"detail": str(exc), "traceback": traceback.format_exc()}
  40. )
  41. return JSONResponse(status_code=500, content={"detail": "Internal server error"})
  42. # Add custom exception logging or other middleware here if needed
  43. # Include Routers
  44. app.include_router(auth.router)
  45. app.include_router(orders.router)
  46. app.include_router(catalog.router)
  47. app.include_router(portfolio.router)
  48. app.include_router(files.router)
  49. app.include_router(chat.router)
  50. if __name__ == "__main__":
  51. import uvicorn
  52. uvicorn.run(app, host="0.0.0.0", port=8000)