Skip to content
Snippets Groups Projects
Commit ed6f1a26 authored by anne.matthes's avatar anne.matthes
Browse files

Fix maniest service and add healthy test

parent 66cc346c
No related branches found
No related tags found
No related merge requests found
...@@ -2,5 +2,12 @@ ...@@ -2,5 +2,12 @@
// Note: actual env vars supersede .env file and .env file supersedes .env.defaults file // Note: actual env vars supersede .env file and .env file supersedes .env.defaults file
require('dotenv-defaults').config() require('dotenv-defaults').config()
const createApp = require('./src/server') const logger = require('pino')()
createApp() const createApp = require('./src/createApp')
const app = createApp()
// Binds and listens for connections on the specified host and port
app.listen(process.env.PORT, () => {
logger.info(`manifest-service listening on port ${process.env.PORT}`)
})
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
"swagger-ui-express": "^4.1.6" "swagger-ui-express": "^4.1.6"
}, },
"devDependencies": { "devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.7.0",
"eslint": "^7.20.0", "eslint": "^7.20.0",
"eslint-plugin-import": "^2.22.1", "eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^24.4.0", "eslint-plugin-jest": "^24.4.0",
...@@ -37,8 +39,11 @@ ...@@ -37,8 +39,11 @@
"jest-extended": "^0.11.5", "jest-extended": "^0.11.5",
"jest-junit": "^12.2.0", "jest-junit": "^12.2.0",
"lint-staged": ">=10", "lint-staged": ">=10",
"mock-fs": "^5.0.0",
"nodemon": "^2.0.7", "nodemon": "^2.0.7",
"pino-pretty": "^4.7.1" "pino-pretty": "^4.7.1",
"superagent": "^6.1.0",
"supertest": "^6.1.6"
}, },
"lint-staged": { "lint-staged": {
"*.js": "eslint --cache --fix" "*.js": "eslint --cache --fix"
......
const { describe, it } = require('@jest/globals')
describe('Manifest service', () => {
it('tests nothing', () => {})
})
const { describe, it, expect } = require('@jest/globals')
const request = require('supertest')
const createApp = require('../src/createApp')
describe('Manifest service', () => {
it('is healthy', async () => {
const app = createApp()
return await request(app)
.get('/healthy')
.then(response => {
expect(response.statusCode).toBe(200)
})
})
})
...@@ -26,59 +26,61 @@ const fs = require('fs') ...@@ -26,59 +26,61 @@ const fs = require('fs')
const fetch = require('node-fetch') const fetch = require('node-fetch')
const swaggerDocument = yaml.load(fs.readFileSync('./swagger.yaml', 'utf8')) const swaggerDocument = yaml.load(fs.readFileSync('./swagger.yaml', 'utf8'))
// The app returned by express() is in fact a JavaScript Function, designed to be passed to Node’s HTTP servers as a callback to handle requests. module.exports = () => {
// The app returned by express() is in fact a JavaScript Function, designed to be passed to Node’s HTTP servers as a callback to handle requests.
const app = express() const app = express()
const healthCheck = new health.HealthChecker() const healthCheck = new health.HealthChecker()
// Application-level middleware // Application-level middleware
app.use(httpLogger) app.use(httpLogger)
app.use(helmet()) app.use(helmet())
app.use('/healthy', health.LivenessEndpoint(healthCheck)) app.use('/healthy', health.LivenessEndpoint(healthCheck))
app.use('/ready', health.ReadinessEndpoint(healthCheck)) app.use('/ready', health.ReadinessEndpoint(healthCheck))
app.use(apiMetrics({ excludeRoutes: ignorePaths })) app.use(apiMetrics({ excludeRoutes: ignorePaths }))
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)) app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument))
app.use('/swagger.json', (req, res) => res.json(swaggerDocument)) app.use('/swagger.json', (req, res) => res.json(swaggerDocument))
const isReady = async () => { const isReady = async () => {
await fetchManifest() await fetchManifest()
if (manifestCache.length === 0) throw new Error('No manifests found.') if (manifestCache.length === 0) throw new Error('No manifests found.')
} }
const readinessCheck = new health.ReadinessCheck('readinessCheck', isReady)
healthCheck.registerReadinessCheck(readinessCheck)
const livenessCheck = new health.LivenessCheck('livenessCheck', isReady) const readinessCheck = new health.ReadinessCheck('readinessCheck', isReady)
healthCheck.registerLivenessCheck(livenessCheck) healthCheck.registerReadinessCheck(readinessCheck)
const urls = yaml.load(fs.readFileSync('./config/manifests/urls.yaml', 'utf8')).manifests const livenessCheck = new health.LivenessCheck('livenessCheck', isReady)
healthCheck.registerLivenessCheck(livenessCheck)
let manifestCache = [] const urls = yaml.load(fs.readFileSync('./config/manifests/urls.yaml', 'utf8')).manifests
let lastCached
const fetchManifest = async () => { let manifestCache = []
if (+new Date() < lastCached + 30000) return let lastCached
const results = urls.map(url => fetch(url).then(result => {
if (!result.ok) throw new Error(`Failed to load manifest for url ${result.url} (Status: ${result.status}: ${result.statusText})`)
return result.json().catch(err => { throw new Error(`Failed to load manifest for url ${result.url}: ${err}`) })
}))
manifestCache = (await Promise.all(results)).flat()
lastCached = +new Date()
}
app.get('/api/manifest.json', async (req, res, next) => { const fetchManifest = async () => {
try { if (+new Date() < lastCached + 30000) return
await fetchManifest() const results = urls.map(url => fetch(url).then(result => {
res.json(manifestCache || []) if (!result.ok) throw new Error(`Failed to load manifest for url ${result.url} (Status: ${result.status}: ${result.statusText})`)
} catch (err) { return result.json().catch(err => { throw new Error(`Failed to load manifest for url ${result.url}: ${err}`) })
next(err) }))
manifestCache = (await Promise.all(results)).flat()
lastCached = +new Date()
} }
})
app.use(function (err, req, res, next) { app.get('/api/manifest.json', async (req, res, next) => {
logger.error(err) try {
res.status(500).end() await fetchManifest()
}) res.json(manifestCache || [])
} catch (err) {
module.exports = app next(err)
}
})
app.use(function (err, req, res, next) {
logger.error(err)
res.status(500).end()
})
return app
}
const app = require('./app')
const logger = require('pino')()
// Binds and listens for connections on the specified host and port
app.listen(process.env.PORT, () => {
logger.info(`manifest-service listening on port ${process.env.PORT}`)
})
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment