-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
194 lines (172 loc) · 5.46 KB
/
app.js
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
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const { MongoClient, ServerApiVersion } = require('mongodb');
const expressLayouts = require('express-ejs-layouts');
const session = require('express-session');
const MongoStore = require('connect-mongo');
const flash = require('connect-flash');
const passport = require('passport');
const path = require('path');
const fs = require('fs');
const User = require('./models/User');
const app = express();
// Suppress deprecation warning
process.removeAllListeners('warning');
// Passport config
require('./config/passport')(passport);
// MongoDB Connection URI
const uri = "mongodb+srv://stream:telvinteum@stream.o3qip.mongodb.net/?retryWrites=true&w=majority&appName=stream";
// Create a MongoClient with a MongoClientOptions object
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
// DB Config with Mongoose
mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true
}
})
.then(async () => {
try {
// Connect the client to the server
await client.connect();
// Send a ping to confirm a successful connection
await client.db("admin").command({ ping: 1 });
console.log("MongoDB Connected Successfully!");
} catch (err) {
console.error("Error connecting to MongoDB:", err);
}
})
.catch(err => console.error('MongoDB connection error:', err));
// EJS
app.use(expressLayouts);
app.set('view engine', 'ejs');
app.set('layout', 'layouts/main');
// Express body parser
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Express session with MongoDB store
app.use(session({
secret: process.env.SESSION_SECRET || 'KquWbFL6DYB7sw4FxuVXAyZnwbfArT0YpoxF1yNGKZg',
resave: false,
saveUninitialized: false,
rolling: true,
store: MongoStore.create({
mongoUrl: uri,
collectionName: 'sessions',
ttl: 24 * 60 * 60, // 1 day
autoRemove: 'native',
touchAfter: 24 * 3600, // time period in seconds
crypto: {
secret: process.env.SESSION_SECRET || 'KquWbFL6DYB7sw4FxuVXAyZnwbfArT0YpoxF1yNGKZg'
}
}),
cookie: {
secure: false, // Set to false for development
httpOnly: true,
maxAge: 24 * 60 * 60 * 1000, // 1 day
sameSite: 'lax'
},
name: 'streamvista.sid'
}));
// Initialize Passport and restore authentication state from session
app.use(passport.initialize());
app.use(passport.session());
// Connect flash
app.use(flash());
// Global variables middleware
app.use((req, res, next) => {
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.warning_msg = req.flash('warning_msg');
res.locals.error = req.flash('error');
res.locals.user = req.user || null;
res.locals.isAuthenticated = req.isAuthenticated();
res.locals.title = 'StreamVista';
res.locals.path = req.originalUrl || '/';
next();
});
// Create necessary directories
const directories = [
path.join(__dirname, 'public', 'uploads', 'avatars'),
path.join(__dirname, 'public', 'uploads', 'videos'),
path.join(__dirname, 'public', 'uploads', 'thumbnails'),
path.join(__dirname, 'uploads', 'temp')
];
directories.forEach(dir => {
if (!fs.existsSync(dir)) {
try {
fs.mkdirSync(dir, { recursive: true });
// Set proper permissions
fs.chmodSync(dir, 0o755);
} catch (err) {
console.error(`Error creating directory ${dir}:`, err);
}
}
});
// Static files
app.use(express.static(path.join(__dirname, 'public')));
// Routes
app.use('/', require('./routes/index'));
app.use('/users', require('./routes/users'));
app.use('/videos', require('./routes/videos'));
app.use('/playlists', require('./routes/playlists'));
app.use('/dashboard', require('./routes/dashboard'));
const profileRoutes = require('./routes/profile');
app.use('/profile', profileRoutes);
// Handle both /feed and /videos/feed
app.get('/feed', (req, res) => {
if (!req.isAuthenticated()) {
req.flash('error_msg', 'Please log in to view this resource');
return res.redirect('/users/login');
}
res.redirect('/videos/feed');
});
// Error handling middleware
app.use((req, res, next) => {
res.status(404).render('errors/404', {
user: req.user,
title: '404 Not Found'
});
});
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).render('errors/500', {
user: req.user,
title: '500 Server Error'
});
});
// Handle deprecation warnings
process.on('warning', (warning) => {
if (warning.name === 'DeprecationWarning') {
if (warning.code === 'DEP0044') {
// Suppress this specific warning
return;
}
}
});
// Cleanup on app termination
process.on('SIGINT', async () => {
try {
await client.close();
console.log('MongoDB connection closed.');
process.exit(0);
} catch (err) {
console.error('Error during cleanup:', err);
process.exit(1);
}
});
// Override util.isArray
const util = require('util');
util.isArray = Array.isArray;
const PORT = process.env.PORT || 3000;
app.listen(PORT, console.log(`Server running on port ${PORT}`));