-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
340 lines (286 loc) · 10.9 KB
/
server.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
const express = require("express");
const app = express();
const http = require("http");
http.globalAgent.maxSockets = 10;
const uuidv4 = require('uuid/v4');
const requests = {};
const responses = {};
const dataSubjects = {};
app.disable("x-powered-by");
app.use(express.json({type: "application/vnd.api+json"}));
app.use(express.urlencoded({ extended: true }));
// TODO: check if still needed
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
res.header("Access-Control-Allow-Headers", "Content-Type, Authorization, Content-Length, X-Requested-With, APP_KEY");
// intercepts OPTIONS method
if (req.method === "OPTIONS") {
// respond with 200
res.status(200).send()
}
next()
});
app.use(function (req, res, next) {
res.type("application/vnd.api+json");
next();
});
app.get("/data-portability-requests", (req, res) => {
let payload = {
"data": Object.values(requests).map((request) => { return request.toJsonApi() })
};
if(req.query.include && req.query.include.includes("data-portability-response")) {
payload["included"] = [].concat.apply(
Object.values(requests)
.filter((request) => {return request.dataPortabilityResponseId != null})
.map((request) => {
const response = responses[request.dataPortabilityResponseId];
return [response.toJsonApi(), ...response.getIncluded()];
}));
}
res.status(200).json(payload);
});
app.post("/data-portability-requests", (req, res) => {
const uuid = uuidv4();
requests[uuid] = new DataPortabilityRequest(uuid, new Date(), req.body.data.relationships["data-subject"].data.id, null);
let payload = {"data": requests[uuid].toJsonApi()};
try {
console.debug({topic: dataPortabilityRequestsTopic, payload: JSON.stringify(payload)}, "Producing on topic");
producer.produce(
dataPortabilityRequestsTopic, // Topic
null, // Partition, null uses default
Buffer.from(JSON.stringify(payload)), // Message
null,
Date.now()
)
} catch (error) {
log.error({topic: dataPortabilityRequestsTopic, err: error}, "An error occurred when trying to send message to Kafka topic")
}
res.status(201).json(payload);
});
app.get("/data-portability-requests/:id", (req, res) => {
let uuid = req.params.id;
let request = requests[uuid];
if(!request) return res.status(404).end();
let payload = {"data": request.toJsonApi()};
if(req.query.include && req.query.include.includes("data-portability-response")) {
if(request.dataPortabilityResponseId) {
const response = responses[request.dataPortabilityResponseId].toJsonApi();
payload["included"] = [response.toJsonApi(), ...response.getIncluded()];
}
}
res.status(200).json(payload);
});
app.delete("/data-portability-requests/:id", (req, res) => {
let uuid = req.params.id;
if(requests[uuid].dataPortabilityResponseId) {
delete responses[requests[uuid].dataPortabilityResponseId];
}
delete requests[uuid];
res.status(204).end();
});
app.get("/data-portability-responses", (req, res) => {
let payload = {
"data": Object.values(responses).map((response) => { return response.toJsonApi() }),
"included": [].concat.apply([],
Object.values(responses).map((response) => { return response.getIncluded() })
)
};
if(req.query.include && req.query.include.includes("data-portability-request")) {
payload["included"] = payload["included"].concat(Object.values(responses)
.filter((response) => {return response.dataPortabilityRequestId != null})
.map((response) => { return requests[response.dataPortabilityRequestId].toJsonApi()}));
}
res.status(200).json(payload);
});
app.post("/data-portability-responses", (req, res) => {
const uuid = uuidv4();
const request = requests[req.body.data.relationships["data-portability-request"].data.id];
if(!request) {
return res.status(404).end();
}
responses[uuid] = new DataPortabilityResponse(uuid, new Date(), req.body.data.attributes.payload, request.id);
request.dataPortabilityResponseId = uuid;
let payload = {"data": responses[uuid].toJsonApi()};
res.status(201).json(payload);
});
app.get("/data-portability-responses/:id", (req, res) => {
let uuid = req.params.id;
let response = responses[uuid];
if(!response) return res.status(404).end();
let payload = {"data": response.toJsonApi(), "included": response.getIncluded()};
if(req.query.include && req.query.include.includes("data-portability-request")) {
if(response.dataPortabilityRequestId) {
payload["included"] = payload["included"].concat([requests[response.dataPortabilityRequestId].toJsonApi()]);
}
}
res.status(200).json(payload);
});
app.delete("/data-portability-responses/:id", (req, res) => {
let uuid = req.params.id;
if(!responses[uuid]) {
return res.status(404).end();
}
for (let request of Object.values(requests)) {
if(request.dataPortabilityResponseId === uuid) {
request.dataPortabilityResponseId = null;
break;
}
}
delete responses[uuid];
res.status(204).end();
});
app.get("/data-subjects", (req, res) => {
let payload = {"data": Object.values(dataSubjects).map((dataSubject) => { return dataSubject.toJsonApi() })};
res.status(200).json(payload);
});
app.post("/data-subjects", (req, res) => {
const uuid = req.body.data.id || uuidv4();
dataSubjects[uuid] = new DataSubject(uuid, req.body.data.attributes["name"]);
let payload = {"data": dataSubjects[uuid].toJsonApi()};
res.status(201).json(payload);
});
app.get("/data-subjects/:id", (req, res) => {
let uuid = req.params.id;
let dataSubject = dataSubjects[uuid];
if(!dataSubject) return res.status(404).end();
let payload = {"data": dataSubject.toJsonApi()};
res.status(200).json(payload);
});
class DataPortabilityRequest {
constructor(id, timestamp, dataSubjectId, dataPortabilityResponseId) {
this.id = id;
this.timestamp = timestamp;
this.dataSubjectId = dataSubjectId;
this.dataPortabilityResponseId = dataPortabilityResponseId;
}
toJsonApi() {
let payload = {
"id": this.id,
"type": "data-portability-requests",
"attributes": {
"timestamp": this.timestamp
},
"relationships": {}
};
if(this.dataSubjectId) {
payload.relationships["data-subject"] = {
"data": {
"id": this.dataSubjectId,
"type": "data-subjects"
}
}
}
if(this.dataPortabilityResponseId) {
payload.relationships["data-portability-response"] = {
"data": {
"id": this.dataPortabilityResponseId,
"type": "data-portability-responses"
}
}
}
return payload;
}
}
class DataPortabilityResponse {
constructor(id, timestamp, payload, dataPortabilityRequestId) {
this.id = id;
this.timestamp = timestamp;
this.dataPortabilityRequestId = dataPortabilityRequestId;
this.convertedPayload = this.convertPayload(payload);
}
toJsonApi() {
return this.convertedPayload.data;
}
getIncluded() {
return this.convertedPayload.included || [];
}
convertPayload(payload) {
if(!payload["data"]) payload["data"] = {"attributes": {}, "relationships": {}, "links": {}};
payload["data"]["id"] = this.id;
payload["data"]["type"] = "data-portability-responses";
payload["data"]["attributes"]["timestamp"] = this.timestamp;
payload["data"]["links"]["self"] = "/data-portability-responses/" + this.id;
payload["data"]["relationships"]["data-portability-request"] = {
"data": {
"id": this.dataPortabilityRequestId,
"type": "data-portability-requests"
}
};
delete payload["data"]["attributes"]["is-initialized"];
delete payload["data"]["attributes"]["title"];
return payload;
}
}
class DataSubject {
constructor(id, name) {
this.id = id;
this.name = name;
}
toJsonApi() {
return {
"id": this.id,
"type": "data-subjects",
"attributes": {
"name": this.name
},
"relationships": {}
};
}
}
const Kafka = require("node-rdkafka");
const producer = new Kafka.Producer({
"metadata.broker.list": process.env["KAFKA_BROKER_LIST"] || "localhost:9092",
"api.version.request": false
});
const dataPortabilityRequestsTopic = process.env["DATA_PORTABILITY_REQUESTS_TOPIC"] || "data-portability-requests";
const MAX_RETRIES = 10;
let retryCount = 0;
let server;
function setup () {
const connectOptions = {"timeout": process.env["KAFKA_TIMEOUT"] || 5000};
producer.connect(connectOptions);
producer.on("connection.failure", function (error) {
if (retryCount >= MAX_RETRIES) {
console.error({err: error}, "Could not connect to Kafka, exiting");
process.exit(1);
}
retryCount++;
const timeout = (Math.pow(2, retryCount) + Math.random()) * 1000;
console.warn({err: error, timeout, retryCount}, `Failed to connect to kafka, retrying in ${timeout} ms`);
setTimeout(producer.connect.bind(producer), timeout, connectOptions)
});
producer.on("event.error", function (error) {
console.error({err: error}, "Error from kafka producer")
});
producer.on("delivery-report", function (error, report) {
if (error) {
console.error({err: error}, "Error in kafka delivery report")
} else {
console.info({report}, "Kafka delivery report")
}
});
producer.setPollInterval(100);
producer.on("ready", async () => {
console.debug("Kafka producer ready.");
server = app.listen(8080, () => {
const { address } = server.address();
const { port } = server.address();
console.debug("App listening at http://%s:%s", address, port);
});
})
}
setup();
// Handle SIGTERM gracefully
process.on("SIGTERM", gracefulShutdown);
process.on("SIGINT", gracefulShutdown);
process.on("SIGHUP", gracefulShutdown);
function gracefulShutdown () {
// Serve existing requests, but refuse new ones
console.warn("Received signal to terminate: wrapping up existing requests");
server.close(() => {
// Exit once all existing requests have been served
console.warn("Received signal to terminate: done serving existing requests. Exiting");
process.exit(0)
})
}