app.py 2.6 KB

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