2.0.0 • Published 4 years ago

@donsky/node-gateway v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Node-Gateway

This is the gateway side of the SOA: Working Example

Prerequisites ( standard AMQP 0-9-1 ):

  • AMQP Server/Cluster accepting connections with credentials. ( see Local setup )
  • Consumer Service(s)/Cluster(s) responding on responseTopic with CorrelationID. (@donsky/node-service )

Impliments:

  • NodeJS
  • AVRO Message Standards & Schema ( dependancy: avsc )
  • AMQP 0-9-1 Communication Standard ( dependancy: amqplib )
  • Babel 7 & ESNext plugins ( dependancy: babel-* )
  • KoaJS net server listening on PORT ( dependancy: koa & koa-* )

Initialize Your Gateway Server

import Gateway from "@donsky/node-gateway"

new Gateway().listen()

Configure

All of the following configurations must be handled in the env vars, or in config in the code. Examples below:

ENV VARS

Add the following to your bashrc, or bash profile depending on environment. I use docker-compose so I just attach them to the container environment. These will be picked up automatically by the default config file.

export PORT=80
export MQ_PROTOCOL=amqp
export MQ_HOSTNAME=rabbitmq
export MQ_PORT=5672
export MQ_USERNAME=defaultAdmin
export MQ_PASSWORD=SomePassword
The hostname can be a URI or a local hostname, in this example, 'rabbitmq' is my docker container hostname. During deploy this would change and be environment specific.
Code
const config = {
  mq: {
    username: "defaultAdmin",
    password: "SomePassword"
    hostname: "rabbitmq"
    port    : 5672
  },
  koaMiddleware: [
    require( "koa-cors"   )(),
    require( "koa-helmet" )(),
    require( "koa-morgan" )( "combined" ),
    require( "koa-sslify" )({
      trustProtoHeader     : true,
      redirectMethods      : [ "HEAD", "OPTIONS", "GET", "POST" ],
      specCompliantDisallow: true
    })
  ],
  port: 80
}

Action Object Options:

// action.js

/** 
 * Required Attributes
 */
const required = {
  topic       : "consumerTopic",
  name        : "consumerActionName",
  requestAVRO : [
    { name: "firstName", type: "string" }
  ],
  responseAVRO: [
    { name: "response", type: "string" }
  ]
}
> I share a file with my service

/**
 * Optional Attributes
 */
const optional = {
  /**
   * Defaults to (ctx)=>ctx
   * One transformer is fine this is
   * just an example of chaining.
   */
  requestTransformers : [
    ( ctx ) => ctx.request.body.firstName,
    ( firstName )=> requestAVRO
  ],
  /**
   * Defaults to (responseAVRO)=>responseAVRO
   * One transformer is fine this is
   * just an example of chaining.
   */
  responseTransformers: [ // Optional
    ( responseAVRO ) => ( responseAVRO.response ),
    ( response ) => ( { lastName: response } )
  ]
}

export default { ...required, ...optional }

Events:

"ready" - When the server is listening
"error" - If something goes wrong

Example:

import Gateway from "@donsky/node-gateway"
import action from "./action"

const gateway = new Gateway({port:80})
gateway
  .on( "ready", msg => msg |> console.log )
  .on( "error", err => err |> console.error )
  .post( "/route", action /*[ action2, action3, ...]*/ )
  /*.get( "/path/:id", getPathId ) */
  .listen() // This is new, and now required to start the listener

Notes:

  • Without any actions the server should start, but it won't do much
  • Port 80 is not allowed to be exposed on a mac, so the default is actually 8080: This can be found in (./lib/config.js).port
  • This works well with PM2 (Process Manager 2)

Local:

May I recommend spinning up a docker environment for those prerequisites, and getting all the services talking:

# Note: I use a docker container
version: "3.8"
services:
  rabbitmq:
    image: rabbitmq:management
    ports:
     - "5672:5672"
     - "15672:15672"
    environment:
      # ${ENV_VAR} work here for values as well
      RABBITMQ_DEFAULT_USER: defaultAdmin
      RABBITMQ_DEFAULT_PASS: SomePassword
    restart: on-failure   
  gateway:
    build:
      context: ./
    ports:
     - "80:80"
    environment:
     - NODE_ENV=local
     - HOSTNAME=gateway
     - PORT=80
     - MQ_PROTOCOL=amqp
     - MQ_HOSTNAME=rabbitmq
     - MQ_PORT=5672
     - MQ_USERNAME=defaultAdmin
     - MQ_PASSWORD=SomePassword
    depends_on:
     - rabbitmq
    restart: on-failure

Donate

Use this Patreon link to donate to this project... and thank you!

2.0.0

4 years ago

1.0.0

4 years ago

0.5.2

4 years ago

0.5.1

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.3.2-alpha

4 years ago

0.3.1-alpha

4 years ago

0.3.3

4 years ago

0.2.10

4 years ago

0.2.9

4 years ago

0.3.0-alpha

4 years ago

0.2.7

4 years ago

0.2.8

4 years ago

0.2.6

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.0

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.3

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago