This repository has been archived by the owner on Feb 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
95 lines (83 loc) · 2.21 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
var express = require('express'),
app = express(),
server = require('http').Server(app),
io = require('socket.io')(server),
mongoose = require('mongoose'),
port = 8088;
const NAME_DB = process.env.MONGODB_NAME || "eventlogs",
USERNAME_DB = process.env.MONGODB_USERNAME || "admin",
PASS_DB = process.env.MONGODB_PWD || "admin",
HOST_DB = process.env.MONGODB_HOST || 'localhost',
PORT_DB = process.env.MONGODB_PORT || 27017;
var operationEventSchema = new mongoose.Schema({
docID: {
type: String
},
siteID: {
type: String
},
operationID: {
type: String
},
event: {
type: String,
enum: ['sent', 'received', 'integrated']
},
timestamp: {
type: Date
}
});
var OperationEventModel = mongoose.model('op_events', operationEventSchema);
var documentEventSchema = new mongoose.Schema({
docID: {
type: String
},
siteID: {
type: String
},
event: {
type: String,
enum: ['joined']
},
timestamp: {
type: Date
}
});
var DocumentEventModel = mongoose.model('doc_events', documentEventSchema);
// Connection to the mongoDB running instance
mongoose.connect('mongodb://' + HOST_DB + ':' + PORT_DB + '/' + NAME_DB);
// Check if connection succeed
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback() {
console.log('Connection to mongoDB instance succeed!');
});
app.use(express.static('static'));
app.get('/', function(req, res) {
res.sendFile(__dirname + '/test-log.html');
});
io.on('connection', function(socket) {
socket.on('events', function(events) {
console.log("received some events to store in the db");
var jsonEvents = JSON.parse(events);
jsonEvents.forEach(function(jsonEvent) {
var event;
if (jsonEvent.event === 'joined') {
event = new DocumentEventModel(jsonEvent);
} else {
event = new OperationEventModel(jsonEvent);
}
event.save(function(err) {
if (err) {
console.log(err);
}
});
});
});
socket.on('time', function(data) {
socket.emit('time', Date.now());
});
});
server.listen(port, function() {
console.log('Time log server started at localhost:', port);
});