-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiscoveryData.js
More file actions
184 lines (151 loc) · 5.15 KB
/
DiscoveryData.js
File metadata and controls
184 lines (151 loc) · 5.15 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
'use strict';
// S4S Discovery Data Services
// File: DiscoveryData.js
const version = '20200815';
// Required modules
const restify = require('restify');
const Logger = require('bunyan');
const moment = require('moment');
const util = require('./utility');
// Configuration
const config = require('./config');
// Create a file logger enabling the standard serializers
const logInst = Logger.createLogger({
name: 'DiscoveryData',
serializers: Logger.stdSerializers,
streams: [
{
type: 'file',
path: config.logFile,
level: config.logLevel
}]
});
// Setup restify
const server = restify.createServer({name: 'S4S Discovery Data Server', version: '1.3.0', log: logInst});
server.use(restify.plugins.bodyParser());
// CORS support
server.pre(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', '*');
next();
});
// Correctly handle HTTP OPTIONS (for CORS)
server.on('MethodNotAllowed', function (req, res) {
if (req.method == 'OPTIONS') {
// Add header to satisfy CORS preflight check
res.header('Access-Control-Allow-Headers', 'content-type, x-requested-with');
res.send('');
} else {
logInst.info({req: req}, 'Method Not Allowed: ' + req.method + ' ' + req.path);
res.header('content-type', 'text/plain');
res.send(405, 'Method Not Allowed\n');
}
});
// Log 'Not Found' errors
server.on('NotFound', function (req, res) {
logInst.info({req: req}, 'Not Found');
res.header('content-type', 'text/plain');
res.send(404, 'Not Found\n');
});
// Log completion
process.on('SIGINT', function () {
var msg = 'Stopped: ' + moment().format('YYYYMMDD-HH:mm:ss');
logInst.info(msg);
console.log('\n'+msg);
process.exit();
})
// Keep track of which service modules are ready
var isReady = {};
util.setNotReady(isReady, 'providers');
util.setNotReady(isReady, 'participants');
util.setNotReady(isReady, 'reference');
util.setNotReady(isReady, 'data');
// ---------- Document the available routes --------------------
server.get('/', documentRoutes);
// ---------- Configure the 'providers' service --------------------
var providers = require('./providers');
providers.on('ready', function () {
// Check whether all services are ready
if (util.setReady(isReady, 'providers')) {
// Yes -- start listening for requests
listen();
}
});
// Allowed 'providers' methods and routes
server.get('/providers', providers.providers);
server.get('/providers/:id', providers.providersForParticipant);
// ---------- Configure the 'participants' service --------------------
var participants = require('./participants');
participants.on('ready', function () {
// Check whether all services are ready
if (util.setReady(isReady, 'participants')) {
// Yes -- start listening for requests
listen();
}
});
// Allowed 'participants' methods and routes
server.get('/participants', participants.participants);
server.get('/participants/:id', participants.participantData);
server.post('/participants/:id/:provider/:resourceId', participants.participantAnnotation);
// ---------- Configure the 'reference' service --------------------
var reference = require ('./reference');
reference.on('ready', function () {
// Check whether all services are ready
if (util.setReady(isReady, 'reference')) {
// Yes -- start listening for requests
listen();
}
});
// Allowed 'reference' methods and routes
server.get('/reference/:provider/:referencePath', reference.reference);
// ---------- Configure the 'data' service --------------------
var data = require ('./data');
data.on('ready', function () {
// Check whether all services are ready
if (util.setReady(isReady, 'data')) {
// Yes -- start listening for requests
listen();
}
});
// Allowed 'data' methods and routes
server.get('/data/manifest', data.manifest);
server.put('/data/upload/:id', binaryParser, data.upload);
server.get('/data/download/:id', data.download);
//---------------------------------------------------------------------------------
// SUPPORT FUNCTIONS
// Binary data parser
function binaryParser (req, res, next) {
if (req.contentType() !== 'application/octet-stream' ) {
next();
} else {
let buffer = [];
req.on('error', next);
req.on('data', function onRequestData(chunk) {
buffer.push(chunk);
});
req.once('end', function() {
req.body = Buffer.concat(buffer).toString('binary');
next();
});
}
}
// Document routes
function documentRoutes (req, res, next) {
if (req == undefined) {
// Return documentation
return {pre: {desc:server.name + ' -- All Valid Routes', version:version},
desc: 'Show this page (all valid ' + config.deploy + ' routes).'};
} else {
res.writeHead(200);
res.end(util.documentRestifyRoutes(server));
return next();
}
}
// Start listening...
function listen () {
server.listen(config.listenPort, function() {
var msg = 'Started: {0} {1} Listening at {2} ({3})'.format(moment().format('YYYYMMDD-HH:mm:ss'), server.name, server.url, config.deploy);
logInst.info(msg);
console.log(msg);
});
}