-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
529 lines (448 loc) · 23.5 KB
/
Copy pathserver.js
File metadata and controls
529 lines (448 loc) · 23.5 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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
const express = require("express");
const nodemailer = require("nodemailer");
const cors = require("cors");
const rateLimit = require("express-rate-limit");
const helmet = require("helmet");
const jwt = require("jsonwebtoken");
const path = require("path");
require("dotenv").config();
const { db, initDB } = require("./db");
const app = express();
const compression = require("compression");
app.use(compression());
app.use(helmet({ contentSecurityPolicy: false }));
app.use(cors());
app.use(express.json());
const contactLimiter = rateLimit({ windowMs: 15 * 60 * 1000, max: 5 });
const adminLimiter = rateLimit({ windowMs: 15 * 60 * 1000, max: 50 });
app.use("/api/contact", contactLimiter);
const JWT_SECRET = process.env.JWT_SECRET;
if (!JWT_SECRET) {
throw new Error("JWT_SECRET environment variable is missing.");
}
function requireAdmin(req, res, next) {
const auth = req.headers.authorization;
if (!auth || !auth.startsWith("Bearer ")) return res.status(401).json({ error: "Unauthorized" });
try {
jwt.verify(auth.slice(7), JWT_SECRET);
next();
} catch {
res.status(401).json({ error: "Invalid or expired token" });
}
}
// ── Email ───────────────────────────────────────────────────────────────────
const transporter = nodemailer.createTransport({
service: "gmail",
auth: { user: process.env.EMAIL_USER, pass: process.env.EMAIL_PASS },
});
// ═══════════════════════════════════════════════════════════════════════
// STATIC FILES — must come BEFORE any catch-all routes
// ═══════════════════════════════════════════════════════════════════════
// Serve admin panel static files FIRST
const staticOptions = {
maxAge: "1y",
setHeaders: (res, filePath) => {
const ext = path.extname(filePath).toLowerCase();
if (ext === ".html" || ext === ".json" || ext === ".xml") {
res.setHeader("Cache-Control", "public, max-age=0, must-revalidate");
} else {
res.setHeader("Cache-Control", "public, max-age=31536000, immutable");
}
}
};
app.use("/admin", express.static(path.join(__dirname, "admin"), staticOptions));
// Serve uploaded project images
app.use("/img/projects", express.static(path.join(__dirname, "img", "projects"), staticOptions));
// Serve main portfolio static files
app.use(express.static(__dirname, staticOptions));
// ═══════════════════════════════════════════════════════════════════════
// PUBLIC API
// ═══════════════════════════════════════════════════════════════════════
app.get("/api/projects", async (req, res) => {
try {
const result = await db.execute("SELECT * FROM projects WHERE visible=1 ORDER BY sort_order ASC");
const projects = result.rows.map(p => ({
...p,
tech_tags: p.tech_tags ? p.tech_tags.split(",").map(t => t.trim()).filter(Boolean) : [],
}));
res.json(projects);
} catch (e) {
console.error(e);
res.status(500).json({ error: "DB error" });
}
});
app.get("/api/skills", async (req, res) => {
try {
const result = await db.execute("SELECT * FROM skills WHERE visible=1 ORDER BY category, sort_order ASC");
const grouped = { frontend: [], backend: [], others: [] };
for (const s of result.rows) {
const cat = (s.category || "frontend").toLowerCase();
if (grouped[cat]) grouped[cat].push(s);
}
res.json(grouped);
} catch (e) {
res.status(500).json({ error: "DB error" });
}
});
app.get("/api/content", async (req, res) => {
try {
const result = await db.execute("SELECT key, value FROM portfolio_content");
const content = {};
for (const r of result.rows) content[r.key] = r.value;
res.json(content);
} catch (e) {
res.status(500).json({ error: "DB error" });
}
});
app.post("/api/contact", async (req, res) => {
try {
const { name, email, message } = req.body;
if (!name || !email || !message) return res.status(400).json({ error: "All fields are required" });
await db.execute({ sql: "INSERT INTO messages (name, email, message) VALUES (?,?,?)", args: [name, email, message] });
if (process.env.EMAIL_USER && process.env.EMAIL_PASS) {
await transporter.sendMail({
from: process.env.EMAIL_USER,
to: "ankit5242raj1@outlook.com",
subject: `Portfolio Contact: ${name}`,
text: `Name: ${name}\nEmail: ${email}\nMessage: ${message}`,
});
}
res.status(200).json({ message: "Message sent successfully" });
} catch (error) {
console.error("Contact error:", error);
res.status(500).json({ error: "Failed to send message" });
}
});
app.post("/api/analytics/track", async (req, res) => {
try {
const { page_path, referer } = req.body;
// Get visitor IP
let ip = req.headers["x-forwarded-for"] || req.socket.remoteAddress || "127.0.0.1";
if (ip.includes(",")) {
ip = ip.split(",")[0].trim();
}
const userAgent = req.headers["user-agent"] || "Unknown";
// Check Vercel geolocational headers
let country = req.headers["x-vercel-ip-country"] || "Unknown";
let city = req.headers["x-vercel-ip-city"] || "Unknown";
let lat = req.headers["x-vercel-ip-latitude"] ? parseFloat(req.headers["x-vercel-ip-latitude"]) : null;
let lon = req.headers["x-vercel-ip-longitude"] ? parseFloat(req.headers["x-vercel-ip-longitude"]) : null;
// If local environment (localhost, private network, loopback, or missing coordinates)
const isLocalIP = ip === "::1" || ip === "127.0.0.1" || ip.startsWith("10.") || ip.startsWith("192.168.") || ip.startsWith("172.");
if (isLocalIP) {
// Mock coordinates for Muzaffarpur, Bihar, India (or New Delhi) for visual confirmation
country = "India";
city = "Muzaffarpur (Local Dev)";
lat = 26.2201;
lon = 85.3837;
} else if (!lat || !lon) {
// If deployed but Vercel headers are missing, fallback to standard geolocation lookup using node-fetch equivalent
try {
const geoResponse = await fetch(`http://ip-api.com/json/${ip}`).then(r => r.json());
if (geoResponse && geoResponse.status === "success") {
country = geoResponse.country || country;
city = geoResponse.city || city;
lat = geoResponse.lat || lat;
lon = geoResponse.lon || lon;
}
} catch (geoErr) {
console.error("External geolocation failed:", geoErr);
}
}
await db.execute({
sql: "INSERT INTO visitor_logs (ip_address, user_agent, country, city, lat, lon, page_path, referer) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
args: [ip, userAgent, country, city, lat || 0.0, lon || 0.0, page_path || "/", referer || "Direct"]
});
res.status(200).json({ status: "success" });
} catch (error) {
console.error("Analytics tracking error:", error);
res.status(500).json({ error: "Failed to track visit" });
}
});
// ═══════════════════════════════════════════════════════════════════════
// ADMIN AUTH
// ═══════════════════════════════════════════════════════════════════════
app.post("/api/admin/login", adminLimiter, async (req, res) => {
const { username, password } = req.body;
if (!username || !password) {
return res.status(400).json({ error: "Username and password are required." });
}
try {
const result = await db.execute({
sql: "SELECT * FROM admin_users WHERE username = ?",
args: [username]
});
if (result.rows.length === 0) {
return res.status(401).json({ error: "Invalid credentials" });
}
const user = result.rows[0];
const bcrypt = require("bcryptjs");
const match = await bcrypt.compare(password, user.password_hash);
if (!match) {
return res.status(401).json({ error: "Invalid credentials" });
}
const token = jwt.sign({ username, role: "admin" }, JWT_SECRET, { expiresIn: "24h" });
res.json({ token, username });
} catch (error) {
console.error("Login error:", error);
res.status(500).json({ error: "Authentication system failure." });
}
});
app.post("/api/admin/change-password", requireAdmin, async (req, res) => {
const { currentPassword, newPassword } = req.body;
if (!currentPassword || !newPassword) {
return res.status(400).json({ error: "Current and new passwords are required." });
}
try {
const auth = req.headers.authorization;
const decoded = jwt.verify(auth.slice(7), JWT_SECRET);
const username = decoded.username;
const result = await db.execute({
sql: "SELECT * FROM admin_users WHERE username = ?",
args: [username]
});
if (result.rows.length === 0) {
return res.status(404).json({ error: "User not found" });
}
const user = result.rows[0];
const bcrypt = require("bcryptjs");
const match = await bcrypt.compare(currentPassword, user.password_hash);
if (!match) {
return res.status(400).json({ error: "Incorrect current password" });
}
const newHash = await bcrypt.hash(newPassword, 10);
await db.execute({
sql: "UPDATE admin_users SET password_hash = ? WHERE username = ?",
args: [newHash, username]
});
res.json({ message: "Password updated successfully" });
} catch (error) {
console.error("Change password error:", error);
res.status(500).json({ error: "Internal server error" });
}
});
// ═══════════════════════════════════════════════════════════════════════
// ADMIN — PROJECTS
// ═══════════════════════════════════════════════════════════════════════
app.get("/api/admin/projects", requireAdmin, async (req, res) => {
const result = await db.execute("SELECT * FROM projects ORDER BY sort_order ASC");
res.json(result.rows.map(p => ({ ...p, tech_tags: p.tech_tags ? p.tech_tags.split(",").map(t => t.trim()) : [] })));
});
app.post("/api/admin/projects", requireAdmin, async (req, res) => {
try {
const { title, description, image_url, demo_url, github_url, tech_tags, badge, sort_order, visible } = req.body;
const tags = Array.isArray(tech_tags) ? tech_tags.join(",") : (tech_tags || "");
const result = await db.execute({
sql: "INSERT INTO projects (title,description,image_url,demo_url,github_url,tech_tags,badge,sort_order,visible) VALUES (?,?,?,?,?,?,?,?,?)",
args: [title, description || "", image_url || "", demo_url || "", github_url || "", tags, badge || "Live", sort_order || 0, visible !== false ? 1 : 0],
});
res.json({ id: Number(result.lastInsertRowid), message: "Created" });
} catch (e) { res.status(500).json({ error: e.message }); }
});
app.put("/api/admin/projects/:id", requireAdmin, async (req, res) => {
try {
const { title, description, image_url, demo_url, github_url, tech_tags, badge, sort_order, visible } = req.body;
const tags = Array.isArray(tech_tags) ? tech_tags.join(",") : (tech_tags || "");
await db.execute({
sql: "UPDATE projects SET title=?,description=?,image_url=?,demo_url=?,github_url=?,tech_tags=?,badge=?,sort_order=?,visible=? WHERE id=?",
args: [title, description || "", image_url || "", demo_url || "", github_url || "", tags, badge || "Live", sort_order || 0, visible !== false ? 1 : 0, req.params.id],
});
res.json({ message: "Updated" });
} catch (e) { res.status(500).json({ error: e.message }); }
});
app.delete("/api/admin/projects/:id", requireAdmin, async (req, res) => {
await db.execute({ sql: "DELETE FROM projects WHERE id=?", args: [req.params.id] });
res.json({ message: "Deleted" });
});
// ═══════════════════════════════════════════════════════════════════════
// ADMIN — SKILLS
// ═══════════════════════════════════════════════════════════════════════
app.get("/api/admin/skills", requireAdmin, async (req, res) => {
const result = await db.execute("SELECT * FROM skills ORDER BY category, sort_order ASC");
res.json(result.rows);
});
app.post("/api/admin/skills", requireAdmin, async (req, res) => {
try {
const { name, icon_class, icon_url, category, sort_order } = req.body;
const result = await db.execute({
sql: "INSERT INTO skills (name,icon_class,icon_url,category,sort_order,visible) VALUES (?,?,?,?,?,1)",
args: [name, icon_class || "", icon_url || "", category || "frontend", sort_order || 0],
});
res.json({ id: Number(result.lastInsertRowid), message: "Created" });
} catch (e) { res.status(500).json({ error: e.message }); }
});
app.put("/api/admin/skills/:id", requireAdmin, async (req, res) => {
try {
const { name, icon_class, icon_url, category, sort_order, visible } = req.body;
await db.execute({
sql: "UPDATE skills SET name=?,icon_class=?,icon_url=?,category=?,sort_order=?,visible=? WHERE id=?",
args: [name, icon_class || "", icon_url || "", category || "frontend", sort_order || 0, visible !== false ? 1 : 0, req.params.id],
});
res.json({ message: "Updated" });
} catch (e) { res.status(500).json({ error: e.message }); }
});
app.delete("/api/admin/skills/:id", requireAdmin, async (req, res) => {
await db.execute({ sql: "DELETE FROM skills WHERE id=?", args: [req.params.id] });
res.json({ message: "Deleted" });
});
// ═══════════════════════════════════════════════════════════════════════
// ADMIN — MESSAGES
// ═══════════════════════════════════════════════════════════════════════
app.get("/api/admin/messages", requireAdmin, async (req, res) => {
const result = await db.execute("SELECT * FROM messages ORDER BY created_at DESC");
res.json(result.rows);
});
app.put("/api/admin/messages/:id/read", requireAdmin, async (req, res) => {
await db.execute({ sql: "UPDATE messages SET read=1 WHERE id=?", args: [req.params.id] });
res.json({ message: "Marked as read" });
});
app.delete("/api/admin/messages/:id", requireAdmin, async (req, res) => {
await db.execute({ sql: "DELETE FROM messages WHERE id=?", args: [req.params.id] });
res.json({ message: "Deleted" });
});
// ═══════════════════════════════════════════════════════════════════════
// ADMIN — ANALYTICS & ACTIVITY
// ═══════════════════════════════════════════════════════════════════════
app.get("/api/admin/analytics", requireAdmin, async (req, res) => {
try {
// Get total views count
const totalViewsRes = await db.execute("SELECT COUNT(*) as count FROM visitor_logs");
const totalViews = totalViewsRes.rows[0].count;
// Get unique visitors count (by IP address)
const uniqueVisitorsRes = await db.execute("SELECT COUNT(DISTINCT ip_address) as count FROM visitor_logs");
const uniqueVisitors = uniqueVisitorsRes.rows[0].count;
// Get last 100 visitor logs to plot on Leaflet
const recentLogsRes = await db.execute("SELECT * FROM visitor_logs ORDER BY created_at DESC LIMIT 100");
res.json({
totalViews,
uniqueVisitors,
recentLogs: recentLogsRes.rows
});
} catch (error) {
console.error("Admin analytics fetch error:", error);
res.status(500).json({ error: "Failed to load analytics details" });
}
});
app.get("/api/admin/recent-activity", requireAdmin, async (req, res) => {
try {
// 1. Get latest projects (latest 10)
const projectsRes = await db.execute("SELECT id, title, created_at FROM projects ORDER BY created_at DESC LIMIT 10");
const projects = projectsRes.rows.map(p => ({
type: "project",
title: `Project Payload Deployed`,
description: `New project "${p.title}" successfully compiled and committed to database core.`,
time: p.created_at
}));
// 2. Get latest messages (latest 10)
const messagesRes = await db.execute("SELECT id, name, created_at FROM messages ORDER BY created_at DESC LIMIT 10");
const messages = messagesRes.rows.map(m => ({
type: "message",
title: `Transmission Intercepted`,
description: `New incoming signal intercepted from operator "${m.name}". Secure logs committed to index.`,
time: m.created_at
}));
// 3. Get latest visitors (latest 10)
const visitorsRes = await db.execute("SELECT id, city, country, page_path, created_at FROM visitor_logs ORDER BY created_at DESC LIMIT 10");
const visitors = visitorsRes.rows.map(v => ({
type: "visitor",
title: `Remote Session Logged`,
description: `Connection tracked at page "${v.page_path}" originating from location: ${v.city}, ${v.country}.`,
time: v.created_at
}));
// Chronologically merge them
const merged = [...projects, ...messages, ...visitors];
// Sort descending by time
merged.sort((a, b) => {
const aTime = a.time ? new Date(a.time).getTime() : 0;
const bTime = b.time ? new Date(b.time).getTime() : 0;
return bTime - aTime;
});
res.json(merged.slice(0, 15)); // return latest 15 activities
} catch (error) {
console.error("Admin activity fetch error:", error);
res.status(500).json({ error: "Failed to compile recent activity list" });
}
});
// ═══════════════════════════════════════════════════════════════════════
// ADMIN — CONTENT
// ═══════════════════════════════════════════════════════════════════════
app.get("/api/admin/content", requireAdmin, async (req, res) => {
const result = await db.execute("SELECT key, value FROM portfolio_content");
const content = {};
for (const r of result.rows) content[r.key] = r.value;
res.json(content);
});
app.put("/api/admin/content", requireAdmin, async (req, res) => {
try {
for (const [key, value] of Object.entries(req.body)) {
await db.execute({
sql: "INSERT INTO portfolio_content (key,value) VALUES (?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value",
args: [key, value],
});
}
res.json({ message: "Updated" });
} catch (e) { res.status(500).json({ error: e.message }); }
});
// ═══════════════════════════════════════════════════════════════════════
// PUBLIC — CERTIFICATES
// ═══════════════════════════════════════════════════════════════════════
app.get("/api/certificates", async (req, res) => {
try {
const result = await db.execute("SELECT * FROM certificates WHERE visible=1 ORDER BY sort_order ASC, created_at DESC");
res.json(result.rows);
} catch (e) { res.status(500).json({ error: "DB error" }); }
});
// ═══════════════════════════════════════════════════════════════════════
// ADMIN — CERTIFICATES
// ═══════════════════════════════════════════════════════════════════════
app.get("/api/admin/certificates", requireAdmin, async (req, res) => {
const result = await db.execute("SELECT * FROM certificates ORDER BY sort_order ASC, created_at DESC");
res.json(result.rows);
});
app.post("/api/admin/certificates", requireAdmin, async (req, res) => {
try {
const { title, issuer, issue_date, credential_url, image_url, type, sort_order } = req.body;
const result = await db.execute({
sql: "INSERT INTO certificates (title,issuer,issue_date,credential_url,image_url,type,sort_order,visible) VALUES (?,?,?,?,?,?,?,1)",
args: [title, issuer || "", issue_date || "", credential_url || "", image_url || "", type || "certificate", sort_order || 0],
});
res.json({ id: Number(result.lastInsertRowid), message: "Created" });
} catch (e) { res.status(500).json({ error: e.message }); }
});
app.put("/api/admin/certificates/:id", requireAdmin, async (req, res) => {
try {
const { title, issuer, issue_date, credential_url, image_url, type, sort_order, visible } = req.body;
await db.execute({
sql: "UPDATE certificates SET title=?,issuer=?,issue_date=?,credential_url=?,image_url=?,type=?,sort_order=?,visible=? WHERE id=?",
args: [title, issuer || "", issue_date || "", credential_url || "", image_url || "", type || "certificate", sort_order || 0, visible !== false ? 1 : 0, req.params.id],
});
res.json({ message: "Updated" });
} catch (e) { res.status(500).json({ error: e.message }); }
});
app.delete("/api/admin/certificates/:id", requireAdmin, async (req, res) => {
await db.execute({ sql: "DELETE FROM certificates WHERE id=?", args: [req.params.id] });
res.json({ message: "Deleted" });
});
// ── Admin page route ─────────────────────────────────────────────────────────
app.get("/admin", (req, res) => {
res.sendFile(path.join(__dirname, "admin", "index.html"));
});
// 404
app.use((req, res) => {
res.status(404).sendFile(path.join(__dirname, "404.html"));
});
const PORT = process.env.PORT || 3000;
// Export the Express app for Vercel
module.exports = app;
// Only listen if not running on Vercel
if (process.env.VERCEL !== "1") {
initDB().then(() => {
app.listen(PORT, () => {
console.log(`🚀 Portfolio: http://localhost:${PORT}`);
console.log(`🔐 Admin: http://localhost:${PORT}/admin`);
});
}).catch(err => { console.error("DB init failed:", err); process.exit(1); });
} else {
// Initialize DB asynchronously for Vercel (creates tables/data if they don't exist)
initDB().catch(err => console.error("DB init failed:", err));
}