Skip to content

Commit b66d7c1

Browse files
authored
Update main.py
1 parent 133b018 commit b66d7c1

File tree

1 file changed

+34
-64
lines changed

1 file changed

+34
-64
lines changed

main.py

Lines changed: 34 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,104 +3,74 @@
33

44
from src.common.event_bus import EventBus
55
from src.common.utils import get_logger
6-
from src.common.models import MatchResultEvent, DriverLocationEvent
6+
from src.common.models import DriverLocationEvent
77

8-
from src.dispatch-service.producer import DispatchProducer
9-
from src.dispatch-service.consumer import DispatchConsumer
10-
from src.dispatch-service.api_router import router, DRIVER_STORE, MATCH_RESULTS_STORE
8+
from src.driver-location-service.location_store import DriverLocationStore
9+
from src.driver-location-service.consumer import DriverLocationConsumer
10+
from src.driver-location-service.api_router import router, DRIVER_STORE
1111

12-
# --------------------------------------------------------------------
13-
# CORE STATE STORES
14-
# --------------------------------------------------------------------
1512

16-
# In-memory driver store populated by Driver Location Service
17-
_driver_locations: list[DriverLocationEvent] = []
13+
# ------------------------------------------------------------
14+
# INITIALIZATION
15+
# ------------------------------------------------------------
1816

19-
def driver_store():
20-
return list(_driver_locations)
17+
logger = get_logger("DriverLocationMain")
2118

22-
# In-memory match results
23-
_match_results: list[MatchResultEvent] = []
24-
25-
def match_results_store():
26-
return _match_results
27-
28-
29-
# Surge lookup (injected later; stub for now)
30-
def surge_lookup(zone_id: str) -> float:
31-
# In production → query pricing service cache
32-
return 1.0
33-
34-
35-
# --------------------------------------------------------------------
36-
# SERVICE INITIALIZATION
37-
# --------------------------------------------------------------------
38-
39-
logger = get_logger("DispatchMain")
4019
event_bus = EventBus()
20+
driver_store = DriverLocationStore()
4121

4222
app = FastAPI(
43-
title="Dispatch Service",
44-
description="Driver-rider matching microservice.",
23+
title="Driver Location Service",
24+
description="Real-time driver location ingestion service for the ride-sharing platform.",
4525
version="1.0.0",
4626
)
4727

4828

49-
# --------------------------------------------------------------------
29+
# ------------------------------------------------------------
5030
# STARTUP SEQUENCE
51-
# --------------------------------------------------------------------
31+
# ------------------------------------------------------------
5232

5333
@app.on_event("startup")
5434
async def startup_event():
5535

56-
logger.info("Starting Dispatch Service...")
36+
logger.info("Starting Driver Location Service...")
5737

58-
# Inject stores into API router
59-
global DRIVER_STORE, MATCH_RESULTS_STORE
38+
# Inject store into API router
39+
global DRIVER_STORE
6040
DRIVER_STORE = driver_store
61-
MATCH_RESULTS_STORE = _match_results
6241

63-
# Initialize producer and consumer
64-
producer = DispatchProducer(event_bus)
65-
consumer = DispatchConsumer(
42+
# Initialize consumer
43+
consumer = DriverLocationConsumer(
6644
event_bus=event_bus,
67-
driver_store=driver_store,
68-
surge_lookup=surge_lookup,
45+
store=driver_store
6946
)
7047

71-
# Subscribe to trip requests
72-
await event_bus.subscribe("trip_requests", consumer.handle_trip_request)
73-
74-
# Subscribe to match results
75-
async def match_result_listener(data):
76-
event = MatchResultEvent(**data)
77-
_match_results.append(event)
78-
logger.info(f"[DISPATCH] Stored match result for rider {event.rider_id}")
79-
80-
await event_bus.subscribe("match_results", match_result_listener)
81-
82-
# Start background async tasks
83-
asyncio.create_task(producer.start())
48+
# Subscribe to driver location update events
49+
await event_bus.subscribe(
50+
"driver_location_updates",
51+
consumer.handle_driver_location
52+
)
8453

85-
logger.info("Dispatch Service started successfully.")
54+
logger.info("Driver Location Service started successfully.")
8655

8756

88-
# --------------------------------------------------------------------
57+
# ------------------------------------------------------------
8958
# ROUTER
90-
# --------------------------------------------------------------------
59+
# ------------------------------------------------------------
9160

92-
app.include_router(router, prefix="/dispatch")
61+
app.include_router(router, prefix="/driver-location")
9362

9463

95-
# --------------------------------------------------------------------
96-
# LOCAL RUN
97-
# --------------------------------------------------------------------
64+
# ------------------------------------------------------------
65+
# LOCAL RUNNER
66+
# ------------------------------------------------------------
9867

9968
if __name__ == "__main__":
10069
import uvicorn
70+
10171
uvicorn.run(
102-
"src.dispatch-service.main:app",
72+
"src.driver-location-service.main:app",
10373
host="0.0.0.0",
104-
port=8002,
74+
port=8003,
10575
reload=True
10676
)

0 commit comments

Comments
 (0)