-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
128 lines (112 loc) · 4.62 KB
/
app.js
File metadata and controls
128 lines (112 loc) · 4.62 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
import 'dotenv/config.js';
import express from 'express';
import session from 'express-session';
import path from 'path';
import expressLayouts from 'express-ejs-layouts';
import mongoose from 'mongoose';
import MongoStore from 'connect-mongo';
import User from './models/User.js';
import config from './config/index.js';
import publicRoutes from './routes/public.js';
import authRoutes from './routes/auth.js';
import leaseRoutes from './routes/lease.js';
import dashboardRoutes from './routes/dashboard.js';
import bugRoutes from './routes/bugReport.js';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const app = express();
// Needed because __dirname is not defined in ESM
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Connect to MongoDB
mongoose.connect(config.mongodbUri)
.then(() => {
console.log('✅ Connected to MongoDB');
})
.catch(err => console.error('❌ MongoDB connection error:', err));
// ─────────────────────────────────────────────
// Middleware
// ─────────────────────────────────────────────
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
// View engine setup
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(expressLayouts);
app.set('layout', 'layout');
// Session config
app.use(session({
secret: config.sessionSecret,
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl: config.mongodbUri,
ttl: 24 * 60 * 60 // 1 day
}),
cookie: {
secure: process.env.NODE_ENV === 'production',
maxAge: 24 * 60 * 60 * 1000 // 1 day
}
}));
// Attach user to request if logged in
app.use(async (req, res, next) => {
if (req.session.userId) {
try {
const user = await User.findById(req.session.userId);
if (user) {
req.user = user;
}
} catch (error) {
console.error('Error fetching user:', error);
}
}
next();
});
//Set testing and development variables
app.use((req, res, next) => {
res.locals.isDevelopment = config.isDevelopment;
res.locals.isTesting = config.isTesting;
next();
});
// ─────────────────────────────────────────────
// Routes
// ─────────────────────────────────────────────
app.use('/', publicRoutes);
app.use('/', authRoutes);
app.use('/', leaseRoutes);
app.use('/', dashboardRoutes);
app.use('/', bugRoutes);
app.use('/uploads/screenshots', express.static('uploads/screenshots'));
// ─────────────────────────────────────────────
// Error Handler
// ─────────────────────────────────────────────
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).render('error', { error: 'Something went wrong!' });
});
// ─────────────────────────────────────────────
// Cleanup Tasks
// ─────────────────────────────────────────────
setInterval(async () => {
try {
await User.cleanupUnverified();
await User.anonymizeInactive();
} catch (error) {
console.error('Error in cleanup tasks:', error);
}
}, 15 * 60 * 1000); // Every 15 minutes
// ─────────────────────────────────────────────
// Start Server
// ─────────────────────────────────────────────
app.listen(config.port, () => {
console.log(`🚀 Server is running on port ${config.port}`);
if (config.isDevelopment) {
console.log('🧪 Development mode is active');
console.log(`🔐 Default verification code: ${config.devVerificationCode}`);
}
if (config.isTesting) {
console.log('🧪 Testing mode is active');
console.log(`🔐 Only users from the following domains are permitted: ${config.allowedDomains.join(', ')}`);
}
});