Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
188 lines (149 loc) · 4.78 KB
/
Copy pathindex.js
File metadata and controls
188 lines (149 loc) · 4.78 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
const express = require("express")
const { engine } = require('express-handlebars');
const { allowInsecurePrototypeAccess } = require('@handlebars/allow-prototype-access');
const Handlebars = require('handlebars');
const app = express()
const path = require('path');
const session = require('express-session');
const moment = require("moment")
const bodyparser = require('body-parser')
const { createServer } = require("http");
const { Server } = require("socket.io");
const DBConnection = require('./config/DBConnection');
//Routes
const main = require('./routes/main')
const transactions = require('./routes/transactions')
const listing = require('./routes/listing')
const api = require('./routes/api')
const inbox = require('./routes/inbox')
const user = require('./routes/user')
app.use('/webhook', express.raw({type: "*/*"}));
app.use(express.json());
app.use(bodyparser.urlencoded({ extended: false }))
// To send forms and shit
// socket implementation
const httpServer = createServer(app);
const io = new Server(httpServer, { cors: { origin: "*" } });
const messagingHandler = require("./sockets/messaging")
const onConnection = (socket) => {
socket.onAny((eventName, ...args) => {
console.log(eventName, "was just fired", args)
});
socket.userid = socket.handshake.auth.id
socket.join(`User ${socket.userid}`)
// console.log(socket.rooms)
messagingHandler(io, socket)
console.log(`${socket.id} has connected`);
}
io.on("connection", onConnection);
app.set('io', io)
// Library to use MySQL to store session objects
const MySQLStore = require('express-mysql-session');
var options = {
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USER,
password: process.env.DB_PWD,
database: process.env.DB_NAME,
clearExpired: true, // The maximum age of a valid session; milliseconds:
expiration: 3600000, // 1 hour = 60x60x1000 milliseconds
// How frequently expired sessions will be cleared; milliseconds:
checkExpirationInterval: 1800000 // 30 min
}
// To sstore: new MySQLStore(options),tore session information. By default it is stored as a cookie on browser
app.use(session({
key: 'vidjot_session',
secret: 'tojdiv',
store: new MySQLStore(options),
resave: false,
saveUninitialized: false,
}));
//database
DBConnection.setUpDB(false)
// passport
const passport = require('passport');
const passportConfig = require('./config/passportConfig');
passportConfig.localStrategy(passport);
// Initialize passport middleware
app.use(passport.initialize());
app.use(passport.session());
// Initilize Passport middleware
//Message Library
const flash = require('connect-flash');
app.use(flash());
const flashMessenger = require('flash-messenger');
const ensureAuthenticated = require("./helpers/auth");
app.use(flashMessenger.middleware);
// Place to define global variables
app.use(async function (req, res, next) {
res.locals.messages = req.flash('message');
res.locals.errors = req.flash('error');
res.locals.user = req.user;
res.locals.authenticated = req.isAuthenticated();
next();
});
app.engine(
"handlebars",
engine({
handlebars: allowInsecurePrototypeAccess(Handlebars),
defaultLayout: "main",
helpers: {
equals(arg1, arg2, options) {
return arg1 == arg2 ? options.fn(this) : options.inverse(this);
},
setVar(name, value, options) {
options.data.root[name] = value;
},
gt(a, b) {
var next = arguments[arguments.length - 1];
return (a > b) ? next.fn(this) : next.inverse(this);
},
ge(a, b) {
var next = arguments[arguments.length - 1];
return (a >= b) ? next.fn(this) : next.inverse(this);
},
identifygender(v1, v2) {
if (v1 == null) {
return true;
}
else if (String(v1) == v2) {
return true;
}
return false;
},
formatDate(date, targetFormat) {
return moment(date).format(targetFormat);
},
checkOwner(userId, OwnerId){
if(userId == OwnerId){
return true;
}return false;
},
add(v1, v2) {
return parseInt(v1) + parseInt(v2);
},
},
})
);
app.set('view engine', 'handlebars');
//Sets up engine and the default layout for handlebars
app.use(express.static(path.join(__dirname, 'public')));
// Creates static folder for publicly accessible HTML, CSS and Javascript files
app.all("/*", (req, res, next) => {
req.app.locals.layout = 'main';
next()
});
//Set layout for all routes
app.use("/", main)
app.use("/", transactions)
app.use("/", listing)
app.use("/inbox", ensureAuthenticated, inbox)
app.use("/api", api)
app.use("/user", user)
app.use('/build/', express.static(path.join(__dirname, 'node_modules/three/build')));
app.use('/jsm/', express.static(path.join(__dirname, 'node_modules/three/examples/jsm')));
const port = 5000;
// Starts the server and listen to port
httpServer.listen(port, () => {
console.log(`Server started on port ${port}`);
});