-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.js
98 lines (81 loc) · 2.54 KB
/
server.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
const express = require('express');
const app = express();
const bodyparser = require('body-parser');
const session = require('express-session');
const path = require('path');
const ejs = require('ejs');
const cookieParser = require('cookie-parser');
const i18n = require('i18n');
const passport = require('passport');
const { Strategy } = require('passport-discord');
/**
* Load function is responsible for initializing and configuring the express app, setting up middleware,
* defining routes, and starting the web server.
*
* @param {Client} client - the client object used for interacting with external services
*/
module.exports.load = async (client) => {
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: true }));
app.use(express.json());
app.engine('html', ejs.renderFile);
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, '/views'));
app.use(express.static(path.join(__dirname, '/public')));
app.use(session({
secret: process.env.CLIENT_SECRET,
resave: false,
saveUninitialized: false,
}));
// Configure cookie parser
app.use(cookieParser());
// Configure i18n
i18n.configure({
locales: ['en', 'ro'],
directory: __dirname + '/i18n',
defaultLocale: 'en',
cookie: 'locale',
autoReload: false,
updateFiles: false,
objectNotation: true,
});
// Initialize passport and restore authentication state, if any, from the session.
app.use(async function(req, res, next) {
req.client = client;
next();
});
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((obj, done) => {
done(null, obj);
});
passport.use(new Strategy({
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: process.env.CALLBACK_URL,
scope: [ 'identify', 'guilds' ],
}, function(accessToken, refreshToken, profile, done) {
process.nextTick(function() {
return done(null, profile);
});
}));
// Initialize i18n middleware and set the default locale to 'en' if it's not set in the session or in the request headers or cookies or in the environment
app.use(i18n.init);
// Define routes here
app.use('/', require('./routes/index'));
app.use('/dashboard', require('./routes/dashboard'));
// 404 handler
app.get('*', (req, res) => {
res.render('../views/404', {
bot: req.client,
user: req.user,
});
});
// Start the web server
app.listen(process.env.PORT, () => {
console.log(`[Dashboard]: Web server now online on port:${process.env.PORT} 📶.`);
});
};