Skip to content

Commit a6330ee

Browse files
Merge branch 'fix/deployment'
2 parents ecd4420 + 6ac466c commit a6330ee

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

backend/routes/predict.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,15 @@ function estimateTravelTime(distanceKm, startTime, city) {
5959
return minutes > 5 ? minutes : 5;
6060
}
6161

62+
// This file now exclusively serves as an Express route handler for local development or a traditional server.
6263
export const predictRoute = (req, res) => {
64+
const requestId = `req-${Date.now()}-${Math.random().toString(36).substring(2, 7)}`;
65+
console.log(`[predict.js] [${requestId}] Received prediction request.`);
6366
try {
6467
const { from, to, startTime, city } = req.body || {};
6568

6669
if (!from || !to || !startTime || !city) {
70+
console.warn(`[predict.js] [${requestId}] Bad Request: Missing required fields. Body:`, req.body);
6771
return res.status(400).json({ error: 'Missing required fields: from, to, startTime, city' });
6872
}
6973

@@ -73,6 +77,7 @@ export const predictRoute = (req, res) => {
7377
from.lat < -90 || from.lat > 90 || to.lat < -90 || to.lat > 90 ||
7478
from.lon < -180 || from.lon > 180 || to.lon < -180 || to.lon > 180
7579
) {
80+
console.warn(`[predict.js] [${requestId}] Bad Request: Invalid coordinates. From:`, from, "To:", to);
7681
return res.status(400).json({ error: 'Invalid coordinates' });
7782
}
7883

@@ -81,9 +86,11 @@ export const predictRoute = (req, res) => {
8186
const cacheKey = createCacheKey(from, to, startTime, cityKey);
8287
const cached = predictionCache.get(cacheKey);
8388
if (cached !== undefined) {
89+
console.log(`[predict.js] [${requestId}] Cache HIT for key: ${cacheKey}`);
8490
return res.json({ ...cached, cached: true });
8591
}
8692

93+
console.log(`[predict.js] [${requestId}] Cache MISS for key: ${cacheKey}. Calculating new prediction.`);
8794
const distanceKm = calculateDistance(from.lat, from.lon, to.lat, to.lon);
8895
const minutes = estimateTravelTime(distanceKm, startTime, cityKey);
8996

@@ -98,9 +105,11 @@ export const predictRoute = (req, res) => {
98105
};
99106

100107
predictionCache.set(cacheKey, prediction);
108+
console.log(`[predict.js] [${requestId}] Prediction successful. Result:`, prediction);
101109
return res.json({ ...prediction, cached: false });
102110
} catch (error) {
103-
console.error('Prediction error:', error);
111+
const errorMessage = error instanceof Error ? error.message : String(error);
112+
console.error(`[predict.js] [${requestId}] Prediction error: ${errorMessage}`, { error });
104113
return res.status(500).json({
105114
error: 'Internal server error',
106115
message: error?.message ?? String(error),

backend/server.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,22 @@ dotenv.config();
99
const app = express();
1010
const PORT = process.env.PORT || 8000;
1111

12+
const allowedOrigins = (process.env.FRONTEND_URL || 'http://localhost:3000')
13+
.split(',')
14+
.map(origin => origin.trim());
15+
16+
console.log(`[server.js]:${allowedOrigins}`)
1217
// Middleware
1318
app.use(cors({
14-
origin: process.env.FRONTEND_URL || 'http://localhost:3000',
15-
credentials: true
19+
origin: (origin, callback) => {
20+
// Allow requests with no origin (like mobile apps or curl requests)
21+
if (!origin || allowedOrigins.includes(origin)) {
22+
callback(null, true);
23+
} else {
24+
callback(new Error('Not allowed by CORS'));
25+
}
26+
},
27+
credentials: true,
1628
}));
1729
app.use(express.json());
1830
app.use(express.urlencoded({ extended: true }));
@@ -66,6 +78,7 @@ app.use('*', (req, res) => {
6678
if (process.env.NODE_ENV !== 'test') {
6779
app.listen(PORT, () => {
6880
console.log(`🚀 GoPredict API Server running on port ${PORT}`);
81+
console.log(`✅ CORS enabled for origins: ${allowedOrigins.join(', ')}`);
6982
console.log(`📊 Health check: http://localhost:${PORT}/api/health`);
7083
console.log(`🔮 Prediction endpoint: http://localhost:${PORT}/api/predict`);
7184
});

0 commit comments

Comments
 (0)