This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
200 lines (178 loc) · 6.2 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
195
196
197
198
199
200
//load dependencies
const express = require('express'); //main express shiz
const path = require('path'); //for filesystem
const favicon = require('serve-favicon'); //serves favicon
const bodyParser = require('body-parser'); //parses http request information
const session = require('express-session'); //session middleware (uses cookies)
const passport = require('passport'); //for user sessions
const Client = require('node-rest-client').Client;//for reading from REST APIs (e.g., TheBlueAlliance)
const useragent = require('express-useragent'); //for info on connected users
const colors = require('colors'); //for pretty debugging
const monk = require("monk"); //Monk for connecting to db
const useFunctions = require('./useFunctions'); //Functions inside separate module for app.use
const fs = require("fs");
//check if we have a db user file
var hasDBUserFile = fs.existsSync(".dbuser");
var db;
if(hasDBUserFile){
var dbUser = JSON.parse(fs.readFileSync(".dbuser", {"encoding": "utf8"}));
console.log(dbUser);
console.log(`${dbUser.username}:${dbUser.password}@localhost:27017/app`);
db = monk(`${dbUser.username}:${dbUser.password}@localhost:27017/app`);
}
else{
db = monk("localhost:27017/app"); //Local db on localhost without authentication
}
var client = new Client(); //Creates node-rest-client.
//isDev is typically used as a locals var in view engine.
//debug is used for logging.
//production is used to cache pug views.
var isDev = false, debug = false, production = false;
/* Check process arguments.
If -dev or --dev, isDev = true.
If -debug or --debug, debug = true.
If -d or --d, both = true.
*/
for(var i in process.argv){
switch(process.argv[i]){
case "-dev":
case "--dev":
console.log("Dev");
isDev = true;
break;
case "-d":
case "--d":
console.log("Dev");
isDev = true;
case "-debug":
case "--debug":
console.log("Debug");
debug = true;
break;
case "-production":
case "--production":
production = true;
}
}
//PUG CACHING (if dev is NOT enabled or production IS enabled)
if(!isDev || production){
console.log("Production");
process.env.NODE_ENV = "production";
}
//Create app.
var app = express();
//set app's bools to these arguments
app.isDev = isDev;
app.debug = debug;
//Boilerplate setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(bodyParser.json({limit: '50kb'}));
app.use(bodyParser.urlencoded({ limit: '50kb', extended: true, parameterLimit: 5000}));
app.use(express.static(path.join(__dirname, 'public')));
var mongoose = require('mongoose');
//check if we have a db user file
var hasDBUserFile = fs.existsSync(".dbuser");
var db;
//Connect to mongoose for session storage
if(hasDBUserFile){
//if there is a .dbuser file, connect with the user inside that file
var dbUser = JSON.parse(fs.readFileSync(".dbuser", {"encoding": "utf8"}));
//Usage: mongodb://[USER]:[PASSWORD]@[HOST]/[DB]?authSource=[AUTH DB]?w=1
console.log(`mongodb://${dbUser.username}:${dbUser.password}@localhost/sessions?authSource=app&w=1`);
mongoose.connect(`mongodb://${dbUser.username}:${dbUser.password}@localhost/sessions?authSource=app&w=1`, {});
}
else{
//if there is no .dbuser, then connect without username and password.
mongoose.connect('mongodb://localhost/sessions', {useNewUrlParser: true}); //Local connection w/o authentication
}
//Session
/*
//As it is written here, there is a critical bug.
//When one user signs in on one device, EVERY user on EVERY device becomes signed in for that user.
//Must be fixed.
app.use(session({
secret: 'sfl44-dfjl-436gg-dsfdf',
resave: true,
saveUninitialized: true,
cookie: { maxAge: 2628000000 },
store: new (require('express-sessions'))({
storage: 'mongodb',
instance: mongoose, // optional
host: 'localhost', // optional
port: 27017, // optional
db: 'sessions', // optional
collection: 'sessions', // optional
expire: 86400 // optional
})
}))
*/
//Session
app.use(session({
secret: 'marcus night',
resave: true,
saveUninitialized: true
}));
//User agent for logging
app.use(useragent.express());
//Passport setup (user authentication)
require('./passport-config');
app.use(passport.initialize());
app.use(passport.session());
app.use(function(req, res, next){
//For logging
req.requestTime = Date.now();
//For database
req.db = db;
//For user login
req.passport = passport;
next();
});
//sets view engine vars for user
app.use(useFunctions.userViewVars);
//Event stuff
app.use(useFunctions.getEventInfo);
//Logging and timestamping
app.use(useFunctions.logger);
//adds logging to res.render function
app.use(useFunctions.renderLogger);
//adds TBA API key to req
app.use(useFunctions.setupNodeRestClient);
//USER ROUTES
var index = require('./routes/index');
var login = require('./routes/login');
var dashboard = require("./routes/dashboard");
var scouting = require("./routes/scouting");
var reports = require('./routes/reports');
var allianceselection = require('./routes/allianceselection');
var image = require("./routes/image");
//ADMIN ROUTES
var adminindex = require('./routes/admin/adminindex');
var scoutingaudit = require("./routes/admin/audit");
var current = require("./routes/admin/current");
var externaldata = require("./routes/admin/externaldata");
var scoutingpairs = require('./routes/admin/scoutingpairs');
var teammembers = require("./routes/admin/teammembers");
var manualinput = require("./routes/admin/manualinput");
//CONNECT URLS TO ROUTES
app.use('/', index);
app.use('/login', login);
app.use('/scouting', scouting);
app.use("/dashboard", dashboard);
app.use('/reports', reports);
app.use('/allianceselection', allianceselection);
app.use('/admin', adminindex);
app.use('/admin/scoutingpairs', scoutingpairs);
app.use("/admin/teammembers", teammembers);
app.use('/admin/data', externaldata);
app.use('/admin/current', current);
app.use('/admin/audit', scoutingaudit);
app.use('/admin/manualinput', manualinput);
app.use('/image', image);
// catch 404 and forward to error handler
app.use(useFunctions.notFoundHandler);
// error handler
app.use(useFunctions.errorHandler);
console.log("app.js:".red + " " +"Ready!".bgGreen)
module.exports = app;