main.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from fastapi import FastAPI, HTTPException, Request
  2. from fastapi.staticfiles import StaticFiles
  3. from fastapi.middleware.cors import CORSMiddleware
  4. from fastapi.exceptions import RequestValidationError
  5. from fastapi.responses import JSONResponse
  6. import traceback
  7. import os
  8. import locales
  9. import config
  10. from routers import auth, orders, catalog, portfolio, files, chat, blog
  11. app = FastAPI(title="Radionica 3D API")
  12. # Configure CORS
  13. app.add_middleware(
  14. CORSMiddleware,
  15. allow_origins=[
  16. "http://localhost:5173",
  17. "http://127.0.0.1:5173",
  18. "http://localhost:5000", # if user uses different port
  19. "https://localhost:5173"
  20. ],
  21. allow_credentials=True,
  22. allow_methods=["*"],
  23. allow_headers=["*"],
  24. )
  25. @app.exception_handler(RequestValidationError)
  26. async def validation_exception_handler(request: Request, exc: RequestValidationError):
  27. lang = request.query_params.get("lang", "en")
  28. errors = []
  29. for error in exc.errors():
  30. error_type = error.get("type", "unknown")
  31. ctx = error.get("ctx", {})
  32. translated_msg = locales.translate_error(error_type, lang, **ctx)
  33. loc = ".".join(str(l) for l in error.get("loc", [])[1:])
  34. errors.append({
  35. "loc": error.get("loc"),
  36. "msg": f"{loc}: {translated_msg}" if loc else translated_msg,
  37. "type": error_type
  38. })
  39. return JSONResponse(status_code=422, content={"detail": errors})
  40. @app.exception_handler(Exception)
  41. async def all_exception_handler(request: Request, exc: Exception):
  42. print(f"ERROR: {exc}")
  43. traceback.print_exc()
  44. if config.DEBUG:
  45. return JSONResponse(
  46. status_code=500,
  47. content={"detail": str(exc), "traceback": traceback.format_exc()}
  48. )
  49. return JSONResponse(status_code=500, content={"detail": "Internal server error"})
  50. # Add custom exception logging or other middleware here if needed
  51. # Include Routers
  52. app.include_router(auth.router)
  53. app.include_router(orders.router)
  54. app.include_router(catalog.router)
  55. app.include_router(portfolio.router)
  56. app.include_router(files.router)
  57. app.include_router(chat.router)
  58. app.include_router(blog.router)
  59. # Mount Static Files
  60. if not os.path.exists("uploads"):
  61. os.makedirs("uploads")
  62. # Mount static files for uploads and previews with caching
  63. app.mount("/uploads", StaticFiles(directory="uploads", html=False), name="uploads")
  64. @app.middleware("http")
  65. async def add_cache_control_header(request, call_next):
  66. response = await call_next(request)
  67. if request.url.path.startswith("/uploads"):
  68. response.headers["Cache-Control"] = "public, max-age=604800, immutable"
  69. return response
  70. if __name__ == "__main__":
  71. import uvicorn
  72. uvicorn.run(app, host="0.0.0.0", port=8000)