-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
147 lines (112 loc) · 3.86 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
const express = require('express');
const path = require('path');
const serveIndex = require('serve-index')
const app = express();
let index = require('./routes/index');
let autofill = require('./routes/run');
let changelog = require('./routes/changelog');
// set up for socket io
const server = require('http').Server(app)
const io = require('socket.io')(server)
// set up for WS event emitter
const events = require('events');
const eventEmitter = new events.EventEmitter();
// load environment variables
require('dotenv').config();
// View Engine
// app.set('views', path.join(__dirname, 'client'));
app.set('views', '/');
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
// Set static Folder
// MW
app.use(express.json());
app.use(express.urlencoded({ extended: true}))
server.listen(process.env.PORT, function () {
console.log("Listening on *:" + process.env.PORT);
});
// routes
app.use(express.static(path.join(__dirname, 'client')));
app.get('/healthcheck', (req, res) => res.send("OK"));
app.use('/', index);
app.use('/run', autofill);
app.use('/changelog', changelog);
app.use('/history', serveIndex(path.join(__dirname, 'newman')));
app.use('/history', express.static(path.join(__dirname, 'newman')));
app.use('/static', express.static(path.join(__dirname, 'client')));
/*
Array to store the list of users along with there respective socket id.
*/
const users = [];
io.on('connection', function (socket) {
socket.emit("log_message", {data: "Connected WS. Welcome.", class: "grey-text"});
socket.emit("log_message", {data: "Just enter your user name and password to start. :)", class: "teal-text"});
socket.emit("log_message", {data: "Medical leaves are coming soon... Stay tuned!"});
socket.on('user', function (userName) {
users.push({
id: socket.id,
user: userName
});
eventEmitter.emit('logging', userName + " joined the party.", "blue-text");
});
socket.on('disconnect', function () {
for (let i = 0; i < users.length; i++) {
if (users[i].id === socket.id) {
send(users[i].user + " left.")
users.splice(i, 1);
}
}
});
});
eventEmitter.on('logging', function (action, message, clz, user) {
if (user) {
let socketId;
for (let i = 0; i < users.length; i++) {
if (users[i].user === user) {
socketId = users[i].id
}
}
io.to(socketId).emit(action, {
data: message,
class: clz
});
} else {
io.emit(action, {
data: message,
class: clz
});
}
});
// Exception handling
app.all('*', function (req, res) {
console.log("[TRACE] Server 404 request: " + req.originalUrl);
res.redirect("/")
});
originConsoleLog = console.log;
originConsoleErr = console.error;
if (process.env.NODE_ENV === 'development') {
// Override console.log
console.log = function (data) {
eventEmitter.emit('logging', "DEV: " + data, "grey-text text-lighten-1");
originConsoleLog(data);
};
console.error = function (data) {
eventEmitter.emit('logging', data);
originConsoleErr(data);
};
}
send = function (data, userName, clazz) {
eventEmitter.emit('logging', 'log_message', data, clazz, userName);
if (process.env.NODE_ENV === 'development') originConsoleLog(data);
};
sendErr = function (data, userName, clazz) {
eventEmitter.emit('logging', 'log_message', data, "red-text " + clazz, userName);
originConsoleErr(data);
};
sendHTML = function (data, userName) {
eventEmitter.emit('logging', 'preview', data, null, userName);
};
toast = function (data, userName, clazz) {
eventEmitter.emit('logging', 'toast', data, clazz, userName);
if (process.env.NODE_ENV === 'development') originConsoleLog("TOAST: " + data);
};