Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// intercept console calls and use built in (pino) logger instead
logger.interceptConsole()
const { nodeEnv } = config.environment
console.log('starting application', { nodeEnv })
// create koa application and router
const app = new Koa()
const router = new Router()
// create routes
router.get('/', (ctx) => {
ctx.status = 200
})
router.get('/metrics', middleware.koa.prometheusMetrics())
router.get(
'/health',
middleware.koa.healthCheck([
async () => {
// fake check
return new Promise((resolve, reject) => {
if (Math.random() < 0.2) {
return reject(new Error('check failed'))
}
resolve()
})
},
])
)
async () => {
// fake check
return new Promise((resolve, reject) => {
if (Math.random() < 0.2) {
return reject(new Error('check failed'))
}
resolve()
})
},
])
)
// register middleware
app.use(middleware.koa.errorHandler())
app.use(middleware.koa.requestLogger())
app.use(router.routes())
app.use(router.allowedMethods())
// use `stoppable` to stop accepting new connections and closes existing,
// idle connections(including keep - alives) without killing requests that are in-flight
// on .stop() call
const server = stoppable(http.createServer(app.callback()))
// start server
server.listen(cfg.server.port)
server.once('listening', () => {
const { port } = server.address()
console.log(`server is listening on port ${port}`)
})
server.once('error', (err) => {