This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
p-mcgowan@homebot
committed
Dec 5, 2017
1 parent
9437ca9
commit e434d9b
Showing
2 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
node_modules | ||
config.js | ||
./config.js | ||
|
||
public | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const join = require('path').join; | ||
const logFile = require('fs').createWriteStream(join(__dirname, './logs/api.txt'), { flags: 'w' }); | ||
const ospreyAuth = require('./modules/security.js'); | ||
const util = require('util'); | ||
|
||
const config = { | ||
spec: join(__dirname, './spec/hr-fhir-api.raml'), | ||
mountpoint: '/v1', | ||
ospreyConfig: { | ||
server: { | ||
notFoundHandler: false | ||
}, | ||
security: ospreyAuth, | ||
disableErrorInterception: true | ||
}, | ||
ramlOnError: (error, request, response, next) => { | ||
if (error) { | ||
// More granularity later, just 422 for now | ||
if (process.env.NODE_ENV !== 'production') { | ||
config.log(error); | ||
console.trace(error); | ||
return response.status(422).json({ message: 'Bad request', status: 422, error }); | ||
} else { | ||
return response.status(422).json({ message: 'Bad request', status: 422, error: error.message }); | ||
} | ||
} | ||
return next(); | ||
}, | ||
ramlOnNotFound: (request, response, next) => { | ||
if (request.resourcePath) { | ||
return next(); | ||
} | ||
return response.status(404).json({ message: 'Not found', status: 404 }); | ||
}, | ||
log: function() { | ||
let where; | ||
try { throw Error() } catch (err) { where = err.stack; } | ||
logFile.write(`${where}\n` + util.inspect(arguments, { depth: null, colors: true }) + '\n'); | ||
}, | ||
error: (response, error) => { | ||
const message = { message: 'Something went wrong' }; | ||
if (process.env.NODE_ENV !== 'production') { | ||
config.log(error) | ||
console.trace(error); | ||
message.error = error; | ||
} | ||
return response.status(500).json(message); | ||
} | ||
}; | ||
|
||
module.exports = config; |