Skip to content

Commit

Permalink
First release (#1)
Browse files Browse the repository at this point in the history
* fix: pipeline

* ⚡

* ⚡

* ⚡

* fix: transform package into module import only

* Update src/index.ts

Co-authored-by: João Mantovani <jpm.mantovani45@gmail.com>

* Update README.md

Co-authored-by: João Mantovani <jpm.mantovani45@gmail.com>

* ⚡

* chore: include build workflow
docs: update README with new badges

* docs: update badges

---------

Co-authored-by: João Mantovani <jpm.mantovani45@gmail.com>
  • Loading branch information
gritzkoo and joaomantovani authored Feb 21, 2023
1 parent cbfffa2 commit d38bb54
Show file tree
Hide file tree
Showing 10 changed files with 641 additions and 468 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: build
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: repo clone
uses: actions/checkout@v3
- name: setup node version
uses: actions/setup-node@v1
with:
node-version: 16.x
- name: build
run: |
npm install --legacy-peer-deps
npm run build
27 changes: 1 addition & 26 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,16 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
# keep the versions below up to date with https://nodejs.org/en/about/releases/
# use only up to lts
node-version: [14.x, 16.x]
redis-version: [6]
services:
postgresql:
image: postgres
env:
POSTGRES_PASSWORD: root
ports:
- 5432:5432

steps:
- name: Git checkout
uses: actions/checkout@v2

- name: Setup DynamoDB Local
uses: rrainn/dynamodb-action@v2.0.0
with:
port: 8000
cors: "*"

- name: Configure Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Start Redis
uses: supercharge/redis-github-action@1.1.0
with:
redis-version: ${{ matrix.redis-version }}

- name: Sets up a Memcached server
uses: niden/actions-memcached@v7

- name: Cache node_modules
id: cache-modules
uses: actions/cache@v2
Expand All @@ -53,7 +28,7 @@ jobs:
run: npm install --legacy-peer-deps

- name: Run test in version ${{ matrix.node-version }}
run: npm run test:coverage
run: npm run test
env:
CI: true

Expand Down
237 changes: 236 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,237 @@
# nodejs-health-checker-lw
A successor package for nodejs-health-checker

<div align="center">

![npm](https://img.shields.io/npm/dt/nodejs-health-checker-lw?style=for-the-badge)

[![test](https://github.com/gritzkoo/nodejs-health-checker-lw/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/gritzkoo/nodejs-health-checker-lw/actions/workflows/main.yml)
[![build](https://github.com/gritzkoo/nodejs-health-checker-lw/actions/workflows/build.yaml/badge.svg?branch=main)](https://github.com/gritzkoo/nodejs-health-checker-lw/actions/workflows/build.yaml)
[![Coverage Status](https://coveralls.io/repos/github/gritzkoo/nodejs-health-checker-lw/badge.svg?branch=main)](https://coveralls.io/github/gritzkoo/nodejs-health-checker-lw?branch=main)
![GitHub](https://img.shields.io/github/license/gritzkoo/nodejs-health-checker-lw)
![GitHub issues](https://img.shields.io/github/issues/gritzkoo/nodejs-health-checker-lw)
![npm](https://img.shields.io/npm/v/nodejs-health-checker-lw)
[![Tag Status](https://img.shields.io/github/v/tag/gritzkoo/nodejs-health-checker-lw)](https://img.shields.io/github/v/tag/gritzkoo/nodejs-health-checker-lw)
[![Languages Status](https://img.shields.io/github/languages/count/gritzkoo/nodejs-health-checker-lw)](https://img.shields.io/github/languages/count/gritzkoo/nodejs-health-checker-lw)
[![Repo Size Status](https://img.shields.io/github/repo-size/gritzkoo/nodejs-health-checker-lw)](https://img.shields.io/github/repo-size/gritzkoo/nodejs-health-checker-lw)
</div>
____

A successor package for [nodejs-health-checker](https://github.com/gritzkoo/nodejs-health-checker) to simplify health checks.

The main purpose of this package is to standardize the liveness and readiness actions for Nodejs applications, for Kubernetes deployments.

## How to install

```sh
npm i --save nodejs-health-checker-lw
```

OR

```sh
yarn add nodejs-health-checker-lw
```

## Creating your own checkers

It's important to keep in mind that you need to create the tests you API needs as the example below:

```ts
// file /app/src/github.ts
import { Check } from 'nodejs-health-checker-lw'
import 'fetch' from 'node-fetch'

export const MyIntegrationTest = async(): Promise<Check> => {
const check = new Check({url: 'https://github.com/status'})
fetch(check.url,{
timeout: 10
}).then( response => {
check.status = response.status === 200;
check.error = response.status !== 200 ? { http_status: response.status } : undefined,
resolve(check)
}).catch( error => {
check.error = error
resolve(check)
})
}
```

## How to init

```ts
import { HealthChecker, Check } from 'nodejs-health-checker-lw'
import { MyIntegrationTest } from 'src/github'
export const check = new HealthChecker({
name: 'myapp',
version: 'v1.0.0',
// integrations are the list of outside integrations of your API you need to test
// to guarantee that your service is healthy to work
integrations: [
{
// name is just a string that will help you fast identify the integration you need to check-on
name: 'github integration',
//
handle: MyIntegrationTest
}
}
]
})
```

___

## Liveness method

Will return an `JSON` as below:

```json
{
"status": "fully functional",
"version": "v1.0.0"
}
```

## Readiness method

Will return an `JSON` as below:

```jsonc
{
"name": "myapp",
"version": "v1.0.0",
// the main status of all integrations
// will return true when none of integrations fails
"status": true,
"date": "2022-07-10T00:46:19.186Z",
"duration": 0.068,
"integrations": [
{
"name": "github integration",
"status": true,
"response_time": 0.068,
"url": "https://github.com/status"
}
]
}
```

___

## How to use it

Once you create a constant with an instance of HealthChecker, you can now call the methods, liveness, and readiness, in your application, in CLI or API mode

### CLI interface

```js
import check from 'src/to/your/check/const'
const cliArgs = process.argv.slice(2)
switch (cliArgs[0]) {
case 'liveness':
console.log(check.liveness())
break
case 'readiness':
check.readiness().then(response => console.log(response))
break
default:
console.error(`invalid option: ${cliArgs[0]}... accepty only liveness or readiness`)
}
```

In `Kubernetes` deployment

``` yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: your-nodejs-app
spec:
selector:
matchLabels:
app: your-nodejs-app
template:
metadata:
labels:
app: your-nodejs-app
spec:
containers:
- name: your-nodejs-app
image: your-nodejs-app:tag
resources:
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 80
livenessProbe:
exec:
command:
- "/bin/node"
- "your-script.js"
- "liveness"
readinessProbe:
exec:
command:
- "/bin/node"
- "your-script.js"
- "readiness"
```
____
## HTTP interface
In `javascript`

```js
import express from 'express'
import check from 'src/to/your/check/const'
const PORT = process.env.PORT||80;
const server = express()
server.get('/health-check/liveness', (_, res) => {
res.json(check.liveness())
})
server.get('/health-check/readiness', async (_, res) => {
res.json(await check.readiness())
})
server.listen(PORT, () => {
console.log(`[SERVER] Running at http://localhost:${PORT}`);
});
```

In `Kubernetes deployment`

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: your-nodejs-app
spec:
selector:
matchLabels:
app: your-nodejs-app
template:
metadata:
labels:
app: your-nodejs-app
spec:
containers:
- name: your-nodejs-app
image: your-nodejs-app:tag
resources:
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 80
livenessProbe:
httpGet:
path: /health-check/liveness
port: http
readinessProbe:
httpGet:
path: /health-check/readiness
port: http
```
____
3 changes: 2 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
image: node:16.15.1
working_dir: /app/
command: npm run dev
# user: "1000:1000"
# userns_mode: "root"
volumes:
- .:/app

Empty file added docs/index.md
Empty file.
Loading

0 comments on commit d38bb54

Please sign in to comment.