app.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from fastapi import FastAPI
  2. from starlette.middleware.sessions import SessionMiddleware
  3. from config.settings import SECRET_KEY, validate_config
  4. def create_app() -> FastAPI:
  5. """Create and configure FastAPI application"""
  6. app = FastAPI(title="Web Pedidos Klein - FastAPI Backend")
  7. # Add SessionMiddleware
  8. app.add_middleware(
  9. SessionMiddleware,
  10. secret_key=SECRET_KEY,
  11. max_age=60 * 60 # max_age in seconds for Starlette
  12. )
  13. return app
  14. def setup_routes(app: FastAPI):
  15. """Setup all application routes"""
  16. from routes import chat, users, products, orders, static
  17. from fastapi import Depends
  18. from auth.security import protect_chat_api
  19. # Chat routes
  20. app.add_api_route("/api/chat/init-chat", chat.init_chat, methods=["GET"], summary="Initialize chat and get anti-abuse token")
  21. app.add_api_route("/api/chat/completions", chat.chat_completions, methods=["POST"],
  22. summary="Get chat completions from OpenAI", dependencies=[Depends(protect_chat_api)])
  23. # User routes
  24. app.add_api_route("/api/existsUser", users.exists_user, methods=["POST"], summary="Check if user exists")
  25. # Product routes
  26. app.add_api_route("/api/get_products", products.get_products, methods=["GET"], summary="Get products")
  27. # Order routes
  28. app.add_api_route("/api/printer/order", orders.printer_order, methods=["POST"],
  29. summary="Printer order", dependencies=[Depends(protect_chat_api)])
  30. # Static routes
  31. from fastapi.responses import HTMLResponse
  32. app.add_api_route("/", static.serve_index_html, methods=["GET"],
  33. response_class=HTMLResponse, include_in_schema=False)
  34. # Mount static files
  35. static.mount_static_files(app)