How to use @banzaicloud/service-tools - 8 common examples

To help you get started, we’ve selected a few @banzaicloud/service-tools examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github banzaicloud / service-tools / examples / express / index.js View on Github external
middleware.express.healthCheck([
    async () => {
      // fake check
      return new Promise((resolve, reject) => {
        if (Math.random() < 0.2) {
          return reject(new Error('check failed'))
        }

        resolve()
      })
    },
  ])
)

// register error middleware
app.use(middleware.express.errorHandler())

// 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))
// 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) => {
  console.error('server error', err)
  process.exit(1)
github banzaicloud / service-tools / examples / koa / index.js View on Github external
const http = require('http')
const { promisify } = require('util')
const { config, catchErrors, logger, middleware, gracefulShutdown } = require('@banzaicloud/service-tools')
const stoppable = require('stoppable')
const Koa = require('koa')
const Router = require('koa-router')
const cfg = require('./config')

// catch all uncaught exceptions and unhandled promise rejections and exit application
catchErrors([closeResources])

// 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
github banzaicloud / service-tools / examples / express / index.js View on Github external
'use strict'

const http = require('http')
const { promisify } = require('util')
const { config, catchErrors, logger, middleware, gracefulShutdown } = require('@banzaicloud/service-tools')
const stoppable = require('stoppable')
const express = require('express')
const cfg = require('./config')

// catch all uncaught exceptions and unhandled promise rejections and exit application
catchErrors([closeResources])

// intercept console calls and use built in (pino) logger instead
logger.interceptConsole()

const { nodeEnv } = config.environment
console.log('starting application', { nodeEnv })

// create an express application
const app = express()

// create routes
app.get('/', (req, res) => {
  res.status(200).end()
})
app.get('/metrics', middleware.express.prometheusMetrics())
app.get(
  '/health',
  middleware.express.healthCheck([
    async () => {
      // fake check
      return new Promise((resolve, reject) => {
github banzaicloud / service-tools / examples / koa / index.js View on Github external
'use strict'

const http = require('http')
const { promisify } = require('util')
const { config, catchErrors, logger, middleware, gracefulShutdown } = require('@banzaicloud/service-tools')
const stoppable = require('stoppable')
const Koa = require('koa')
const Router = require('koa-router')
const cfg = require('./config')

// catch all uncaught exceptions and unhandled promise rejections and exit application
catchErrors([closeResources])

// 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([
github banzaicloud / service-tools / examples / express / index.js View on Github external
'use strict'

const http = require('http')
const { promisify } = require('util')
const { config, catchErrors, logger, middleware, gracefulShutdown } = require('@banzaicloud/service-tools')
const stoppable = require('stoppable')
const express = require('express')
const cfg = require('./config')

// catch all uncaught exceptions and unhandled promise rejections and exit application
catchErrors([closeResources])

// intercept console calls and use built in (pino) logger instead
logger.interceptConsole()

const { nodeEnv } = config.environment
console.log('starting application', { nodeEnv })

// create an express application
const app = express()

// create routes
app.get('/', (req, res) => {
  res.status(200).end()
})
app.get('/metrics', middleware.express.prometheusMetrics())
app.get(
  '/health',
  middleware.express.healthCheck([
    async () => {
github banzaicloud / service-tools / examples / express / index.js View on Github external
catchErrors([closeResources])

// intercept console calls and use built in (pino) logger instead
logger.interceptConsole()

const { nodeEnv } = config.environment
console.log('starting application', { nodeEnv })

// create an express application
const app = express()

// create routes
app.get('/', (req, res) => {
  res.status(200).end()
})
app.get('/metrics', middleware.express.prometheusMetrics())
app.get(
  '/health',
  middleware.express.healthCheck([
    async () => {
      // fake check
      return new Promise((resolve, reject) => {
        if (Math.random() < 0.2) {
          return reject(new Error('check failed'))
        }

        resolve()
      })
    },
  ])
)
github banzaicloud / service-tools / examples / koa / index.js View on Github external
// 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()
      })
    },
  ])
)
github banzaicloud / service-tools / examples / koa / index.js View on Github external
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) => {

@banzaicloud/service-tools

Node.js service tools for common use cases

Apache-2.0
Latest version published 3 years ago

Package Health Score

47 / 100
Full package analysis