-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
131 lines (107 loc) · 3.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
/*
* Copyright (C) 2017 mibbit., All Rights Reserved.
*/
'use strict';
/**
* Entry file for the servers
*
* @author AON
* @version 1.0.0
*/
/*---------- import libraries ----------*/
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const redis = require('./db');
const morgan = require('morgan');
const winston = require('winston');
const http = require('http');
const io = require('socket.io');
const helmet = require('helmet');
/*---------- import custom modules ----------*/
const config = require('./config/config');
const channelRoutes = require('./app/routes/channelRoutes');
// app instance created and passed to server instance which in turn is passed to socket instance
const app = express();
const server = http.Server(app);
const socket = io(server);
// import config and bind to the app instance
// configure server port
const port = config.PORT;
// Express Middleware for serving static
// app.use(helmet);
// files and parsing the request body
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
/*====================================
= logger setup =
====================================*/
var logger = new winston.Logger({
transports: [
new winston.transports.File({
level: 'info',
filename: './logs/all-logs.log',
handleExceptions: true,
json: true,
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: false
}),
new winston.transports.Console({
level: 'debug',
handleExceptions: true,
json: false,
colorize: true
})
],
exitOnError: false
});
logger.stream = {
write: function(message, encoding){
logger.info(message);
}
};
app.use(morgan("dev", { "stream": logger.stream }));
/*===== End of logger setup ======*/
// configure routes
app.use('/', channelRoutes);
app.use(function(req, res) {
res.send(404, 'not found :-(');
});
// Start the Server
server.listen(port, function() {
console.log('Server Started. Listening on *:' + port);
});
// store list of all channels
let channels = [];
// Store people in chatroom
let chatters = [];
// Store messages in chatroom
let chat_messages = [];
// Redis Client Ready
redis.once('ready', function() {
console.log("connected to redis server!")
// Flush Redis DB. Not to be used until migrations are required
// client.flushdb();
// Initialize chat rooms
redis.get('chat_rooms', function(err, reply) {
if(reply) {
chat_rooms = JSON.parse(reply);
}
});
// Initialize Chatters
redis.get('chat_users', function(err, reply) {
if (reply) {
chatters = JSON.parse(reply);
}
});
// Initialize Messages
redis.get('chat_app_messages', function(err, reply) {
if (reply) {
chat_messages = JSON.parse(reply);
}
});
});