• This repository has been archived on 05/Aug/2022
  • Stars
    star
    99
  • Rank 343,315 (Top 7 %)
  • Language
    TypeScript
  • License
    Apache License 2.0
  • Created over 6 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Prepare your Node.js application for production

Node.js Service Tools

Prepare your Node.js application for production!

This library provides common functionalities, like graceful error handling & shutdown, structured JSON logging and several HTTP middleware to make your application truly ready for modern containerised environments, like Kubernetes.

Installation

npm i @banzaicloud/service-tools
# or
yarn add @banzaicloud/service-tools

Usage & Examples

This library is written in TypeScript, refer to the published types or the source code for argument and return types.

Examples are available for Express and Koa frameworks. Check out the examples folder!

Main exports

catchErrors(options)

Catch uncaught exceptions and unhandled Promise rejections. It is not safe to resume normal operation after 'uncaughtException'.

const { catchErrors } = require('@banzaicloud/service-tools')

// ...

// the handlers return a Promise
// the handlers are called in order
catchErrors([closeServer, closeDB])

// the error will be caught and the handlers will be called before exiting
throw new Error()

gracefulShutdown(handlers, options)

Graceful shutdown: release resources (databases, HTTP connections, ...) before exiting. When the application receives SIGTERM or SIGINT signals, the close handlers will be called. The handlers should return a Promise.

const { gracefulShutdown } = require('@banzaicloud/service-tools')

// ...

// the handlers return a Promise
// the handlers are called in order
gracefulShutdown([closeServer, closeDB])

logger

A pino structured JSON logger instance configured with config.pino.

const { logger } = require('@banzaicloud/service-tools')

logger.info({ metadata: true }, 'log message')
// > {"level":30,"time":<ts>,"msg":"log message","pid":0,"hostname":"local","metadata":true,"v":1}
Use provided logger instead of console

Globally overwrite the console and use the logger provided by the library to print out messages.

const { logger } = require('@banzaicloud/service-tools')

console.log('log message')
// > log message

logger.interceptConsole()

console.log('log message')
// > {"level":30,"time":<ts>,"msg":"log message","pid":0,"hostname":"local","v":1}

Config

Load configurations dynamically.

config.environment

Uses the NODE_ENV environment variable, with accepted values of: production, development, test.

const { config } = require('@banzaicloud/service-tools')
// validates the NODE_ENV environment variable
console.log(config.environment)
// > { nodeEnv: 'production' }

config.pino

Used by the provided logger. Uses the LOGGER_LEVEL and LOGGER_REDACT_FIELDS environment variables. The LOGGER_LEVEL can be one of the following: fatal, error, warn, info, debug, trace. LOGGER_REDACT_FIELDS is a comma separated list of field names to mask out in the output (defaults to: 'password, pass, authorization, auth, cookie, _object').

const pino = require('pino')
const { config } = require('@banzaicloud/service-tools')

const logger = pino(config.pino)

logger.info({ metadata: true, password: 'secret' }, 'log message')
// > {"level":30,"time":<ts>,"msg":"log message","pid":0,"hostname":"local","metadata":true,"password":"[REDACTED]","v":1}

Middleware (Koa)

Several middleware for the Koa web framework.

errorHandler(options)

Koa error handler middleware.

const Koa = require('koa')
const { koa: middleware } = require('@banzaicloud/service-tools').middleware

const app = new Koa()

// this should be the first middleware
app.use(middleware.errorHandler())

healthCheck(checks, options)

Koa health check endpoint handler.

const Koa = require('koa')
const Router = require('koa-router')
const { koa: middleware } = require('@banzaicloud/service-tools').middleware

// ...

const app = new Koa()
const router = new Router()

// the checks return a Promise
router.get('/health', middleware.healthCheck([checkDB]))

app.use(router.routes())
app.use(router.allowedMethods())

prometheusMetrics(options)

Koa Prometheus metrics endpoint handler. By default it collects some default metrics.

const Koa = require('koa')
const Router = require('koa-router')
const { koa: middleware } = require('@banzaicloud/service-tools').middleware

// ...

const app = new Koa()
const router = new Router()

router.get('/metrics', middleware.prometheusMetrics())

app.use(router.routes())
app.use(router.allowedMethods())

requestValidator(options)

Koa request validator middleware. Accepts Joi schemas for body (body parser required), params and query (query parser required). Returns with 400 if the request is not valid. Assigns validated values to ctx.state.validated.

const joi = require('joi')
const qs = require('qs')
const Koa = require('koa')
const Router = require('koa-router')
const bodyParser = require('koa-bodyparser')
const { koa: middleware } = require('@banzaicloud/service-tools').middleware

// ...

const app = new Koa()
const router = new Router()

const paramsSchema = joi
  .object({
    id: joi
      .string()
      .hex()
      .length(64)
      .required(),
  })
  .required()

const bodySchema = joi.object({ name: joi.string().required() }).required()

const querySchema = joi.object({ include: joi.array().default([]) }).required()

router.get(
  '/',
  middleware.requestValidator({ params: paramsSchema, body: bodySchema, query: querySchema }),
  async function routeHandler(ctx) {
    const { params, body, query } = ctx.state.validated
    // ...
  }
)

app.use(bodyParser())
// query parser
app.use(async function parseQuery(ctx, next) {
  ctx.query = qs.parse(ctx.querystring, options)
  ctx.request.query = ctx.query
  await next()
})
app.use(router.routes())
app.use(router.allowedMethods())

requestLogger(options)

Koa request logger middleware. Useful for local development and debugging.

const Koa = require('koa')
const { koa: middleware } = require('@banzaicloud/service-tools').middleware

// ...

const app = new Koa()

// this should be the second middleware after the error handler
// ...
app.use(middleware.requestLogger())

Middleware (Express)

Several middleware for the Express web framework.

errorHandler(options)

Express error handler middleware.

const express = require('express')
const { express: middleware } = require('@banzaicloud/service-tools').middleware

const app = express()

// this should be the last middleware
app.use(middleware.errorHandler())

healthCheck(checks, options)

Express health check endpoint handler.

const express = require('express')
const { express: middleware } = require('@banzaicloud/service-tools').middleware

// ...

const app = express()

// the checks return a Promise
app.get('/health', middleware.healthCheck([checkDB]))

prometheusMetrics(options)

Express Prometheus metrics endpoint handler. By default it collects some default metrics.

const express = require('express')
const { express: middleware } = require('@banzaicloud/service-tools').middleware

// ...

const app = express()

app.get('/metrics', middleware.prometheusMetrics())

requestValidator(options)

Express request validator middleware. Accepts Joi schemas for body (body parser required), params and query. Returns with 400 if the request is not valid. Assigns validated values to req.

const joi = require('joi')
const express = require('express')
const { express: middleware } = require('@banzaicloud/service-tools').middleware

// ...

const app = express()

const paramsSchema = joi
  .object({
    id: joi
      .string()
      .hex()
      .length(64)
      .required(),
  })
  .required()

const bodySchema = joi.object({ name: joi.string().required() }).required()

const querySchema = joi.object({ include: joi.array().default([]) }).required()

app.use(express.json())
app.get(
  '/',
  middleware.requestValidator({ params: paramsSchema, body: bodySchema, query: querySchema }),
  function routeHandler(req, res) {
    const { params, body, query } = req
    // ...
  }
)

More Repositories

1

bank-vaults

A Vault swiss-army knife: a K8s operator, Go client with automatic token renewal, automatic configuration, multiple unseal options and more. A CLI tool to init, unseal and configure Vault (auth methods, secret engines). Direct secret injection into Pods.
Go
1,864
star
2

pipeline

Banzai Cloud Pipeline is a solution-oriented application platform which allows enterprises to develop, deploy and securely scale container-based applications in multi- and hybrid-cloud environments.
Go
1,494
star
3

logging-operator

Logging operator for Kubernetes based on Fluentd and Fluentbit
Go
1,027
star
4

koperator

Oh no! Yet another Apache Kafka operator for Kubernetes
Go
782
star
5

istio-operator

An operator that manages Istio deployments on Kubernetes
Go
534
star
6

banzai-charts

Curated list of Banzai Cloud Helm charts used by the Pipeline Platform
Mustache
367
star
7

thanos-operator

Kubernetes operator for deploying Thanos
Go
280
star
8

pke

PKE is an extremely simple CNCF certified Kubernetes installer and distribution, designed to work on any cloud, VM or bare metal.
Go
263
star
9

hpa-operator

Horizontal Pod Autoscaler operator for Kubernetes. Annotate and let the HPA operator do the rest.
Go
239
star
10

dast-operator

Dynamic Application and API Security Testing
Go
189
star
11

spark-metrics

Spark metrics related custom classes and sinks (e.g. Prometheus)
Scala
175
star
12

cloudinfo

Cloud instance type and price information as a service
Go
165
star
13

telescopes

Telescopes is a cloud instance types and full cluster layout recommender consisting of on-demand and spot/preemptible AWS EC2, Google, Azure, Oracle and Alibaba cloud instances.
Go
163
star
14

hollowtrees

A ruleset based watchguard to keep spot/preemptible instance based clusters safe, with plugins for VMs, Kubernetes, Prometheus and Pipeline
Go
155
star
15

kurun

Run main.go in Kubernetes with one command, also port-forward your app into Kubernetes
Go
132
star
16

jwt-to-rbac

JWT-to-RBAC lets you automatically generate RBAC resources based on JWT tokens
Go
113
star
17

nodepool-labels-operator

Nodepool Labels operator for Kubernetes
Go
69
star
18

drone-kaniko

A thin shim-wrapper around the official Google Kaniko Docker image to make it behave like the Drone Docker plugin.
Shell
56
star
19

pvc-operator

Go
51
star
20

satellite

Determine your cloud provider with a simple HTTP call
Go
50
star
21

prometheus-jmx-exporter-operator

Go
45
star
22

chartsec

Helm Chart security scanner
Go
45
star
23

anchore-image-validator

Anchore Image Validator lets you automatically detect or block security issues just before a Kubernetes pod starts.
Go
44
star
24

spot-price-exporter

Prometheus exporter to track spot price history
Go
41
star
25

logrus-runtime-formatter

Golang runtime package based automatic function, line and package fields for Logrus
Go
40
star
26

kubeconfiger

Example tool for cleaning up untrusted kubeconfig files
Go
36
star
27

spot-termination-exporter

Prometheus spot instance exporter to monitor AWS instance termination with Hollowtrees
Go
36
star
28

imps

ImagePullSecrets controller allows you to distribute image pull secrets based on namespace/pod matches.
Go
32
star
29

banzai-cli

CLI for Banzai Cloud Pipeline platform
Go
30
star
30

allspark

AllSpark is a simple building block for quickly building web microservice deployments for demo purposes.
Go
26
star
31

istio-client-go

Golang API representation for Istio resources
Go
23
star
32

istio-external-demo

Working example for restricting access to external services from an Istio service mesh
23
star
33

k8s-cncf-meetup

Kubernetes and Cloud Native Computing Meetup slides
DIGITAL Command Language
19
star
34

spot-config-webhook

A Kubernetes mutating admission webhook that sets the scheduler of specific pods based on a ConfigMap.
Go
19
star
35

aws-billing-alarm

Shell
15
star
36

aws-autoscaling-exporter

Prometheus exporter with AWS auto scaling group and instance level metrics for Hollowtrees
Go
14
star
37

docker-cruise-control

Linkedin's Cruise Control container image built for Koperator (https://github.com/banzaicloud/koperator)
Shell
13
star
38

lambda-slack-bot

AWS Lambda Golang Slack bot to list running EC2 instances
Go
12
star
39

fluent-plugin-label-router

Fluentd plugin to route records based on Kubernetes labels and namespace
Ruby
11
star
40

preemption-exporter

Prometheus preemptible instance exporter to monitor GCP instance termination with Hollowtrees
Go
11
star
41

docker-kafka

Dockerfile to building Docker image for Apache Kafka
Dockerfile
10
star
42

ht-k8s-action-plugin

Hollowtrees plugin used to interact with Kubernetes on specific event triggers
Go
10
star
43

crd-updater

Helm 3 library chart that emulates Helm 2 CRD update behavior.
Go
10
star
44

jmx-exporter-loader

Java
10
star
45

go-code-generation-demo

Go
9
star
46

bank-vaults-workshop

Material for the Hacktivity 2019 - Bank-Vaults workshop
7
star
47

pipeline-cp-launcher

Pipeline ControlPlane launcher using AWS Cloudformation or Azure ARM template, running on Kubernetes
Makefile
7
star
48

go-cruise-control

It's client library written in Golang for interacting with Linkedin Cruise Control using its HTTP API.
Go
6
star
49

zeppelin-pdi-example

6
star
50

bank-vaults-docs

Bank-Vaults documentation
Shell
5
star
51

logging-operator-docs

Logging operator documentation
Shell
4
star
52

koperator-docs

Documentation for Koperator - the operator for managing Apache Kafka on Kubernetes
HTML
4
star
53

banzai-types

Common types, configs and utils used across several Banzai Cloud projects
Go
4
star
54

circleci-orbs

Makefile
3
star
55

ht-aws-asg-action-plugin

Hollowtrees plugin to detach instances from auto scaling groups
Go
3
star
56

drone-plugin-sonar

Drone plugin for static code analysis
Go
3
star
57

fluent-plugin-tag-normaliser

Fluent output plugin to transform tags based on record content
Ruby
2
star
58

cicd-go

A Go client for the Pipeline CI/CD subsystem
Go
2
star
59

gin-utilz

Gin framework utilities
Go
2
star
60

banzailint

Custom lint rules for Banzai Cloud code
Makefile
2
star
61

docker-jmx-exporter

Docker image for JMX-exporter
Dockerfile
2
star
62

.github

Github default files
1
star
63

developer-guide

Guide for developers on writing code and maintaining projects
1
star
64

cadence-aws-sdk

Cadence wrapper around the AWS Go SDK to make working with the AWS API easier in Cadence workflows
Go
1
star
65

drone-plugin-k8s-client

Drone plugin implementation for k8s operations
Go
1
star
66

thanos-operator-docs

Thanos operator documentation
Shell
1
star
67

dynamic-class-gen

Java
1
star
68

custom-runner

go custom runner
Go
1
star
69

cluster-registry

Go
1
star
70

log-socket

Service and CLI tool for forwarding logs through a WebSocket connection
Go
1
star
71

kube-service-annotate

A Kubernetes mutating webhook to annotate services based on rules
Go
1
star
72

pipeline-sdk

SDK for extending Pipeline
Go
1
star
73

integrated-service-sdk

Client SDK for the Integrated Service Operator
Go
1
star
74

pipeline-cp-images

Pipeline ControlPlane images
Shell
1
star
75

dockerized-newman

Automated end-2-end testing with Postman in Docker
1
star
76

drone-plugin-zeppelin-client

Zeppelin REST API client plugin for Drone. A step in the Pipeline PaaS CI/CD component to provision a Kubernetes cluster or use a managed one
Go
1
star