-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
190 lines (171 loc) · 5.14 KB
/
app.js
File metadata and controls
190 lines (171 loc) · 5.14 KB
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
/**
* Module dependencies.
*/
var express = require('express'),
path = require('path'),
app = express(),
browserify = require('browserify-middleware'),
mongoskin = require('mongoskin'),
SseCommunication = require('sse-communication/Simple'),
ReferenceServer = require('syncit-server/ReferenceServer'),
getConfiguration = function(key) {
"use strict";
if (!process.env.hasOwnProperty(key)) {
throw new Error("Could not find configuration key '" + key + "'");
}
return process.env[key];
},
nonPersistentDatabases = ['MEMORY'],
syncItServerPersist = (function() {
"use strict";
var ServerPersistMongodb = require('syncit-server/ServerPersist/Mongodb'),
ServerPersistMemoryAsync = require('syncit-server/ServerPersist/MemoryAsync');
if (getConfiguration('DATABASE__TYPE') == 'MEMORY') {
return new ServerPersistMemoryAsync();
}
if (getConfiguration('DATABASE__TYPE') == 'MONGODB') {
var mongoskinConnection = mongoskin.db(
'mongodb://' +
getConfiguration('DATABASE__MONGODB_HOST') + ':' +
getConfiguration('DATABASE__MONGODB_PORT') + '/' +
getConfiguration('DATABASE__MONGODB_DATABASE'),
{w:true}
);
return new ServerPersistMongodb(
function(v) { return JSON.parse(JSON.stringify(v)); },
mongoskinConnection,
mongoskin.ObjectID,
getConfiguration('SYNCIT__COLLECTION'),
function() {}
);
}
throw new Error('Only "memory" and "mongodb" supported for data storage right now');
}()),
http = require('http'),
sseCommunication = new SseCommunication(),
setDeviceIdMiddleware = function(req, res, next) {
"use strict";
req.deviceId = req.params.deviceId;
next(null);
},
referenceServer = new ReferenceServer(
function(req) {
"use strict";
return req.deviceId;
},
syncItServerPersist,
sseCommunication
);
// all environments
(function(mode) {
"use strict";
/* global __dirname: false */
app.set('port', getConfiguration('HTTP__PORT'));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
if ( mode !== 'production') {
app.get('/js/App.js', browserify('./public/js/App.js'));
}
app.use(express.static(path.join(__dirname, 'public')));
}(app.get('env')));
(function() {
"use strict";
/* global console: false */
if ('development' == app.get('env')) {
console.log("DEVELOPMENT MODE");
app.use(express.errorHandler());
}
}());
var statusCodesObj = (function(data) {
"use strict";
var oData = {};
for (var i=0, l=data.length; i<l; i++) {
oData[
data[i].description.toLowerCase().replace(/[^a-z]/,'_')
] = data[i].status;
}
return oData;
}(require('./res/http_status_codes.js')));
var getStatusCode = function(status) {
"use strict";
if (!statusCodesObj.hasOwnProperty(status)) {
throw new Error("Could not find status code for status '" + status + "'");
}
return statusCodesObj[status];
};
var getQueueitemSequence = function(req, res, next) {
"use strict";
referenceServer.getQueueitems(
req,
function(err, status, data) {
if (err) { return next(err); }
res.json(getStatusCode(status), data);
}
);
};
app.get('/syncit/sequence/:s/:seqId', getQueueitemSequence);
app.get('/syncit/sequence/:s', getQueueitemSequence);
app.get('/syncit/change/:s/:k/:v', function(req, res, next) {
"use strict";
referenceServer.getDatasetDatakeyVersion(
req,
function(err, status, data) {
if (err) { return next(err); }
res.json(getStatusCode(status), data);
}
);
});
app.get('/', function(req, res) {
"use strict";
res.render('front', {
production: (app.get('env') === 'production' ? true : false),
persistData: (nonPersistentDatabases.indexOf(getConfiguration('DATABASE__TYPE')) == -1)
});
});
app.post('/syncit/:deviceId', setDeviceIdMiddleware, function(req, res, next) {
"use strict";
referenceServer.push(req, function(err, status, data) {
if (err) { return next(err); }
res.json(getStatusCode(status), data);
});
});
app.get(
'/sync/:deviceId',
setDeviceIdMiddleware,
referenceServer.sync.bind(referenceServer),
function(req, res, next) {
"use strict";
referenceServer.getMultiQueueitems(req, function(err, status, data) {
if (err) { return next(err); }
if (status !== 'ok') {
return res.write(SseCommunication.formatMessage(
'status-information', status
));
}
res.write(SseCommunication.formatMessage('download', data));
});
}
);
referenceServer.listenForFed(function(seqId, dataset, datakey, queueitem, newValue) {
"use strict";
var message =
"> The data stored at `" + dataset + "." + datakey + "` has changed to:\n>\n" +
"> " + JSON.stringify(newValue) + "\n>\n" +
"> This change occured because the following queueitem was applied:\n>\n" +
"> " + JSON.stringify(queueitem) + "\n>\n";
/* global console: false */
console.log(message);
});
var serverHttp = null;
serverHttp = http.createServer(app);
serverHttp.listen(getConfiguration('HTTP__PORT'), function(){
"use strict";
/* global console: false */
console.log('Express HTTP server listening on port ' + getConfiguration('HTTP__PORT'));
});