app.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from fastapi import FastAPI
  2. from fastapi.middleware.cors import CORSMiddleware
  3. from starlette.middleware.sessions import SessionMiddleware
  4. from config.settings import SECRET_KEY
  5. from routes import sales
  6. def create_app() -> FastAPI:
  7. """Create and configure FastAPI application"""
  8. app = FastAPI(title="Web Pedidos Klein - FastAPI Backend",
  9. description="Backend for the Web Pedidos Klein application using FastAPI",)
  10. # Add CORS middleware
  11. app.add_middleware(
  12. CORSMiddleware,
  13. allow_origins=["https://admin.kleinexpress.store"], # Permite solo este origen
  14. allow_credentials=True,
  15. allow_methods=["*"],
  16. allow_headers=["*"],
  17. )
  18. # Add SessionMiddleware
  19. app.add_middleware(
  20. SessionMiddleware,
  21. secret_key=SECRET_KEY,
  22. max_age=60 * 60 # max_age in seconds for Starlette
  23. )
  24. return app
  25. def setup_routes(app: FastAPI):
  26. """Setup all application routes"""
  27. from routes import chat, users, products, orders, static
  28. from fastapi import Depends
  29. from auth.security import get_current_user
  30. # Chat routes
  31. app.include_router(chat.chat_router, prefix="/api/chat",tags=["Chat"], dependencies=[Depends(get_current_user)])
  32. # User routes
  33. app.include_router(users.user_router, prefix="/api/users", tags=["Users"])
  34. # Product routes
  35. app.include_router(products.product_router, prefix="/api/products", tags=["Products"],dependencies=[Depends(get_current_user)])
  36. # Order routes
  37. app.include_router(orders.order_router, prefix="/api/orders", tags=["Orders"], dependencies=[Depends(get_current_user)])
  38. # Sales routes
  39. app.include_router(sales.sales_router, prefix="/api/sales", tags=["Sales"], dependencies=[Depends(get_current_user)])
  40. # Verification routes
  41. app.include_router(users.verify_router, prefix="/verify", tags=["Verification"])
  42. # Static routes
  43. from fastapi.responses import HTMLResponse
  44. app.add_api_route("/", static.serve_app_html, methods=["GET"],
  45. response_class=HTMLResponse, include_in_schema=False)
  46. app.add_api_route("/register", static.serve_register_html, methods=["GET"],
  47. response_class=HTMLResponse, include_in_schema=False)
  48. app.add_api_route("/images/{image_path:path}", static.serve_image, methods=["GET"],
  49. response_class=HTMLResponse, include_in_schema=False)
  50. # Mount static files
  51. static.mount_main_static_files(app)
  52. static.mount_register_static_files(app)