-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
app.js
143 lines (116 loc) · 3.96 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
// * Code Owner: Diwas Atreya (Atreya#2401)
// * Github: https://github.com/diwasatreya
// * Contact on Twitter: https://twitter.com/DiwasAtreya
const express = require('express');
const session = require('express-session');
const morgan = require('morgan');
const { readdir } = require('fs');
const jwt = require('jsonwebtoken');
const dotenv = require("dotenv");
const mongoose = require("mongoose");
mongoose.connect(
process.env.DB,
{ useUnifiedTopology: true, useNewUrlParser: true },
() => console.log("Connected to Database")
);
class Website {
constructor() {
this.app = express();
this.config = require('./config.json');
this.config.callbackURL = this.config.callbackURL = this.config.bot.production
? `${this.config.bot.url}/auth/login`
: `${this.config.bot.url}:${this.config.app.port}/auth/login`;
this.bot = require('./bot/index');
this.bot = this.bot(this.config.bot.token);
try {
this._setup();
this._loadRoutes();
this._start();
} catch (e) {
throw typeof e === 'object' ? e : new Error(e);
}
}
_setup() {
this.app.set('views', 'views');
this.app.set('view engine', 'ejs');
this.app.use(express.static('assets'));
this.app.set('port', this.config.app.port || 3000);
this.app.use(morgan('dev'));
this.app.use(session({
secret: `ey.${Date.now()}${this.config.bot.id}${Date.now()}.dashboard.io`,
resave: false,
saveUninitialized: false
}));
this.app.use(express.json());
this.app.use(express.urlencoded({ extended: false }));
this.app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Credentials', true);
req.config = this.config;
req.bot = this.bot;
req.user = req.session.user;
next();
});
this.app.get('/discord', async (req, res) => {
res.redirect(`https://discord.gg/whJeF4mDAX`)
});
this.app.get('/dc', async (req, res) => {
res.redirect(`https://discord.gg/whJeF4mDAX`)
});
this.app.get('/youtube', async (req, res) => {
res.redirect(`https://www.youtube.com/c/Kp18Gamer`)
});
this.app.get('/yt', async (req, res) => {
res.redirect(`https://www.youtube.com/c/Kp18Gamer`)
});
this.app.get('/github', async (req, res) => {
res.redirect(`https://github.com/diwasatreya`)
});
this.app.get('/git', async (req, res) => {
res.redirect(`https://github.com/diwasatreya`)
});
this.app.get('/facebook', async (req, res) => {
res.redirect(`https://www.facebook.com/profile.php?id=100075574807496`)
});
this.app.get('/fb', async (req, res) => {
res.redirect(`https://www.facebook.com/profile.php?id=100075574807496`)
});
this.app.get('/instagram', async (req, res) => {
res.redirect(`https://www.instagram.com/atreyadiwas/`)
});
this.app.get('/ig', async (req, res) => {
res.redirect(`https://www.instagram.com/atreyadiwas/`)
});
this.app.get('/twitter', async (req, res) => {
res.redirect(`https://twitter.com/DiwasAtreya`)
});
this.app.get('/reddit', async (req, res) => {
res.redirect(`https://www.reddit.com/r/programmingwithmemes`)
});
}
_loadRoutes() {
readdir('./routes', (err, files) => {
if (err) return new Error(err);
const routes = files.filter((c) => c.split('.').pop() === 'js');
if (files.length === 0 || routes.length === 0) throw new Error('Application online on port');
for (let i = 0; i < routes.length; i++) {
let route = require(`./routes/${routes[i]}`);
this.app.use(route.name, new route.Router());
}
});
}
_start() {
try {
this.app.listen(this.app.get('port'));
console.log(`Application online on port ::${this.app.get('port')}::`);
} catch (e) {
throw new Error(e);
}
}
}
module.exports = new Website();
// * Code Owner: Diwas Atreya (Atreya#2401)
// * Github: https://github.com/diwasatreya
// * Contact on Twitter: https://twitter.com/DiwasAtreya