How vigneshpy/railvazhi works
Purpose and workspace
Rail Vazhi predicts railway-level-crossing closure windows along a user’s road route, then recommends whether to leave or wait. It is a pnpm monorepo with a browser client, HTTP API, and shared TypeScript contracts:
• apps/web
• apps/api
• packages/shared/src/index.ts
Entry points and request surface
The API process is initialized in apps/api/src/index.ts and exposes focused route handlers:
• Prediction orchestration: apps/api/src/routes/predict.ts
• Train-related data endpoints: apps/api/src/routes/trains.ts
• Gate endpoints: apps/api/src/routes/gates.ts
The Next.js App Router client begins at apps/web/src/app/layout.tsx and apps/web/src/app/page.tsx. The browser calls the API URL configured through NEXT_PUBLIC_API_URL.
Prediction pipeline
POST /api/predict is the central control flow. It accepts origin/destination coordinates, obtains a road route, identifies crossings near that route, and produces closure predictions plus a travel recommendation.
Core stages are implemented in:
• Road routing via OSRM: apps/api/src/services/routing.ts
• Spatial crossing lookup: apps/api/src/services/gates.ts
• Live/scheduled closure calculation: apps/api/src/services/prediction.ts
• Aggregated response endpoint: apps/api/src/routes/predict.ts
The important boundary is that the API owns all external-data, spatial, and prediction logic; the web app renders returned route/gate/recommendation data rather than computing closures itself.
Geospatial persistence
PostgreSQL 16 with PostGIS is the authoritative local dataset for stations and level crossings. Schema, migrations, and seeded corridor data reside in:
• apps/api/src/db/schema.sql
• apps/api/src/db/migrate.ts
• apps/api/src/db/seed.sql
The database client is centralized in apps/api/src/db/client.ts. Route-to-gate matching uses PostGIS spatial proximity (ST_DWithin), isolating geometry querying from HTTP handlers and routing providers.
Train-data and fallback boundary
Train timing is sourced from NTES scraping and falls back to schedule-based information when live data is unavailable. This provider behavior is isolated in:
• apps/api/src/services/ntes.ts
• apps/api/src/services/prediction.ts
Predictions convey confidence such as Live versus Scheduled. This containment allows NTES/provider changes without altering the frontend or spatial gate query layer.
Web interaction and notification model
The mobile-oriented frontend gathers locations, renders the route and predicted gates, and shows the recommendation:
• Route input/location selection: apps/web/src/components/RouteForm.tsx, apps/web/src/components/LocationPickerModal.tsx
• Map rendering: apps/web/src/components/RouteMap.tsx
• Gate and recommendation presentation: apps/web/src/components/GateCard.tsx, apps/web/src/components/RecommendationBanner.tsx
Saved routes are browser-local rather than server accounts, through apps/web/src/hooks/useFavourites.ts. Route monitoring and notifications are client-side via apps/web/src/hooks/useRouteWatcher.ts, apps/web/src/components/NotificationToggle.tsx, and the service worker at apps/web/public/sw.js.
Runtime and deployment dependencies
The API runs Node.js 20, Express, and strict TypeScript; the web runtime is Next.js 14 with Leaflet/OpenStreetMap mapping. External services are OSRM’s public routing server, Nominatim for search/reverse geocoding, and NTES for train data. Expected deployment separates PostGIS (Supabase), API hosting (Railway; apps/api/railway.toml), and web hosting (Vercel), with API CORS restricted by WEB_ORIGIN.
Main entry points: page.tsx Next.js app · index.ts Express API · predict.ts Prediction endpoint · prediction.ts Closure prediction
Components 18
Browser client
- Next.js app · Collects routes and renders API results.
apps/web/src/app/page.tsx - Route input
apps/web/src/components/RouteForm.tsx - Location picker
apps/web/src/components/LocationPickerModal.tsx - Route map
apps/web/src/components/RouteMap.tsx - Gate & recommendation UI
apps/web/src/components/GateCard.tsx - Route watcher · Monitors saved routes and triggers notifications.
apps/web/src/hooks/useRouteWatcher.ts - Notification worker
apps/web/public/sw.js
API service
- Express API · CORS-restricted HTTP boundary for the web client.
apps/api/src/index.ts - Prediction endpoint · Orchestrates routing, gate matching, closure prediction, and recommendation.
apps/api/src/routes/predict.ts - Road routing
apps/api/src/services/routing.ts - Spatial gate lookup · Finds crossings near the road route with ST_DWithin.
apps/api/src/services/gates.ts - Closure prediction · Calculates live or scheduled closure windows and confidence.
apps/api/src/services/prediction.ts - Train & gate endpoints
apps/api/src/routes/trains.ts - NTES provider · Supplies live train timing; schedules are the fallback.
apps/api/src/services/ntes.ts
Data providers
- Stations & crossings · Authoritative local geospatial dataset.
apps/api/src/db/schema.sql - OSRM
- Nominatim
Other components
- Shared contracts · Shared API data contracts.
packages/shared/src/index.ts
Connections 19
- Next.js app → Route input: collects route
- Route input → Location picker: selects locations
- Location picker → Nominatim: searches/geocodes
- Next.js app → Route map: renders route
- Next.js app → Gate & recommendation UI: renders predictions
- Next.js app → Route watcher: monitors route
- Route watcher → Notification worker: notifies
- Next.js app → Express API: NEXT_PUBLIC_API_URL
- Express API → Prediction endpoint: POST /api/predict
- Express API → Train & gate endpoints: train/gate requests
- Prediction endpoint → Road routing: road route
- Road routing → OSRM: routing request
- Prediction endpoint → Spatial gate lookup: route geometry
- Spatial gate lookup → Stations & crossings: spatial proximity query
- Prediction endpoint → Closure prediction: crossing predictions
- Closure prediction → NTES provider: live timing
- Closure prediction → Stations & crossings: scheduled fallback data
- Next.js app → Shared contracts: uses contracts
- Express API → Shared contracts: uses contracts