app.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from time import struct_time
  2. from fastapi import FastAPI
  3. from starlette.middleware.sessions import SessionMiddleware
  4. from config.settings import SECRET_KEY, validate_config
  5. def create_app() -> FastAPI:
  6. """Create and configure FastAPI application"""
  7. app = FastAPI(title="Web Pedidos Klein - FastAPI Backend",
  8. description="Backend for the Web Pedidos Klein application using FastAPI",)
  9. # Add SessionMiddleware
  10. app.add_middleware(
  11. SessionMiddleware,
  12. secret_key=SECRET_KEY,
  13. max_age=60 * 60 # max_age in seconds for Starlette
  14. )
  15. return app
  16. def setup_routes(app: FastAPI):
  17. """Setup all application routes"""
  18. from routes import chat, users, products, orders, static
  19. from fastapi import Depends
  20. from auth.security import get_current_user
  21. # Chat routes
  22. app.include_router(chat.chat_router, prefix="/api/chat",tags=["Chat"], dependencies=[Depends(get_current_user)])
  23. # User routes
  24. app.include_router(users.user_router, prefix="/api/users", tags=["Users"])
  25. # Product routes
  26. app.include_router(products.product_router, prefix="/api/products", tags=["Products"],dependencies=[Depends(get_current_user)])
  27. # Order routes
  28. app.include_router(orders.order_router, prefix="/api/orders", tags=["Orders"], dependencies=[Depends(get_current_user)])
  29. # Static routes
  30. from fastapi.responses import HTMLResponse
  31. app.add_api_route("/", static.serve_app_html, methods=["GET"],
  32. response_class=HTMLResponse, include_in_schema=False)
  33. app.add_api_route("/register", static.serve_register_html, methods=["GET"],
  34. response_class=HTMLResponse, include_in_schema=False)
  35. # Mount static files
  36. static.mount_main_static_files(app)
  37. static.mount_register_static_files(app)