-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
496 lines (415 loc) · 15.4 KB
/
Copy pathserver.js
File metadata and controls
496 lines (415 loc) · 15.4 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// server.js
import express from "express";
import cors from "cors";
import bodyParser from "body-parser";
import mongoose from "mongoose";
import twilio from 'twilio';
import vision from '@google-cloud/vision';
import bcrypt from 'bcrypt';
import axios from 'axios';
import path from "path";
import dotenv from 'dotenv';
import { fileURLToPath } from 'url';
import console, { count, error } from "console";
import { type } from "os";
import multer from 'multer';
import { MachineLearningModel } from './machineLearningModel.js';
import { unique } from "@tensorflow/tfjs";
dotenv.config(); // Load environment variables
const app = express();
const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
// Serve static files from the frontend 'public' directory
app.use(express.static(path.join(dirname, '../frontend/public')));
// Environment variables
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN; // Use the correct auth token
const twilioNumber = process.env.TWILIO_NUMBER;
const emergencyContact = process.env.EMERGENCY_CONTACT;
const emailpassword = process.env.EMAIL_PASSWORD;
const mongourl = process.env.MONGODB_URI;
const nasaApi = process.env.NASA_API_KEY;
const client = new twilio(accountSid, authToken);
// Example to send a message (this will only work with test numbers)
client.messages
.create({
body: 'Hello from Twilio!',
from: '+2349065692168', // Your Twilio test number
to: '+234 814 314 6543', // A test number
})
.then((message) => console.log(message.sid))
.catch((error) => console.error(error));
if (!accountSid || !authToken) {
console.error('Twilio credentials are missing.');
process.exit(1);
}
// Middleware
app.use(cors());
app.use(bodyParser.json());
async function testMongoConnection() {
try {
await mongoose.connect(mongourl, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log("MongoDB connection successful!");
} catch (err) {
console.error("Failed to connect to MongoDB:", err);
} finally {
mongoose.disconnect();
}
}
testMongoConnection();
// Connect to MongoDB
mongoose.connect(mongourl, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 300000, // Increase timeout to 20 seconds
socketTimeoutMS: 4500000, // Increase socket timeout
})
.then(() => console.log("MongoDB connected successfully"))
.catch((err) => console.error('Database connection error:', err));
const userSchema = new mongoose.Schema({
name: {
type: String,
require: true
},
email: {
type: String,
require: true,
unique: true
},
password: {
type: String,
require: true
}
});
// Define schema for SOS messages
const sosMessageSchema = new mongoose.Schema({
message: String,
dateSent: { type: Date, default: Date.now },
});
// Create models
const User = mongoose.model('User', userSchema);
const SosMessage = mongoose.model('SosMessage', sosMessageSchema);
// Generating password reset token
function generatePasswordResetToken() {
return Math.random().toString(36).substr(2, 10);
}
// Send password reset email
function sendPasswordResetEmail( email, token) {
const transporter = nodemailer.createTransport({
host: '(link unavailable)',
port: 587,
secure: false,
auth: {
user: 'mathewoloyede100@gmail.com',
pass: emailpassword
}
});
const mailOptions = {
from: 'mathewoloyede100@gmail.com',
to: email,
subject: 'Password Reset',
text: `Reset your password: http://localhost:3000/reset-password?token=${token}`
};
transporter.sendMail(mailOptions, (err, info) => {
if (err) {
console.log(err);
} else {
console.log('Email sent', info.response);
}
});
}
// Forgot Password Route
app.post('/forgot-password', async (req, res) => {
const { email } = req.body;
const user = await User.findOne({ email });
if (!user) {
return res.status(404).send('User not found');
}
const token = generatePasswordResetToken();
await User.updateOne({ email }, { passwordResetToken: token });
sendPasswordResetEmail(user.email, token);
res.send('Password reset email sent');
});
// Reset Password Route
app.post('/reset-password', async (req, res) => {
const { token, newPassword } = req.body;
const user = await User.findOne({ passwordResetToken: token });
if (!user) {
return res.status(404).send("Invalid token");
}
user.password = bcrypt.hashSync(newPassword, 10);
user.passwordResetToken = null;
await user.save();
res.send('Password updated successfully');
})
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Serve index.html on the root route
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '../frontend/public/index.html'));
});
// SignIn API
app.post("/signin", async (req, res) => {
const { email, password } = req.body;
try {
const user = await User.findOne({ email });
if (!user) return res.status(401).json({ error: 'Invalid email or password' });
const isValidPassword = await bcrypt.compare(password, user.password);
if (!isValidPassword) return res.status(401).json({ error: 'Invalid email or password' });
res.json({ message: 'Signed in successfully', redirect: '/index' });
} catch (error) {
console.error('An error occurred:', error.message);
// res.status(500).json({ error: 'Failed to sign in' });
}
});
app.post('/signup', async (req, res) => {
console.log('Received sign-up request');
const { signUpname, signUpemail, signUppassword } = req.body;
// Validate input
if (!signUpname || !signUpemail || !signUppassword) {
return res.status(400).json({ message: 'All fields are required' });
}
try {
// Check if email already exists using retry logic
const existingUser = await retryWithBackoff(
() => User.findOne({ email: signUpemail }).exec(),
3, // Maximum number of retries
2000 // Initial backoff delay in milliseconds
);
if (existingUser) {
console.log('Email already in use');
return res.status(400).json({ message: 'Email already in use' });
}
// Hash the password
console.log('Hashing password');
const hashedPassword = await bcrypt.hash(signUppassword, 10);
// Create a new user object
const user = new User({
name: signUpname,
email: signUpemail,
password: hashedPassword,
});
// Save the new user using retry logic
await retryWithBackoff(
() => user.save(),
3, // Maximum number of retries
2000 // Initial backoff delay in milliseconds
);
console.log('User saved successfully');
res.status(201).json({ message: 'Signed up successfully', redirect: '/signin' });
} catch (error) {
console.error('Error during sign up:', error);
res.status(500).json({ error: 'Failed to sign up' });
}
});
/**
* Utility function for retrying operations with exponential backoff
* @param {Function} operation - The async operation to retry
* @param {number} maxRetries - Maximum number of retries
* @param {number} initialBackoffDelay - Initial backoff delay in milliseconds
*/
async function retryWithBackoff(operation, maxRetries, initialBackoffDelay) {
let retryCount = 0;
let backoffDelay = initialBackoffDelay;
while (retryCount < maxRetries) {
try {
return await operation();
} catch (error) {
if (error.name === 'MongooseTimeoutError') {
console.error(`Retrying operation after ${backoffDelay}ms delay...`, error);
await new Promise((resolve) => setTimeout(resolve, backoffDelay));
backoffDelay *= 2; // Exponential backoff
retryCount++;
} else {
throw error; // Re-throw non-timeout errors
}
}
}
throw new Error('Maximum number of retries reached. Operation failed.');
}
// Endpoint to forget password
app.post('/forget-password', async (req, res) => {
const { email } = req.body;
const user = await User.findOne({ email });
if(!user) {
return res.status(404).send('User not found');
}
const token = generatePasswordResetToken();
await User.updateOne({ email }, { passwordResetToken: token });
sendPasswordResetEmail( user.email, token);
res.send('Password reset email sent')
});
// Endpoint to send SOS messages
app.post("/api/sos", async (req, res) => {
try {
const sosMessage = new SosMessage({ message: req.body.message });
await sosMessage.save();
// Send SMS via Twilio
client.messages
.create({
body: sosMessage.message,
from: twilioNumber,
to: emergencyContact,
})
.then((message) => {
console.log("SOS message sent:", sosMessage.message);
res.json({ status: "SOS message sent!", message: sosMessage.message });
})
.catch((error) => {
console.error('An error occurred:', error.message);
console.error('Twilio error:', error);
res.status(500).json({ error: "Failed to send SOS message" });
});
} catch (error) {
res.status(500).json({ error: "Failed to send SOS message" });
}
});
// Endpoint to fetch live coverage data
app.get("/api/live-coverage", async (req, res) => {
try {
const response = await axios.get('https://api.spacexdata.com/v4/launches/latest');
const data = response.data;
// Transform the object into an array of relevant details
const liveCoverage = [
`Mission Name: ${data.name || 'N/A'}`,
`Launch Date (UTC): ${data.date_utc || 'N/A'}`,
`Rocket ID: ${data.rocket || 'N/A'}`,
`Launchpad ID: ${data.launchpad || 'N/A'}`,
`Details: ${data.details || 'No details available'}`,
`Webcast: ${data.links?.webcast || 'No webcast link available'}`,
`Wikipedia: ${data.links?.wikipedia || 'No Wikipedia link available'}`,
`Launch Success: ${data.success ? 'Yes' : 'No'}`,
];
res.json(liveCoverage); // Send the array to the client
} catch (error) {
console.error('Error fetching live coverage:', error);
res.status(500).json({ error: "Failed to fetch live coverage." });
}
});
const upload = multer({ dest: './uploads' });
app.post('/api/ai-identifier', upload.single('image'),
async (req, res) => {
try {
const filePath = req.file.path;
// Process the image with Google Vision API
const client = new vision.ImageAnnotatorClient();
const [result] = await client.labelDetection(filePath);
const labels = result.labelAnnotations.map(label => label.description);
// Clean up the file after processing
fs.unlinkSync(filePath);
res.json({ labels });
} catch (error) {
console.error('Error uploading image:', error.message);
res.status(500).json({ error: 'Failed to process image' });
}
});
// Endpoint to fetch space guide data
app.get("/api/space-guide", async (req, res) => {
try {
const response = await axios.get(`https://api.nasa.gov/planetary/apod?api_key=${nasaApi}`);
if (response.status !== 200) {
throw new Error(`NASA API returned status ${response.status}`);
}if (response.status === 200) {
// Successfully fetched data from NASA API
res.json(response.data); // Send it to the frontend
}
} catch (error) {
console.error('Error fetching APOD:', error.message);
res.status(500).json({ error: 'Failed to fetch Astronomy Picture of the Day' });
}
});
// Endpoint to fetch Astronomy Picture of the Day
app.get('/api/apod', async (req, res) => {
try {
const response = await axios.get(`https://api.nasa.gov/planetary/apod?api_key=${nasaApi}`);
res.json(response.data);
} catch (error) {
console.error('Error fetching Astronomy Picture of the Day:', error.message);
res.status(500).json({ error: 'Failed to fetch Astronomy Picture of the Day' });
}
});
// Endpoint to fetch space news
app.get('/api/space-news', async (req, res) => {
try {
const response = await axios.get('https://api.nasa.gov/planetary/apod', {
params: {
api_key: nasaApi,
count: 5,
},
});
console.log("NASA API Response:", response.data);
if (!response.data || !Array.isArray(response.data)) {
return res.status(500).json({ error: "Unexpected API response structure" });
}
const articles = response.data.map(item => ({
title: item.title || 'No Title Available',
description: item.explanation || 'No Description Available',
url: item.url || '#',
}));
res.json(articles);
} catch (error) {
console.error("Error fetching data from NASA API:", error.message);
res.status(500).json({ error: "Failed to fetch space news. Please try again later." });
}
});
// Endpoint to fetch Mars rover photos
app.get('/api/mars-rover-photos', async (req, res) => {
try {
const sol = 1000; // Example Martian day
const response = await axios.get(`https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?sol=${sol}&api_key=${nasaApi}`);
res.json(response.data.photos); // Return the photos array
} catch (error) {
console.error(error);
res.status(500).json({ error: "Failed to fetch Mars rover photos" });
}
});
// Endpoint to fetch upcoming space events (as per previous suggestions)
app.get('/api/upcoming-events',async (req, res) => {
try {
const response = await axios.get('https://launchlibrary.net/1/2/launch?next=1'); // NASA or other launch data
res.json(response.data);
} catch (error) {
console.error("Error fetching NASA launches:", error.message);
res.status(500).json({ error: "Failed to fetch NASA launch data" });
}
});
// Endpoint to fetch NASA mission data
app.get('/api/nasa-missions', async(req, res) => {
try {
const response = await axios.get(`https://api.nasa.gov/planetary/apod?api_key=${nasaApi}`)
res.json(response.data);
} catch (error) {
console.error("Error fetching NASA missions:", error.message);
res.status(500).json({ error: "Failed to fetch NASA missions data" });
}
});
// Endpoint to fetch SpaceX launch data
app.get('/api/spacex-launches', async(req, res) => {
try {
const response = await axios.get('https://api.spacex.com/v4/launches')
res.json(response.data);
} catch (error) {
console.error("Error fetching SpaceX launches:", error.message);
res.status(500).json({ error: "Failed to fetch SpaceX launches data" });
}
});
// ESA spacecraft datm]a
app.get('/api/esa-spacecraft', async(req, res) => {
try {
const response = await axios.get('https://api.esa.int/spacecraft');
res.json(response.data); // Return the spacecraft data to the front-end
} catch (error) {
console.error("Error fetching ESA spacecraft:", error.message);
res.status(500).json({ error: "Failed to fetch ESA spacecraft data" });
}
});
const PORT = process.env.PORT || 3000;
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});