0.1.9 • Published 1 year ago

@tinqjs/tinjs-boot v0.1.9

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

TIN JS BOOT PLUGIN

IMPORTANT!!! BREAKING CHANGES IS WORK IN PROGRESS

A complementary plugin for @tinqjs-cli to enable http in a service

Installation

npm install @tinqjs/tinjs-boot

Usage

Boot a service

// bootstrap service here
import main, {
  createHttpService,
  createHttpRoutes,
  createAmqpService,
  createAmqpSubs,
  amqpPublish,
} from "@tinqjs/tinqjs-boot";
import express from "express";

// @ts-ignore
import * as routes from "./routes/**/*"; // import express routes
// @ts-ignore
import * as mq from "./mq/**/*"; // import mq routes

function httpRoutes() {
  const HTTP_PREFIX_LEN = "./routes".length;
  const filenames = routes.filenames.map((name) =>
    name.substr(HTTP_PREFIX_LEN, name.length - HTTP_PREFIX_LEN - 3)
  );

  return createHttpRoutes({ ...routes, filenames });
}

async function amqpSubs(channel) {
  const HTTP_PREFIX_LEN = "./mq".length;
  const filenames = mq.filenames.map((name) =>
    name.substr(HTTP_PREFIX_LEN, name.length - HTTP_PREFIX_LEN - 3)
  );

  await createAmqpSubs(channel, { ...mq, filenames }, "topic_example");
}

main(async function (config) {
  const app = express();
  app.use(config.http.basePath, httpRoutes());

  const listen = createHttpService(app);

  listen(3001, () => {
    console.log(`Service greet ready`);
  });

  // create amqp listeners

  const channel = await createAmqpService(
    process.env.AMQP_HOST || "amqp://localhost/?heartbeat=45"
  );

  (async () => {
    for await (const c of channel()) {
      await amqpSubs(c);
    }
  })().catch((err) => console.log(err));

  /* setInterval(() => {
    amqpPublish("data.rana.10.user", "topic_example", "hello world");
  }, 10000); */
});

Examples

Checkout project https://github.com/bejorock/tinqjs-example for samples how to use it.

0.1.9

1 year ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.4

2 years ago

0.1.5

2 years ago

0.1.2

2 years ago