-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
134 lines (105 loc) · 3.42 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
const express = require('express');
const app = express();
// const helmet = require('helmet');
const http = require('http').Server(app);
const io = require('socket.io')(http);
const fs = require('./model/utils/FileSystemUtils');
require('./model/utils/Date.prototype.toMysqlTimeStamp');
const swig = require('swig');
const MessageValidator = require('./model/MessageValidator');
// app.use(helmet()); // Error loading CDN...
io.on('connection', function (socket) {
socket.on('container message', function (msg) {
io.emit('container message', msg);
});
});
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.set('view cache', false);
app.use(function (req, res, next) {
var contentType = req.headers['content-type'] || '';
var mime = contentType.split(';')[0];
if (mime !== 'text/plain') {
return next();
}
var data = '';
req.setEncoding('utf8');
req.on('data', function (chunk) {
data += chunk;
});
req.on('end', function () {
req.rawBody = data;
next();
});
});
app.get('/', async (req, res) => {
const dirPath = './app/messages';
const fileContents = [];
/**
* Read all json files in the directory, filter out those needed to process.
* Using Promise.all to time when all async readFiles has completed
*/
fs.readdirAsync(dirPath).then(function (filenames) {
filenames = filenames.filter(function isValidMsgFile(filename) {
return (filename.split('.')[1] === 'json');
});
return Promise.all(filenames.map(function (filename) {
return fs.readFileAsync(dirPath + '/' + filename, 'utf8');
}));
}).then(function (files) {
files.forEach(function(file) {
var jsonFile = JSON.parse(file);
fileContents.push(jsonFile);
});
res.render('index', {
fileContents: fileContents,
});
}).catch(err => {
console.log('Error reading files messages', err);
res.render('index', {
error: err,
fileContents: fileContents,
});
});
});
app.post('/api/messsage', function (req, res) {
var rawBody = req.rawBody;
try {
var MsgValidator = new MessageValidator();
MsgValidator.checkMessageIsNotEmpty(rawBody);
MsgValidator.decodeMessage(rawBody);
MsgValidator.validateUserId();
MsgValidator.validateCurrencyFrom();
MsgValidator.validateCurrencyTo();
MsgValidator.validateAmountSell();
MsgValidator.validateAmountBuy();
MsgValidator.validateRate();
MsgValidator.validateTimePlaced();
MsgValidator.validateOriginatingCountry();
/* save files on file system */
var outputFilename = './app/messages/' + new Date().toMysqlTimeStamp() + '_msg.json';
fs.writeFile(outputFilename, JSON.stringify(MsgValidator.decodedMessage, null, 4), function (err) {
if (err) {
throw new Error('Error writing file on file system: ' + err);
}
// Trigger event for socket and frontend
io.emit('container message', MsgValidator.decodedMessage);
});
res.statusCode = 200;
res.type('application/json');
return res.send('{"message": "The message has been sent"}');
} catch (e) {
res.statusCode = 400;
res.type('application/json');
return res.send('{"message": "' + e.message + '"}');
}
});
app.engine('html', swig.renderFile);
swig.setDefaults({
cache: false
});
app.locals.appTitle = 'Market trade processor';
app.locals.tableTitle = 'Messages';
http.listen(3000, function () {
console.log('listening on *:3000');
});