forked from AMPATH/etl-rest-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
etl-server.js
104 lines (85 loc) · 2.54 KB
/
etl-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
var Hapi = require('hapi');
var mysql = require('mysql');
var Good = require('good');
var Basic = require('hapi-auth-basic');
var https = require('https');
var settings = require('./conf/settings.js');
var squel = require ('squel');
var corsHeaders = require('hapi-cors-headers');
var _ = require('underscore');
var tls = require('tls');
var fs = require('fs');
var routes = require('./etl-routes');
var elasticRoutes = require('./elastic/routes/care.treatment.routes');
var httpsServer = tls.createServer({
key: fs.readFileSync(settings.sslSettings.key),
cert: fs.readFileSync(settings.sslSettings.crt)
});
var server = new Hapi.Server(
{connections: {
//routes: {cors:{origin:["https://amrs.ampath.or.ke:8443"]}}
routes: {cors:{additionalHeaders:['JSNLog-RequestId']}}
}
});
server.connection({
port: 8002,
// host:'localhost',
tls: httpsServer
});
var pool = mysql.createPool(settings.mysqlPoolSettings);
var validate = function (username,password,callback) {
//Openmrs context
var options = {
hostname: 'test1.ampath.or.ke',
port:8443,
path:'/amrs/ws/rest/v1/session',
headers: {
'Authorization': "Basic " + new Buffer(username + ":" + password).toString("base64")
}
};
https.get(options,function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
var result = JSON.parse(body);
console.log(result);
callback(null,result.authenticated,{});
});
}).on('error', function(error) {
console.log(error);
callback(null,false);
});
};
server.register([
{register: Basic, options:{}},
{
register: Good,
options: {
reporters: [{
reporter: require('good-console'),
events: {
response: '*',
log: '*'
}
}]
}
}],
function (err) {
if (err) {
throw err; // something bad happened loading the plugin
}
server.auth.strategy('simple', 'basic', { validateFunc: validate });
//Adding routes
for (var route of routes) {
server.route(route);
}
for (var route of elasticRoutes) {
server.route(route);
}
server.ext('onPreResponse', corsHeaders);
server.start(function () {
server.log('info', 'Server running at: ' + server.info.uri);
});
});