-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_router.py
More file actions
61 lines (47 loc) · 1.41 KB
/
api_router.py
File metadata and controls
61 lines (47 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from fastapi import APIRouter, HTTPException
from src.common.utils import get_logger
from src.common.models import DriverLocationEvent
logger = get_logger("DriverLocationAPI")
router = APIRouter()
# These will be set by main.py
DRIVER_STORE = None
@router.get("/drivers")
async def get_all_drivers():
"""
Returns all active drivers and their current coordinates.
"""
if DRIVER_STORE is None:
raise HTTPException(500, "Driver store not initialized.")
drivers = DRIVER_STORE.get_all_drivers()
return {
"count": len(drivers),
"drivers": drivers
}
@router.get("/drivers/{driver_id}")
async def get_driver(driver_id: str):
"""
Returns location data for a specific driver.
"""
if DRIVER_STORE is None:
raise HTTPException(500, "Driver store not initialized.")
driver = DRIVER_STORE.get_driver(driver_id)
if not driver:
raise HTTPException(404, f"Driver {driver_id} not found.")
return driver
@router.get("/count")
async def get_driver_count():
"""
Quick counter to see how many drivers are active.
"""
if DRIVER_STORE is None:
raise HTTPException(500, "Driver store not initialized.")
return {"active_drivers": DRIVER_STORE.count()}
@router.get("/health")
async def health_check():
"""
Returns service health.
"""
return {
"status": "OK",
"service": "driver-location-service"
}