forked from jondo89/Heavy-lifting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
406 lines (351 loc) · 16.6 KB
/
index.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
var express = require('express');
var path = require('path');
var logger = require('morgan');
var compression = require('compression');
var methodOverride = require('method-override');
var session = require('express-session');
var flash = require('express-flash');
var bodyParser = require('body-parser');
var expressValidator = require('express-validator');
var dotenv = require('dotenv');
var exphbs = require('express-handlebars');
var mongoose = require('mongoose');
var passport = require('passport');
var recaptcha = require('express-recaptcha');
// Load environment variables from .env file
dotenv.load();
// Controllers
var initController = require('./controllers/initialize');
var HomeController = require('./controllers/home');
var userController = require('./controllers/user');
var contractController = require('./controllers/contract');
var contactController = require('./controllers/contact');
var adminController = require('./controllers/admin');
var userInterfaceController = require('./controllers/userinterface');
var createController = require('./controllers/create');
var readController = require('./controllers/read');
var deleteController = require('./controllers/delete');
var pagesController = require('./controllers/pages');
var productController = require('./controllers/product');
var assemblyController = require('./controllers/assembly');
var componentController = require('./controllers/component');
var organizationController = require('./controllers/organization');
var heavyliftingController = require('./controllers/heavy-lifting');
// Passport OAuth strategies
require('./config/passport');
var app = express();
///////////////////////////////////////
/////// FAVICON LOCATION ////////
/////////////////////////////////////
var favicon = require('serve-favicon');
try {
app.use(favicon(__dirname + '/public/img/favicon/favicon-16x16.png'));
} catch (err){
console.log('Favicon not found in the required directory.')
}
////////////////////////////////////////////////////
/////// HEROKU VS LOCALHOST .ENV SWAP ////////
//////////////////////////////////////////////////
if (process.env.MONGODB_URI) {
mongoose.connect(process.env.MONGODB_URI);
} else {
mongoose.connect(process.env.MONGODB);
}
mongoose.connection.on('error', function() {
console.log('MongoDB Connection Error. Please make sure that MongoDB is running.');
process.exit(1);
});
var db = mongoose.connection;
db.once('open', function() {
// we're connected!
console.log('mongoose connection ok')
//compile the schema for mongoose
});
var hbs = exphbs.create({
defaultLayout: 'main',
helpers: {
ifeq: function(a, b, options) {
if (a === b) {
return options.fn(this);
}
return options.inverse(this);
},
toJSON : function(object) {
return JSON.stringify(object);
},
partial: function (name) {
return name;
},
'dotdotdot' : function(str) {
if (str) {
if (str.length > 16)
return str.substring(0,16) + '...';
return str;}
},
'dotdotdotdot' : function(str) {
if (str) {
if (str.length > 200)
return str.substring(0,200) + '...';
return str;
}
},
'dotdotdotdotdot' : function(str) {
if (str) {
if (str.length > 400)
return str.substring(0,400) + '...';
return str;
}
}
}
});
/////////////////////////////////////////////
/////// HTTPS TRAFFIC REDIRECT ////////
///////////////////////////////////////////
// Redirect all HTTP traffic to HTTPS
function ensureSecure(req, res, next){
if(req.headers["x-forwarded-proto"] === "https"){
// OK, continue
return next();
};
res.redirect('https://'+req.hostname+req.url);
};
// Handle environments
if (app.get('env') == 'production') {
app.all('*', ensureSecure);
}
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
/////////////////////////////////////////////
/////// LOCALHOST PORT SETTING ////////
///////////////////////////////////////////
app.set('port', process.env.PORT || 5000);
app.use(compression());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(expressValidator());
app.use(methodOverride('_method'));
app.use(session({ secret: process.env.SESSION_SECRET, resave: true, saveUninitialized: true }));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(function(req, res, next) {
res.locals.user = req.user;
next();
});
app.use(express.static(path.join(__dirname, 'public')));
///////////////////////////////////////////////
//// SET YOUR APP.JSON DETAILS ////
/////////////////////////////////////////////
var myModule = require('./app.json');
var sitename = myModule.sitename
var website = myModule.website
var repo = myModule.repo
app.locals.sitename = sitename
app.locals.website = website
app.locals.repo = repo
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// ROUTING ////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////
//// PAGES ////
///////////////////////////
app.get('/store', pagesController.store);
app.get('/database', pagesController.database);
app.get('/help', pagesController.help);
app.get('/forms', pagesController.forms);
app.get('/assemblies', pagesController.assemblies);
app.get('/configuration', pagesController.configuration);
app.get('/reports', pagesController.reports);
////////////////////////////////////////////
//// INITIALIZE DATABASE ////
//////////////////////////////////////////
app.get('/init', initController.deletedb);
app.get('/deletedb', initController.deletedb);
app.get('/getdb', initController.getdb);
//////////////////////////////////////////////////////////////////////
//// PRIMARY ADMINISTRATIVE DATABASE MODIFICATION ////
////////////////////////////////////////////////////////////////////
app.get('/hl-admin', adminController.hadmin);
app.get('/admin', adminController.admin);
app.get('/read', adminController.read);
app.get('/update', adminController.update);
app.get('/delete', adminController.delete);
/////////////////////////////////
//// DATABASE ////
///////////////////////////////
//Load Template
app.get('/templateload', readController.templateload);
/////////////////////////////////////////
//// CREATE CONTROLLERS ////
///////////////////////////////////////
app.post('/create', createController.create);
///////////////////////////////////////////
//// DELETE CONTROLLERS ////
/////////////////////////////////////////
//get data by array of ids.
app.get('/deleteentryperm', deleteController.deleteentryperm);
//get data by array of ids permanently.
app.get('/deleteentry', deleteController.deleteentry);
/////////////////////////////////////////
//// READ CONTROLLERS ////
///////////////////////////////////////
//admin page table view.
app.get('/getCollectionData', readController.getCollectionData);
//get data by array of ids.
app.get('/getdata', readController.getdata);
//get data by array of ids.
app.get('/getdatacomp', readController.getdatacomp);
//get data by parentid
app.get('/parentid', readController.parentid);
//get data
app.get('/getshortdata', readController.getshortdata);
//get jstree
app.get('/jstree', readController.jstree);
//get the select ddrop down items
app.get('/getformfield', readController.getformfield);
//get the select templatename
app.get('/templatename', readController.templatename);
//get the select groups
app.get('/groups', readController.groups);
//get the navmenu
app.get('/navmenuload', readController.navmenuload);
//get the usermenu
app.get('/loadusermenu', readController.loadusermenu);
//get the loadcompmenu
app.get('/loadcompmenu', readController.loadcompmenu);
//get the single element id
app.get('/singleidcall', readController.singleidcall);
//get the single element id
app.get('/findme', readController.findme);
//get the get assembly all element id
app.get('/getassemblyall', readController.getassemblyall);
//get the get assembly all element id
app.get('/defaultassy', readController.defaultassy);
// getformraw
app.get('/getformraw', readController.getformraw);
// getformraw
app.get('/getcompform', readController.getcompform);
/////////////////////////////////////
//// PRODUCTS ////
///////////////////////////////////
// getformraw
app.get('/productload', productController.productload);
/////////////////////////////////////
//// ASSEMBLY ////
///////////////////////////////////
app.get('/assembly/new', assemblyController.newassy);
//search for the form to load.
app.get('/getform', componentController.additionaldetails , readController.getform);
//Rebuild routing
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////
//// TEMPALTES ////
///////////////////////////////
app.get('/privacy', userInterfaceController.privacy);
app.get('/terms', userInterfaceController.terms);
///////////////////////////////////////////////////
//// USER INTERFACE CONTROLLER ////
/////////////////////////////////////////////////
app.get('/users/', userInterfaceController.users);
app.get('/users/:username/',componentController.componentforms, componentController.usercomponents,organizationController.userorganizations,componentController.organizationcomponents,heavyliftingController.heavyliftingalluser ,userInterfaceController.profile);
app.get('/users/:username/settings/',userInterfaceController.settings);
app.get('/users/:username/settings/:page', componentController.componentforms,componentController.usercomponents,organizationController.userorganizations,componentController.organizationcomponents , userInterfaceController.page);
app.get('/usersearch', userInterfaceController.usersearch);
/////////////////////////////////////
//// ORGANIZATION ////
///////////////////////////////////
//Static
app.get('/organizations', organizationController.orglist);
app.get('/organizations/new', organizationController.neworg);
app.post('/organizations/new', organizationController.createorgstatic);
app.get('/organizations/:orgname/', organizationController.ajaxorguserread ,organizationController.orgprofile);
app.get('/organizations/:orgname/settings',organizationController.ajaxorguserread , organizationController.organizationpermission, organizationController.settings);
app.get('/organizations/:orgname/people', organizationController.ajaxorguserread ,organizationController.people);
app.get('/organizations/:orgname/settings',organizationController.ajaxorguserread , organizationController.settings);
app.get('/organizations/:orgname/settings/:page', organizationController.ajaxorguserread , organizationController.page);
app.put('/organizations/:orgname', userController.ensureAuthenticated, organizationController.orgPut);
app.get('/leaveorganiztion/:ids', organizationController.leaveorganiztion);
//Ajax
app.get('/orguserread', organizationController.orguserread); // Get the active user organizations , owner and member.
app.get('/organizations/:orgname/components', organizationController.ajaxorguserread ,organizationController.components);
app.get('/organizations/:orgname/assemblies',organizationController.ajaxorguserread , organizationController.assemblies);
/////////////////////////////////////
//// CONTRACTS //// Need work here
///////////////////////////////////
//Static
app.get('/contracts', contractController.contlist);
app.get('/contracts/new', contractController.newcont);
app.post('/contracts/new', contractController.createcontstatic);
// Most likely next needed Contract functions //
// This is what routes the display of an individual page for a contract, need to make this entire thing work. Refer to line#205 for how organizations do it and trace the logic
app.get('/contracts/:conttitle/', contractController.ajaxcontuserread, contractController.contowneruserdetail)
//app.get('/organizations/:contname/', contractController.ajaxorguserread , contractController.organizationpermission, contractController.orgowneruserdetail, contractController.orgprofile);
// Breaks application: app.put('/contracts/:conttitle', userController.ensureAuthenticated, organizationController.organizationpermission, contractController.contPut);
///////////////////////////////////
//// COMPONENTS ////
/////////////////////////////////
app.get('/components/', componentController.components);
app.get('/componentssuperadmin/', componentController.componentssuperadmin);
app.get('/component/new', organizationController.ajaxorguserread , componentController.componentforms, componentController.newcomp);
//User Components
app.get('/components/users/', componentController.usersview);
app.get('/components/users/:username/', componentController.users);
app.get('/components/users/:username/:compid', componentController.compiduser);
//Organization Components
app.get('/components/organizations/', componentController.organizationsview);
app.get('/components/organizations/:orgname', componentController.organizations);
app.get('/components/organizations/:orgname/:compid', componentController.compidorg);
//Viewer and calculator
app.get('/components/:compid', componentController.compmore);
//Viewer and calculator (Group) - Stage 2
app.get('/components/:template/:compgroupid',readController.query,readController.query1,readController.query2,readController.query3,readController.query4 , componentController.compmore);
/////////////////////////////////////
//// EMAILING ////
///////////////////////////////////
//Testing of the smtp mail , work great.
app.get('/testmail', userInterfaceController.testmail);
/////////////////////////////////
//// HOME ////
///////////////////////////////
app.get('/',componentController.componentforms, componentController.usercomponents,organizationController.userorganizations,componentController.organizationcomponents ,heavyliftingController.heavyliftingalluser, HomeController.index);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// USER ////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
app.get('/contact', contactController.contactGet);
app.post('/contact', contactController.contactPost);
app.get('/account', userController.ensureAuthenticated, userController.accountGet);
app.put('/account', userController.ensureAuthenticated, userController.accountPut);
app.delete('/account', userController.ensureAuthenticated, userController.accountDelete);
app.get('/signup', userController.signupGet);
app.post('/signup', userController.signupPost);
app.get('/signin', userController.loginGet);
app.post('/signin', userController.loginPost);
app.get('/forgot', userController.forgotGet);
app.post('/forgot', userController.forgotPost);
app.get('/reset/:token', userController.resetGet);
app.post('/reset/:token', userController.resetPost);
app.get('/signout', userController.signout);
app.get('/unlink/:provider', userController.ensureAuthenticated, userController.unlink);
app.get('/auth/google', passport.authenticate('google', { scope: 'profile email' }));
app.get('/auth/google/callback', passport.authenticate('google', { successRedirect: '/', failureRedirect: '/signin' }));
app.get('/auth/github', passport.authenticate('github', { scope: [ 'user:email profile repo' ] }));
app.get('/auth/github/callback', passport.authenticate('github', { successRedirect: '/', failureRedirect: '/signin' }));
/////////////////////////////
//// 404 ////
///////////////////////////
app.get('*', function(req, res){
res.render('404',{layout:false});
});
// Production error handler
if (app.get('env') === 'production') {
app.use(function(err, req, res, next) {
console.error(err.stack);
res.sendStatus(err.status || 500);
});
}
app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
module.exports = app;