Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions conf/config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,13 @@
"port": "85",
"generalPath": "",
"cd4PanelPath": ""
},
"openhim": {
"host": "http://10.50.80.115",
"port": 5001,
"token": "your-openhim-token-here",
"basePath": "/",
"rejectUnauthorized": false,
"authType": "Custom"
}
}
117 changes: 117 additions & 0 deletions etl-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var winston = require('winston');
var path = require('path');
var _ = require('lodash');
var Joi = require('joi');
var request = require('request');
var eidLabData = require('./eid-data-synchronization/eid-lab-results');
var eidService = require('./service/eid.service');
var patientListCompare = require('./service/patient-list-compare.service.js');
Expand Down Expand Up @@ -6469,6 +6470,122 @@ module.exports = (function () {
params: {}
}
}
},
// OpenHIM Proxy Route
{
method: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'],
path: '/openhim/{path*}',
config: {
auth: 'simple',
handler: function (req, reply) {
var openhimConfig = config.openhim;

if (!openhimConfig || !openhimConfig.host || !openhimConfig.token) {
return reply(
Boom.badImplementation(
'OpenHIM configuration is missing or incomplete'
)
);
}

var targetPath = req.params.path || '';
var queryString = req.url.search || '';

// Construct the target URL
var targetUrl = openhimConfig.host;
if (
openhimConfig.port &&
openhimConfig.port !== 80 &&
openhimConfig.port !== 443
) {
targetUrl += ':' + openhimConfig.port;
}

// Handle base path and target path properly to avoid double slashes
var fullPath = '';
if (openhimConfig.basePath) {
fullPath = openhimConfig.basePath;
// Ensure basePath starts with /
if (!fullPath.startsWith('/')) {
fullPath = '/' + fullPath;
}
} else {
fullPath = '/';
}

// Add target path if it exists
if (targetPath) {
// Ensure no double slashes
if (fullPath.endsWith('/') && targetPath.startsWith('/')) {
fullPath += targetPath.substring(1);
} else if (!fullPath.endsWith('/') && !targetPath.startsWith('/')) {
fullPath += '/' + targetPath;
} else {
fullPath += targetPath;
}
}

targetUrl += fullPath + queryString;

// Prepare headers for the proxy request
var headers = {};

// Copy relevant headers from the original request
if (req.headers['content-type']) {
headers['content-type'] = req.headers['content-type'];
}
if (req.headers['accept']) {
headers['accept'] = req.headers['accept'];
}

// Add OpenHIM token authentication
var authType = openhimConfig.authType || 'Bearer';
headers['Authorization'] = authType + ' ' + openhimConfig.token;

var options = {
method: req.method.toUpperCase(),
url: targetUrl,
headers: headers,
json: req.method !== 'GET' ? req.payload : undefined,
timeout: 30000, // 30 second timeout
rejectUnauthorized: openhimConfig.rejectUnauthorized !== false // Default to true unless explicitly set to false
};

request(options, function (error, response, body) {
if (error) {
console.error('OpenHIM proxy error:', error);
return reply(
Boom.badGateway('Failed to connect to OpenHIM server')
);
}

// Forward the response
var replyObject = reply(body);

// Copy important response headers
if (response.headers['content-type']) {
replyObject.type(response.headers['content-type']);
}

// Set the status code
replyObject.code(response.statusCode);
});
},
description: 'Proxy requests to OpenHIM server',
notes:
'Forwards requests transparently to the configured OpenHIM server with token authentication',
tags: ['api', 'proxy'],
validate: {
options: {
allowUnknown: true
},
params: {
path: Joi.string()
.optional()
.description('Path to forward to OpenHIM')
}
}
}
}
];

Expand Down