0.0.9-alpha • Published 2 years ago

moleculer-universal-gateway v0.0.9-alpha

Weekly downloads
-
License
-
Repository
github
Last release
2 years ago

moleculer-universal-gateway

moleculer-universal-gateway allows you to use any node API app (e.g. express, koa, hapi, feathers, nestjs) as an API gateway.

Installation

npm i moleculer-universal-gateway

Usage

Create your app, returning an object with start, and stop methods - these are used to start and stop the server. Below is a simple example using express, you're free to create your app in any way you wish as long as it returns the two methods.

Note that the dependency services are passed in as parameters, so their implemation is decoupled (good for maintenance and testing).

export function createApp({
  logger,
  math,
}: {
  logger: SomeLoggerService;
  math: SomeMathService;
}) {
  const app = express();
  let server: Server | null = null;

  app.get('/add', async (req, res) => {
    const added = await math.add({ a: 1, b: 2 });

    res.json({ added });
  });

  return {
    start,
    stop,
  };

  function start() {
    return new Promise<void>((resolve) => {
      server = app.listen(PORT, () => {
        logger.info(`Listening on port ${PORT}`);

        resolve());
    });
  }

  function stop() {
    return new Promise<string>((resolve, reject) => {
      if (server?.listening) {
        server.close((err) => {
          if (err) {
            logger.error(`Server close error: ${err}`)
            reject();
          }

          logger.info('Server stopped successfully.');
          resolve();
        });
      }
    });
  }
}

Then simply create your gateway service using the mug function:

import { mug } from 'moleculer-universal-gateway';

const broker = new ServiceBroker();

broker.createService(mathService);

const { createService, getApp } = mug(createApp, {
  dependencies: [logger', 'math'],
  // mixins: ...,
  // settings: ...,
  // methods: ...,
  // etc.
});

createService(broker);

// use `getApp` to retrieve the app instance if you wish to use it later:
// const app = getApp();
// app.use(someMiddleware);

return broker.start();

If you need access to the broker's internal logger, there are two strategies. You could either wrap it as a separate service, and pass it in as a dependency as above. Or you could simply wrap your createApp and provide it when you create your gateway:

export function withLogger(logger) {
  return function createApp() {
    const app = express();
    ...
  }
}

const broker = new ServiceBroker();

const { createService, getApp } = mug(withLogger(broker.logger));

createService(broker);