-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.js
More file actions
153 lines (117 loc) · 4.54 KB
/
web.js
File metadata and controls
153 lines (117 loc) · 4.54 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
// web.js
var express = require("express");
var mongoskin = require('mongoskin');
var bodyParser = require('body-parser');
var logfmt = require("logfmt");
var cors = require('cors');
var app = express();
app.use(bodyParser());
app.use(cors()); //enable all CORS requests
var mongoUri = process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost:27017/exampleDb';
var db = mongoskin.db(mongoUri, {safe:true})
app.use(logfmt.requestLogger());
app.param('collectionName', function(req, res, next, collectionName){
req.collection = db.collection(collectionName);
return next();
})
app.get('/', function(req, res, next) {
res.send('<h3>HTTPA Provenance Tracker Gateway</h3><br> Connected Nodes:');
})
app.get('/:collectionName', function(req, res, next) {
req.collection.find({} ,{limit:10, sort: [['_id',-1]]}).toArray(function(e, results){
if (e) return next(e)
res.send(results)
})
})
app.post('/:collectionName', function(req, res, next) {
console.log("Received record for: " + req.body._id);
//Need to update each of the document records!
function updateDocuments(source){
console.log(source);
req.collection.findById(source, function(e, result){
if (e) { return next(e); }
req.collection.updateById(source,
{ $push:{"derivatives" : req.body._id}},
{safe:true, multi:true}, function(e, result){
if (e) return next(e)
console.log("source = "+ source);
console.log("derivative = "+ req.body._id);
console.log((result===1)?{msg:'added this as a derivative'}:{msg:'error'})
});
req.collection.updateById(source,
{ $push: {"activity" : { "name" : "share", "time": new Date(), "derivative": req.body._id, "details": req.body.meta}}},
{safe:true, multi:true}, function(e, result){
if (e) return next(e)
console.log((result===1)?{msg:'added this as a derivative'}:{msg:'error'})
});
});
}
//Check if the req.body has any source attributes. If so, find them and add this _id
//to the derivative field
if (req.body.sources.length > 0){
for (var i = 0; i<req.body.sources.length; i++){
updateDocuments(req.body.sources[i]);
}
}
req.collection.insert(req.body, {}, function(e, results){
if (e) {
res.send("Error occured. Possibly you attempted adding a duplicate log record.");
return next(e)
}
res.send(results)
})
})
app.get('/:collectionName/:id', function(req, res, next) {
req.collection.findById(req.params.id, function(e, result){
if (e) return next(e)
res.send(result)
})
})
app.put('/:collectionName/:id', function(req, res, next) {
//Having $set here, will replace the record with the new values
req.collection.updateById(req.params.id, {$push:req.body}, {safe:true, multi:false}, function(e, result){
if (e) return next(e)
res.send((result===1)?{msg:'success'}:{msg:'error'})
})
});
//Any generic activity, in the URL pattern specify what activity you support
app.put('/:collectionName/:activity/:id', function(req, res, next) {
req.collection.updateById(req.params.id, {$push: {"activity": {"name" : req.params.activity, "time" : new Date(), "details" : req.body}}}, {safe:true, multi:false}, function(e, result){
if (e) return next(e)
res.send((result===1)?{msg:'success'}:{msg:'error'})
})
})
//Audit URIS
app.get('/:collectionName/audit/:id', function(req, res, next){
req.collection.findById(req.params.id, function(e, result){
if (e) return next(e)
if (result != null && result.activity != undefined){
result.activity.sort(
function (a,b) {
if (a != null && b != null){
if (a.time > b.time)
return -1;
if (a.time < b.time)
return 1;
return 0;
}
});
res.send(result.activity);
}
else {
res.send("Audit log record for resource "+ req.params['id'] +" not found.");
}
})
});
app.del('/:collectionName/:id', function(req, res, next) {
req.collection.removeById(req.params.id, function(e, result){
if (e) return next(e)
res.send((result===1)?{msg:'success'}:{msg:'error'})
})
})
var port = Number(process.env.PORT || 5000);
app.listen(port, function() {
console.log("Listening on " + port);
});