-
Notifications
You must be signed in to change notification settings - Fork 2
/
daemon.js
171 lines (154 loc) · 4.4 KB
/
daemon.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
var fs = require('fs');
var express = require('express');
var cors = require('cors');
var bodyParser = require('body-parser');
var superagent = require('superagent');
var UUID = require('uuid');
var _ = require('lodash');
var config = require('./config');
var Promise = require('es6-promises');
var daemon = express();
daemon.use(bodyParser.json({ type: 'application/ld+json' }));
daemon.use(cors({ origin: true }));
daemon.options('*', cors());
// returns a promise which resolves with JSON object from file
function readFile(name){
return new Promise(function(resolve, reject){
fs.readFile(config.dataPath + name, function(err, buffer){
if(err) reject(err);
resolve(JSON.parse(buffer.toString()));
});
});
}
var storage = {
getAll: function(){
return new Promise(function(resolve, reject){
fs.readdir(config.dataPath, function(err, files){
if(err) reject(err);
//remove .gitkeep & .DS_Store
_.remove(files, function(name){ return name === ".gitkeep"; });
_.remove(files, function(name){ return name === ".DS_Store"; });
Promise.all(files.map(readFile)).then(function(listings) {
var result = {
"@context": config.context,
"@graph": listings
};
resolve(result);
}).catch(reject);
});
});
},
save: function(uuid, content){
return new Promise(function(resolve, reject){
var path = config.dataPath + '/' + uuid;
fs.writeFile(path, JSON.stringify(content), function(err, data){
if(err) reject(err);
resolve(content);
});
});
},
delete: function(uuid){
return new Promise(function(resolve, reject){
var path = config.dataPath + '/' + uuid;
fs.unlink(path, function(err){
if(err) reject(err);
resolve();
});
});
},
};
function fetchProfile(uri){
return new Promise(function(resolve, reject){
superagent.get(uri)
.accept('application/ld+json')
.buffer()
.end(function(provRes){
if(provRes.ok){
// FIXME handle parsing errors
resolve(JSON.parse(provRes.text));
} else {
reject();
}
});
});
}
function checkIfNew(uri){
console.log(uri);
return new Promise(function(resolve, reject){
storage.getAll().then(function(all){
var existing = _.find(all['@graph'], function(listing){ return listing.about['@id'] == uri; });
console.log(existing);
if(existing) {
reject();
} else {
resolve();
}
});
});
}
// listProfile: Receives the URI (probably from a plp-editor) and adds it to the list
daemon.post('/', function(req, res){
// Get the URI where the profile is stored from requesr
var profileUri = req.body['@id'];
checkIfNew(profileUri).then(function(){
var uuid = UUID.v4();
var uri = 'http://' + config.domain + '/' + uuid;
var path = 'data/'+ uuid;
fetchProfile(profileUri)
.then(function(profile){
// delete context FIXME
delete profile["@context"];
var listing = {
"@id": uri,
"@type": "Listing",
about: profile
};
storage.save(uuid, listing)
.then(function(data){
var min = {
"@context": "http://plp.hackers4peace.net/context.jsonld",
"@id": data["@id"],
"@type": "Listing"
};
res.type('application/ld+json');
res.send(min);
})
.catch(function(err){
// TODO add error reporting
res.send(500);
});
}).catch(function(){
console.log('failed fetching', profileUri);
// TODO add error reporting
res.send(500);
});
}, function(){
res.send(409);
});
});
// getProfiles: Returns the profiles listed on this plp-directory
daemon.get('/', function(req, res){
storage.getAll()
.then(function(data){
res.type('application/ld+json');
res.send(data);
})
.catch(function(err){
// TODO add error reporting
res.send(500);
});
});
daemon.delete('/:uuid', function(req, res){
// TODO add authentication
storage.delete(req.params.uuid)
.then(function(){
res.send(200);
})
.catch(function(err){
// TODO add error reporting
res.send(500);
});
});
daemon.listen(config.listenOn, function(err){
console.log('listening on:', config.listenOn);
});