main.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from litellm.proxy.proxy_server import ProxyConfig, initialize
  2. from litellm.proxy.proxy_server import app
  3. from fastapi import FastAPI, Request, Depends, status
  4. from fastapi.responses import JSONResponse
  5. from utils.utils import get_http_authorization_cred, get_current_user
  6. from config import ENV
  7. proxy_config = ProxyConfig()
  8. async def config():
  9. router, model_list, general_settings = await proxy_config.load_config(
  10. router=None, config_file_path="./data/litellm/config.yaml"
  11. )
  12. await initialize(config="./data/litellm/config.yaml", telemetry=False)
  13. async def startup():
  14. await config()
  15. @app.on_event("startup")
  16. async def on_startup():
  17. await startup()
  18. @app.middleware("http")
  19. async def auth_middleware(request: Request, call_next):
  20. auth_header = request.headers.get("Authorization", "")
  21. if ENV != "dev":
  22. try:
  23. user = get_current_user(get_http_authorization_cred(auth_header))
  24. print(user)
  25. except Exception as e:
  26. return JSONResponse(status_code=400, content={"detail": str(e)})
  27. response = await call_next(request)
  28. return response