This repository has been archived by the owner on Jun 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
334 lines (248 loc) · 9.01 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
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
const express = require("express");
const app = express();
const package = require('./package.json');
const chalk = require("chalk")
const fetch = require("node-fetch")
let eSession = require('easy-session');
let cookieParser = require('cookie-parser');
const expressUpload = require("express-fileupload")
let creds = require("./creds.json");
const cors = require("cors")
const favicon = require('express-favicon');
const session = require("express-session");
const mysql = require('mysql');
require("dotenv").config()
// Variables
let port = creds.ENV === "dev" ? 3001 : creds.ProductionPort;
let connection;
let db = {
host: "localhost",
user: creds.user,
database: creds.database,
password: creds.databasePassword,
multipleStatements: true,
timeout: 0
};
// Auth
const { loginAuth } = require("./auth/loginAuth");
const authRouter = require("./routes/authentication/auth");
// 911 & Tow Route
const callRouter = require("./routes/global/911-tow")
// In game
const getCitizenRouter = require("./routes/global/get-citizen-ingame");
// Global Router
const globalRouter = require("./routes/global/global")
// Admin
const adminRouter = require("./routes/admin")
// Member Management
const memberManagementRouter = require("./routes/admin/memberManagement")
const citizenManagementRouter = require("./routes/admin/citizenManagement")
// Vehicles Router
const adminVehicleRouter = require("./routes/values/vehicles")
// Genders Router
const adminGendersRouter = require("./routes/values/genders")
// Weapons Router
const adminWeaponsRouter = require("./routes/values/weapons")
// Ethnicities Router
const ethnicityRouter = require("./routes/values/ethnicities");
// Legal Status Router
const adminLegalStatusesRouter = require("./routes/values/legal");
// Edit CAD Router
const editCadRouter = require("./routes/admin/editCADRouter");
// Officers
const officersRouter = require("./routes/officers/officers");
// EMS/FD
const emsFdRouter = require("./routes/ems-fd/ems-fd")
// Citizens Router
// Licenses
const licenseRouter = require("./routes/citizens/licenses");
// Vehicle Router
const citizenVehicleRouter = require("./routes/citizens/vehicles");
// Weapon Router
const citizenWeaponRouter = require("./routes/citizens/weapons");
// Medical Record Router
const medicalRecordRouter = require("./routes/citizens/medicalRecord");
// Company Router
const companyRouter = require("./routes/citizens/company");
// Remaining Citizens Routes, will update these asap
const citizenRouter = require("./routes/citizens/citizen")
// Edit Account Router
const editAccountRouter = require("./routes/admin/editAccountRouter");
// Admin Departments
const departmentsRouter = require("./routes/values/departments");
// Dispatch Router
const dispatchRouter = require("./routes/dispatch");
// Index Router
const indexRouter = require("./routes/index");
// Tow Router
const towRouter = require("./routes/tow/tow");
const bleeterRouter = require("./routes/bleeter/bleeter");
// Middleware
app.use(express.static(__dirname + '/public'));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cookieParser());
app.set('views', __dirname + '/views');
app.set("view engine", "ejs")
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true,
}));
app.use(cors());
app.use(favicon(__dirname + '/public/icons/icon.png'));
app.use(eSession.main(session));
app.use(expressUpload());
// Main Pages
app.use("/", indexRouter)
// Login & Registration
app.use("/auth", authRouter);
// 911 & Tow call
app.use("/call", callRouter)
app.use("/global", getCitizenRouter)
// Keep this line here, otherwise the above pages will not work!
app.use(loginAuth);
// Edit Account Router
app.use("/account", editAccountRouter);
// Member Management
app.use("/admin/members", memberManagementRouter)
app.use("/admin/citizens/", citizenManagementRouter);
// Edit CAD Router
app.use("/admin/edit-cad", editCadRouter)
// Admin
app.use("/admin", adminRouter);
// Citizens vehicle Stuff
app.use("/c/vehicle", citizenVehicleRouter);
// Citizens Weapon Stuff
app.use("/c/weapons", citizenWeaponRouter)
// Citizens Router
app.use("/citizen", citizenRouter)
// Company Router
app.use("/company", companyRouter);
// Bleeter Router
app.use("/bleeter", bleeterRouter);
// Medical Records Router
app.use("/medical", medicalRecordRouter);
// Licenses Router
app.use("/licenses", licenseRouter);
// Tow Router
app.use("/tow", towRouter)
// Dispatch
app.use("/dispatch", dispatchRouter);
// Officers
app.use("/officers", officersRouter);
// Use for bolos, 911 calls, Tow call, update dispatch&Police stuff..
app.use("/global", globalRouter);
// EMS/FD
app.use("/ems-fd", emsFdRouter)
// Admin Values
// Admin Values Vehicles
app.use("/admin/vehicles", adminVehicleRouter);
// Admin Values Genders
app.use("/admin/genders", adminGendersRouter)
// Admin Values Ethnicities
app.use("/admin/ethnicities", ethnicityRouter);
// Admin Values Weapons
app.use("/admin/weapons", adminWeaponsRouter)
// Departments
app.use("/admin/departments", departmentsRouter)
// Legal
app.use("/admin/legal", adminLegalStatusesRouter)
// 404
app.get('/*', (req, res) => {
res.status(404).render("errors/404.ejs", {
title: "404 | SnailyCAD",
isAdmin: "",
desc: "",
});
});
app.post("/*", (req, res) => {
res.status(404).render("errors/404.ejs", {
title: "404 | SnailyCAD",
isAdmin: "",
desc: "",
});
});
async function main() {
console.log(chalk.blue("CHECKING CAD VERSION."));
function handleDisconnect() {
connection = mysql.createConnection(db); // Recreate the connection, since
// the old one cannot be reused.
global.connection = connection
connection.connect(function (err) { // The server is either down
connection.timeout = 0;
if (err) { // or restarting (takes a while sometimes).
console.log('error when connecting to db2:', err);
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
} // to avoid a hot loop, and to allow our node script to
}); // process asynchronous requests in the meantime.
connection.on('error', function (err) {
console.log('db error', err);
if (err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
} else { // connection idle timeout (the wait_timeout
throw err; // server variable configures this)
}
});
connection.on('error', function (err) {
console.log('db error', err);
if (err.code === 'ECONNRESET') { // Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
} else { // connection idle timeout (the wait_timeout
throw err; // server variable configures this)
}
});
};
handleDisconnect();
setInterval(function () {
connection.query("SELECT 1", (err) => {
if (err) {
console.log(err);
}
})
});
const versionUrl = "https://dev-caspertheghost.github.io/api/version.html";
const versionResult = await fetch(versionUrl).then(res => res.json());
// This SQL Is for update 3.4.4, Creates arrest reports table
connection.query(`ALTER TABLE \`users\` ADD \`dispatch_status\` VARCHAR(255) NOT NULL AFTER \`whitelist_status\`;`, (err) => {
if (err) {
if (err.code === "ER_DUP_FIELDNAME") {
return console.log("Database is up to date.");
}
console.log(err);
} else {
console.log("Updated the database successfully");
}
})
let upToDate ="You are all up to date."
// Version checker
if (`${package.version}` !== `${versionResult.latestVersion}`) {
upToDate = false
console.warn(chalk.red(`
-------------------------------\n
Your Version is out of date!
Your Version: ${package.version}
Updated Version: ${versionResult.latestVersion}
Please Pull the latest version on the GitHub page: https://github.com/Dev-CasperTheGhost/snaily-cad Or Run: git pull origin master\n
Warning: Your CAD will still run
-------------------------------\n`))
}
// Checks every 12 hours while running
setInterval(() => {
if (`${package.version}` !== `${versionResult.latestVersion}`) {
upToDate = "Your Version is out of date! Please Pull the latest version on the GitHub page: https://github.com/Dev-CasperTheGhost/snaily-cad Or Run: git pull origin master"
} else {
console.log(chalk.green("You are all up to date."));
}
}, 43200000)
// Show in console that CAD was started
app.listen(port, () => {
upToDate ?
console.log(`
-------------------------------\n
CAD is successfully running on Port: ${port} \n
${upToDate}\n
-------------------------------\n`) : null
});
}
main();