Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hapi Passthrough #100

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion config/config_docker.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"app": {
"port": 3000,
"mllpPort": 3001
"mllpPort": 3001,
"hapiPassthrough": true
},
"mediator": {
"api": {
Expand Down
14 changes: 14 additions & 0 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,17 @@ export function getMetadata(): any {
}
}
}

export function hapiPassthrough(req: Request): any {
const requestUrl = new URL(req.url)
const targetUri = config.get('fhirServer:baseURL') + requestUrl.pathname + requestUrl.search

logger.info(`Getting ${targetUri}`)

const options = {
username: config.get('fhirServer:username'),
password: config.get('fhirServer:password'),
}

return got(targetUri, options)
}
17 changes: 12 additions & 5 deletions src/routes/fhir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import express, { Request, Response } from 'express'
import got from 'got'
import URI from 'urijs'
import config from '../lib/config'
import { invalidBundle, invalidBundleMessage } from '../lib/helpers'
import { hapiPassthrough, invalidBundle, invalidBundleMessage } from '../lib/helpers'
import logger from '../lib/winston'
import { generateSimpleIpsBundle } from '../workflows/ipsWorkflows'
import { getResourceTypeEnum, isValidResourceType } from '../lib/validate'
Expand All @@ -22,21 +22,27 @@ router.get('/:resource/:id?/:operation?', async (req: Request, res: Response) =>
try {
let uri = URI(config.get('fhirServer:baseURL'))

if(isValidResourceType(req.params.resource)) {
if (isValidResourceType(req.params.resource)) {
uri = uri.segment(getResourceTypeEnum(req.params.resource).toString())
} else {
return res.status(400).json({ message: `Invalid resource type ${req.params.resource}` })
if (config.get('app:hapiPassthrough'))
return hapiPassthrough(req)
else
return res.status(400).json({ message: `Invalid resource type ${req.params.resource}` })
}

if (req.params.id && /^[a-zA-Z0-9\-_]+$/.test(req.params.id)) {
uri = uri.segment(encodeURIComponent(req.params.id))
} else {
return res.status(400).json({ message: `Invalid resource id ${req.params.id}` })
if (config.get('app:hapiPassthrough'))
return hapiPassthrough(req)
else
return res.status(400).json({ message: `Invalid resource id ${req.params.id}` })
}

for (const param in req.query) {
const value = req.query[param]
if(value && /^[a-zA-Z0-9\-_]+$/.test(value.toString())) {
if (value && /^[a-zA-Z0-9\-_]+$/.test(value.toString())) {
uri.addQuery(param, encodeURIComponent(value.toString()))
} else {
return res.status(400).json({ message: `Invalid query parameter ${param}=${value}` })
Expand Down Expand Up @@ -79,6 +85,7 @@ router.get('/:resource/:id?/:operation?', async (req: Request, res: Response) =>

res.status(200).json(result)
} catch (error) {

return res.status(500).json(error)
}
})
Expand Down
Loading