Skip to content

Commit e971aab

Browse files
committed
Removed global vars to make compatible with uvicorn workers > 1
1 parent 761bb29 commit e971aab

File tree

15 files changed

+109
-105
lines changed

15 files changed

+109
-105
lines changed

src/app/endpoints/authorized.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55

66
from fastapi import APIRouter, Depends
77

8-
from authentication.interface import AuthTuple
98
from authentication import get_auth_dependency
10-
from models.responses import AuthorizedResponse, UnauthorizedResponse, ForbiddenResponse
9+
from authentication.interface import AuthTuple
10+
from models.responses import AuthorizedResponse, ForbiddenResponse, UnauthorizedResponse
1111

1212
logger = logging.getLogger(__name__)
1313
router = APIRouter(tags=["authorized"])
14-
auth_dependency = get_auth_dependency()
1514

1615

1716
authorized_responses: dict[int | str, dict[str, Any]] = {
@@ -38,7 +37,7 @@
3837

3938
@router.post("/authorized", responses=authorized_responses)
4039
async def authorized_endpoint_handler(
41-
auth: Annotated[AuthTuple, Depends(auth_dependency)],
40+
auth: Annotated[AuthTuple, Depends(get_auth_dependency())],
4241
) -> AuthorizedResponse:
4342
"""
4443
Handle request to the /authorized endpoint.

src/app/endpoints/config.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import logging
44
from typing import Annotated, Any
55

6-
from fastapi import APIRouter, Request, Depends
6+
from fastapi import APIRouter, Depends, Request
77

8-
from authentication.interface import AuthTuple
98
from authentication import get_auth_dependency
9+
from authentication.interface import AuthTuple
1010
from authorization.middleware import authorize
1111
from configuration import configuration
1212
from models.config import Action, Configuration
@@ -15,8 +15,6 @@
1515
logger = logging.getLogger(__name__)
1616
router = APIRouter(tags=["config"])
1717

18-
auth_dependency = get_auth_dependency()
19-
2018

2119
get_config_responses: dict[int | str, dict[str, Any]] = {
2220
200: {
@@ -63,7 +61,7 @@
6361
@router.get("/config", responses=get_config_responses)
6462
@authorize(Action.GET_CONFIG)
6563
async def config_endpoint_handler(
66-
auth: Annotated[AuthTuple, Depends(auth_dependency)],
64+
auth: Annotated[AuthTuple, Depends(get_auth_dependency())],
6765
request: Request,
6866
) -> Configuration:
6967
"""

src/app/endpoints/conversations.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@
33
import logging
44
from typing import Any
55

6+
from fastapi import APIRouter, Depends, HTTPException, Request, status
67
from llama_stack_client import APIConnectionError, NotFoundError
78

8-
from fastapi import APIRouter, HTTPException, Request, status, Depends
9-
10-
from client import AsyncLlamaStackClientHolder
11-
from configuration import configuration
129
from app.database import get_session
1310
from authentication import get_auth_dependency
1411
from authorization.middleware import authorize
12+
from client import AsyncLlamaStackClientHolder
13+
from configuration import configuration
1514
from models.config import Action
1615
from models.database.conversations import UserConversation
1716
from models.responses import (
18-
ConversationResponse,
1917
ConversationDeleteResponse,
20-
ConversationsListResponse,
2118
ConversationDetails,
19+
ConversationResponse,
20+
ConversationsListResponse,
2221
UnauthorizedResponse,
2322
)
2423
from utils.endpoints import (
@@ -30,7 +29,6 @@
3029

3130
logger = logging.getLogger("app.endpoints.handlers")
3231
router = APIRouter(tags=["conversations"])
33-
auth_dependency = get_auth_dependency()
3432

3533
conversation_responses: dict[int | str, dict[str, Any]] = {
3634
200: {
@@ -180,7 +178,7 @@ def simplify_session_data(session_data: dict) -> list[dict[str, Any]]:
180178
@authorize(Action.LIST_CONVERSATIONS)
181179
async def get_conversations_list_endpoint_handler(
182180
request: Request,
183-
auth: Any = Depends(auth_dependency),
181+
auth: Any = Depends(get_auth_dependency()),
184182
) -> ConversationsListResponse:
185183
"""Handle request to retrieve all conversations for the authenticated user."""
186184
check_configuration_loaded(configuration)
@@ -242,7 +240,7 @@ async def get_conversations_list_endpoint_handler(
242240
async def get_conversation_endpoint_handler(
243241
request: Request,
244242
conversation_id: str,
245-
auth: Any = Depends(auth_dependency),
243+
auth: Any = Depends(get_auth_dependency()),
246244
) -> ConversationResponse:
247245
"""
248246
Handle request to retrieve a conversation by ID.
@@ -370,7 +368,7 @@ async def get_conversation_endpoint_handler(
370368
async def delete_conversation_endpoint_handler(
371369
request: Request,
372370
conversation_id: str,
373-
auth: Any = Depends(auth_dependency),
371+
auth: Any = Depends(get_auth_dependency()),
374372
) -> ConversationDeleteResponse:
375373
"""
376374
Handle request to delete a conversation by ID.

src/app/endpoints/conversations_v2.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,24 @@
33
import logging
44
from typing import Any
55

6-
from fastapi import APIRouter, Request, Depends, HTTPException, status
6+
from fastapi import APIRouter, Depends, HTTPException, Request, status
77

8-
from configuration import configuration
98
from authentication import get_auth_dependency
109
from authorization.middleware import authorize
10+
from configuration import configuration
1111
from models.cache_entry import CacheEntry
1212
from models.config import Action
1313
from models.responses import (
14-
ConversationsListResponseV2,
15-
ConversationResponse,
1614
ConversationDeleteResponse,
15+
ConversationResponse,
16+
ConversationsListResponseV2,
1717
UnauthorizedResponse,
1818
)
1919
from utils.endpoints import check_configuration_loaded
2020
from utils.suid import check_suid
2121

2222
logger = logging.getLogger("app.endpoints.handlers")
2323
router = APIRouter(tags=["conversations_v2"])
24-
auth_dependency = get_auth_dependency()
2524

2625

2726
conversation_responses: dict[int | str, dict[str, Any]] = {
@@ -93,7 +92,7 @@
9392
@authorize(Action.LIST_CONVERSATIONS)
9493
async def get_conversations_list_endpoint_handler(
9594
request: Request, # pylint: disable=unused-argument
96-
auth: Any = Depends(auth_dependency),
95+
auth: Any = Depends(get_auth_dependency()),
9796
) -> ConversationsListResponseV2:
9897
"""Handle request to retrieve all conversations for the authenticated user."""
9998
check_configuration_loaded(configuration)
@@ -123,7 +122,7 @@ async def get_conversations_list_endpoint_handler(
123122
async def get_conversation_endpoint_handler(
124123
request: Request, # pylint: disable=unused-argument
125124
conversation_id: str,
126-
auth: Any = Depends(auth_dependency),
125+
auth: Any = Depends(get_auth_dependency()),
127126
) -> ConversationResponse:
128127
"""Handle request to retrieve a conversation by ID."""
129128
check_configuration_loaded(configuration)
@@ -159,7 +158,7 @@ async def get_conversation_endpoint_handler(
159158
async def delete_conversation_endpoint_handler(
160159
request: Request, # pylint: disable=unused-argument
161160
conversation_id: str,
162-
auth: Any = Depends(auth_dependency),
161+
auth: Any = Depends(get_auth_dependency()),
163162
) -> ConversationDeleteResponse:
164163
"""Handle request to delete a conversation by ID."""
165164
check_configuration_loaded(configuration)

src/app/endpoints/feedback.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"""Handler for REST API endpoint for user feedback."""
22

3+
import json
34
import logging
45
import threading
5-
from typing import Annotated, Any
6+
from datetime import UTC, datetime
67
from pathlib import Path
7-
import json
8-
from datetime import datetime, UTC
9-
from fastapi import APIRouter, HTTPException, Depends, Request, status
8+
from typing import Annotated, Any
9+
10+
from fastapi import APIRouter, Depends, HTTPException, Request, status
1011

1112
from authentication import get_auth_dependency
1213
from authentication.interface import AuthTuple
@@ -18,15 +19,14 @@
1819
ErrorResponse,
1920
FeedbackResponse,
2021
FeedbackStatusUpdateResponse,
22+
ForbiddenResponse,
2123
StatusResponse,
2224
UnauthorizedResponse,
23-
ForbiddenResponse,
2425
)
2526
from utils.suid import get_suid
2627

2728
logger = logging.getLogger(__name__)
2829
router = APIRouter(prefix="/feedback", tags=["feedback"])
29-
auth_dependency = get_auth_dependency()
3030
feedback_status_lock = threading.Lock()
3131

3232
# Response for the feedback endpoint
@@ -87,7 +87,7 @@ async def assert_feedback_enabled(_request: Request) -> None:
8787
@authorize(Action.FEEDBACK)
8888
async def feedback_endpoint_handler(
8989
feedback_request: FeedbackRequest,
90-
auth: Annotated[AuthTuple, Depends(auth_dependency)],
90+
auth: Annotated[AuthTuple, Depends(get_auth_dependency())],
9191
_ensure_feedback_enabled: Any = Depends(assert_feedback_enabled),
9292
) -> FeedbackResponse:
9393
"""Handle feedback requests.
@@ -183,7 +183,7 @@ def feedback_status() -> StatusResponse:
183183
@authorize(Action.ADMIN)
184184
async def update_feedback_status(
185185
feedback_update_request: FeedbackStatusUpdateRequest,
186-
auth: Annotated[AuthTuple, Depends(auth_dependency)],
186+
auth: Annotated[AuthTuple, Depends(get_auth_dependency())],
187187
) -> FeedbackStatusUpdateResponse:
188188
"""
189189
Handle feedback status update requests.

src/app/endpoints/health.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,23 @@
88
import logging
99
from typing import Annotated, Any
1010

11+
from fastapi import APIRouter, Depends, Response, status
1112
from llama_stack.providers.datatypes import HealthStatus
1213

13-
from fastapi import APIRouter, status, Response, Depends
14-
from client import AsyncLlamaStackClientHolder
15-
from authentication.interface import AuthTuple
1614
from authentication import get_auth_dependency
15+
from authentication.interface import AuthTuple
1716
from authorization.middleware import authorize
17+
from client import AsyncLlamaStackClientHolder
1818
from models.config import Action
1919
from models.responses import (
2020
LivenessResponse,
21-
ReadinessResponse,
2221
ProviderHealthStatus,
22+
ReadinessResponse,
2323
)
2424

2525
logger = logging.getLogger("app.endpoints.handlers")
2626
router = APIRouter(tags=["health"])
2727

28-
auth_dependency = get_auth_dependency()
29-
3028

3129
async def get_providers_health_statuses() -> list[ProviderHealthStatus]:
3230
"""
@@ -80,7 +78,7 @@ async def get_providers_health_statuses() -> list[ProviderHealthStatus]:
8078
@router.get("/readiness", responses=get_readiness_responses)
8179
@authorize(Action.INFO)
8280
async def readiness_probe_get_method(
83-
auth: Annotated[AuthTuple, Depends(auth_dependency)],
81+
auth: Annotated[AuthTuple, Depends(get_auth_dependency())],
8482
response: Response,
8583
) -> ReadinessResponse:
8684
"""
@@ -126,7 +124,7 @@ async def readiness_probe_get_method(
126124
@router.get("/liveness", responses=get_liveness_responses)
127125
@authorize(Action.INFO)
128126
async def liveness_probe_get_method(
129-
auth: Annotated[AuthTuple, Depends(auth_dependency)],
127+
auth: Annotated[AuthTuple, Depends(get_auth_dependency())],
130128
) -> LivenessResponse:
131129
"""
132130
Return the liveness status of the service.

src/app/endpoints/info.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,21 @@
33
import logging
44
from typing import Annotated, Any
55

6-
from fastapi import APIRouter, HTTPException, Request, status
7-
from fastapi import Depends
6+
from fastapi import APIRouter, Depends, HTTPException, Request, status
87
from llama_stack_client import APIConnectionError
98

10-
from authentication.interface import AuthTuple
119
from authentication import get_auth_dependency
10+
from authentication.interface import AuthTuple
1211
from authorization.middleware import authorize
13-
from configuration import configuration
1412
from client import AsyncLlamaStackClientHolder
13+
from configuration import configuration
1514
from models.config import Action
1615
from models.responses import InfoResponse
1716
from version import __version__
1817

1918
logger = logging.getLogger("app.endpoints.handlers")
2019
router = APIRouter(tags=["info"])
2120

22-
auth_dependency = get_auth_dependency()
23-
2421

2522
get_info_responses: dict[int | str, dict[str, Any]] = {
2623
200: {
@@ -40,7 +37,7 @@
4037
@router.get("/info", responses=get_info_responses)
4138
@authorize(Action.INFO)
4239
async def info_endpoint_handler(
43-
auth: Annotated[AuthTuple, Depends(auth_dependency)],
40+
auth: Annotated[AuthTuple, Depends(get_auth_dependency())],
4441
request: Request,
4542
) -> InfoResponse:
4643
"""

src/app/endpoints/metrics.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
"""Handler for REST API call to provide metrics."""
22

33
from typing import Annotated
4+
5+
from fastapi import APIRouter, Depends, Request
46
from fastapi.responses import PlainTextResponse
5-
from fastapi import APIRouter, Request, Depends
67
from prometheus_client import (
7-
generate_latest,
88
CONTENT_TYPE_LATEST,
9+
generate_latest,
910
)
1011

11-
from authentication.interface import AuthTuple
1212
from authentication import get_auth_dependency
13+
from authentication.interface import AuthTuple
1314
from authorization.middleware import authorize
14-
from models.config import Action
1515
from metrics.utils import setup_model_metrics
16+
from models.config import Action
1617

1718
router = APIRouter(tags=["metrics"])
1819

19-
auth_dependency = get_auth_dependency()
20-
2120

2221
@router.get("/metrics", response_class=PlainTextResponse)
2322
@authorize(Action.GET_METRICS)
2423
async def metrics_endpoint_handler(
25-
auth: Annotated[AuthTuple, Depends(auth_dependency)],
24+
auth: Annotated[AuthTuple, Depends(get_auth_dependency())],
2625
request: Request,
2726
) -> PlainTextResponse:
2827
"""

src/app/endpoints/models.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
from authentication import get_auth_dependency
1111
from authentication.interface import AuthTuple
12+
from authorization.middleware import authorize
1213
from client import AsyncLlamaStackClientHolder
1314
from configuration import configuration
14-
from authorization.middleware import authorize
1515
from models.config import Action
1616
from models.responses import ModelsResponse
1717
from utils.endpoints import check_configuration_loaded
@@ -20,9 +20,6 @@
2020
router = APIRouter(tags=["models"])
2121

2222

23-
auth_dependency = get_auth_dependency()
24-
25-
2623
models_responses: dict[int | str, dict[str, Any]] = {
2724
200: {
2825
"models": [
@@ -54,7 +51,7 @@
5451
@authorize(Action.GET_MODELS)
5552
async def models_endpoint_handler(
5653
request: Request,
57-
auth: Annotated[AuthTuple, Depends(auth_dependency)],
54+
auth: Annotated[AuthTuple, Depends(get_auth_dependency())],
5855
) -> ModelsResponse:
5956
"""
6057
Handle requests to the /models endpoint.

src/app/endpoints/query.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757

5858
logger = logging.getLogger("app.endpoints.handlers")
5959
router = APIRouter(tags=["query"])
60-
auth_dependency = get_auth_dependency()
6160

6261
query_response: dict[int | str, dict[str, Any]] = {
6362
200: {
@@ -174,7 +173,7 @@ def evaluate_model_hints(
174173
async def query_endpoint_handler(
175174
request: Request,
176175
query_request: QueryRequest,
177-
auth: Annotated[AuthTuple, Depends(auth_dependency)],
176+
auth: Annotated[AuthTuple, Depends(get_auth_dependency())],
178177
mcp_headers: dict[str, dict[str, str]] = Depends(mcp_headers_dependency),
179178
) -> QueryResponse:
180179
"""

0 commit comments

Comments
 (0)