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 @@
// Note: actual env vars supersede .env file and .env file supersedes .env.defaults file
require('dotenv-defaults').config()
const createApp = require('./src/server')
createApp()
const logger = require('pino')()
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 @@
"swagger-ui-express": "^4.1.6"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.7.0",
"eslint": "^7.20.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^24.4.0",
......@@ -37,8 +39,11 @@
"jest-extended": "^0.11.5",
"jest-junit": "^12.2.0",
"lint-staged": ">=10",
"mock-fs": "^5.0.0",
"nodemon": "^2.0.7",
"pino-pretty": "^4.7.1"
"pino-pretty": "^4.7.1",
"superagent": "^6.1.0",
"supertest": "^6.1.6"
},
"lint-staged": {
"*.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')
const fetch = require('node-fetch')
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
app.use(httpLogger)
app.use(helmet())
app.use('/healthy', health.LivenessEndpoint(healthCheck))
app.use('/ready', health.ReadinessEndpoint(healthCheck))
app.use(apiMetrics({ excludeRoutes: ignorePaths }))
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument))
app.use('/swagger.json', (req, res) => res.json(swaggerDocument))
// Application-level middleware
app.use(httpLogger)
app.use(helmet())
app.use('/healthy', health.LivenessEndpoint(healthCheck))
app.use('/ready', health.ReadinessEndpoint(healthCheck))
app.use(apiMetrics({ excludeRoutes: ignorePaths }))
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument))
app.use('/swagger.json', (req, res) => res.json(swaggerDocument))
const isReady = async () => {
await fetchManifest()
if (manifestCache.length === 0) throw new Error('No manifests found.')
}
const readinessCheck = new health.ReadinessCheck('readinessCheck', isReady)
healthCheck.registerReadinessCheck(readinessCheck)
const isReady = async () => {
await fetchManifest()
if (manifestCache.length === 0) throw new Error('No manifests found.')
}
const livenessCheck = new health.LivenessCheck('livenessCheck', isReady)
healthCheck.registerLivenessCheck(livenessCheck)
const readinessCheck = new health.ReadinessCheck('readinessCheck', isReady)
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 = []
let lastCached
const urls = yaml.load(fs.readFileSync('./config/manifests/urls.yaml', 'utf8')).manifests
const fetchManifest = async () => {
if (+new Date() < lastCached + 30000) return
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()
}
let manifestCache = []
let lastCached
app.get('/api/manifest.json', async (req, res, next) => {
try {
await fetchManifest()
res.json(manifestCache || [])
} catch (err) {
next(err)
const fetchManifest = async () => {
if (+new Date() < lastCached + 30000) return
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.use(function (err, req, res, next) {
logger.error(err)
res.status(500).end()
})
module.exports = app
app.get('/api/manifest.json', async (req, res, next) => {
try {
await fetchManifest()
res.json(manifestCache || [])
} catch (err) {
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