forked from hyperstudio/MIT-Annotation-Data-Store
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.js
390 lines (364 loc) · 10.2 KB
/
web.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Setup
var application_root = __dirname,
secret = process.env.SECRET,
port = process.env.PORT,
db = process.env.MONGOLAB_URI || process.env.DB,
consumer = process.env.CONSUMER,
version = process.env.VERSION,
path = require("path"),
mongoose = require('mongoose'),
lessMiddleware = require('less-middleware'),
jwt = require('jwt-simple'),
express = require("express"),
app = express();
// CORS
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Expose-Headers', 'Content-Length, Content-Type, Location');
res.header('Access-Control-Allow-Headers', 'Content-Length, Content-Type, X-Annotator-Auth-Token, X-Requested-With');
res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS');
res.header('Access-Control-Max-Age', '86400');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
} else {
next();
}
};
// Schemas
var Schema = mongoose.Schema;
// Annotation Ranges
var Ranges = new Schema({
start: {
type: String,
required: true
},
end: {
type: String,
required: true
},
startOffset: {
type: Number,
required: false
},
endOffset: {
type: Number,
required: false
}
});
var Shape = new Schema({
type: {
type: String,
required: true
},
geometry: {
x: {
type: Number,
required: true
},
y: {
type: Number,
required: true
},
width: {
type: Number,
required: true
},
height: {
type: Number,
required: true
}
}
});
// Annotation Model
var Annotation = new Schema({
id: {
type: String,
required: false
},
annotator_schema_version: {
type: String,
required: false,
default: version
},
created: {
type: Date,
default: Date.now()
},
updated: {
type: Date,
default: Date.now()
},
user: {
type: String,
required: false
},
username: {
type: String,
required: false
},
text: {
type: String,
required: false
},
quote: {
type: String,
required: false
},
uri: {
type: String,
required: false
},
src: {
type: String,
required: false
},
shapes: [Shape],
uuid: {
type: String,
required: false
},
groups: [String],
subgroups: [String],
ranges: [Ranges],
tags: [String],
permissions: {
read: [String],
admin: [String],
update: [String],
delete: [String]
},
legacy: {
type: Boolean,
required: false,
default: false
}
});
var AnnotationModel = mongoose.model('Annotation', Annotation);
// DB
mongoose.connect(db, function(err) {
if (err)
console.error(err);
else
console.log('Database connection established');
});
// config
app.configure(function() {
app.use(allowCrossDomain);
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(lessMiddleware({
src: __dirname + '/public',
compress: true
}));
app.use(express.static(path.join(application_root, "public")));
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});
Annotation.pre('save', function(next) {
this.id = this._id;
next();
});
// ROUTES
app.get('/api', function(req, res) {
res.send('Annotations API is running');
});
// Search annotations
app.get('/api/search', function(req, res) {
var query;
var re = new RegExp(req.query.host, 'i');
switch (req.query.context) {
case 'document':
query = AnnotationModel.find({
'uri': req.query.uri
});
break;
case 'dashboard':
query = AnnotationModel.find();
query.where('uri').regex(re);
break;
case 'search': // only limit to current host, allow searching on any user, document, etc.
query = AnnotationModel.find();
query.where('uri').regex(re);
break;
}
switch (req.query.mode) {
case 'user':
query.where('user').equals(req.query.user);
break;
case 'group':
query.where('subgroups'). in (req.query.subgroups);
query.$where('this.permissions.read.length < 1');
break;
case 'class':
query.where('groups'). in (req.query.groups);
query.$where('this.permissions.read.length < 1');
break;
case 'admin':
break;
}
query.limit(req.query.limit);
if (req.query.sidebar || req.query.context == "dashboard" || req.query.context == "search") {
query.exec(function(err, annotations) {
if (!err) {
if (annotations.length > 0) {
return res.send(annotations);
} else {
return res.send(204, 'Successfully deleted annotation.');
}
} else {
return console.log(err);
}
});
} else {
query.exec(function(err, annotations) {
if (!err) {
// console.info(annotations);
if (annotations.length > 0) {
return res.send({
'rows': annotations
});
} else {
return res.send(204, 'Successfully deleted annotation.');
}
} else {
return console.log(err);
}
});
}
});
// List annotations
app.get('/api/annotations', tokenOK, function(req, res) {
return AnnotationModel.find(function(err, annotations) {
if (!err) {
return res.send(annotations);
} else {
return console.log(err);
}
});
});
// Single annotation
app.get('/api/annotations/:id', tokenOK, function(req, res) {
return AnnotationModel.findById(req.params.id, function(err, annotation) {
if (!err) {
return res.send(annotation);
} else {
return console.log(err);
}
});
});
// POST to CREATE
app.post('/api/annotations', tokenOK, function(req, res) {
var annotation;
console.log("POST: ");
console.log(req.body);
annotation = new AnnotationModel({
user: req.body.user,
username: req.body.username,
consumer: "annotationstudio.mit.edu",
annotator_schema_version: req.body.annotator_schema_version,
created: Date.now(),
updated: Date.now(),
text: req.body.text,
uri: req.body.uri,
src: req.body.src,
quote: req.body.quote,
tags: req.body.tags,
groups: req.body.groups,
subgroups: req.body.subgroups,
uuid: req.body.uuid,
ranges: req.body.ranges,
shapes: req.body.shapes,
permissions: req.body.permissions,
legacy: req.body.legacy
});
annotation.save(function(err) {
if (!err) {
return console.log("Created annotation with uuid: " + req.body.uuid);
} else {
return console.log(err);
}
});
annotation.id = annotation._id;
return res.send(annotation);
});
// PUT to UPDATE
// Single update
app.put('/api/annotations/:id', tokenOK, function(req, res) {
return AnnotationModel.findById(req.params.id, function(err, annotation) {
annotation._id = req.body._id;
annotation.id = req.body._id;
annotation.user = req.body.user;
annotation.username = req.body.username;
annotation.consumer = req.body.consumer;
annotation.annotator_schema_version = req.body.annotator_schema_version;
annotation.created = req.body.created;
annotation.updated = Date.now();
annotation.text = req.body.text;
annotation.uri = req.body.uri;
annotation.url = req.body.url;
annotation.shapes = req.body.shapes;
annotation.quote = req.body.quote;
annotation.tags = req.body.tags;
annotation.groups = req.body.groups;
annotation.subgroups = req.body.subgroups;
annotation.uuid = req.body.uuid;
annotation.ranges = req.body.ranges;
annotation.permissions = req.body.permissions;
annotation.legacy = req.body.legacy;
return annotation.save(function(err) {
if (!err) {
console.log("updated");
} else {
console.log(err);
}
return res.send(annotation);
});
});
});
// Remove an annotation
app.delete('/api/annotations/:id', tokenOK, function(req, res) {
return AnnotationModel.findById(req.params.id, function(err, annotation) {
return annotation.remove(function(err) {
if (!err) {
console.log("removed");
return res.send(204, 'Successfully deleted annotation.');
} else {
console.log(err);
}
});
});
});
// Authentication
function tokenOK(req, res, next) {
try {
var decoded = jwt.decode(req.header('x-annotator-auth-token'), secret);
if (inWindow(decoded)) {
console.log("Token in time window");
} else {
console.log("Token not in in time window.");
}
next();
} catch (err) {
console.log("Error decoding token:");
console.log(err);
return res.send("There was a problem with your authentication token");
}
};
function inWindow(decoded, next) {
var issuedAt = decoded.issuedAt;
var ttl = decoded.ttl;
var issuedSeconds = new Date(issuedAt) / 1000;
var nowSeconds = new Date().getTime() / 1000;
var diff = ((nowSeconds - issuedSeconds));
var result = (ttl - diff);
console.log("Time left on token: about " + Math.floor(result / (60 * 60)) + " hours.");
return ((result > 0) ? true : false);
}
// launch server
app.listen(port, function() {
console.log("Listening on " + port);
});