pnxtech / hydra

A light-weight library for building distributed applications such as microservices

Home Page:https://www.hydramicroservice.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] Integrate with Awilix for DI

huyaloha opened this issue · comments

Hi @cjus,
I'm using Hydra for my project with Awilix. Everything work fine with Awilix and Awilix-express for REST API.
However, I'm facing a trouble to resolve objects when handle inter-service message. It's failed to resolve because there is nowhere to inject container when the hydra.on() get fired.

Do we have any ways to add inject container into (like app.use(middleware-to-inject-container)
const app = hydra.getExpressApp(); app.use(containerMiddleware);

  constructor (opts) {
    this.config = require('./config').HYDRA;
    this.umfHandler = umfHandler; // Resolve handler successfully
  }

  initialize () {
    return new Promise(function (resolve, reject) {
      hydra
        .init(config, function () {
          hydra.registerRoutes(routerConfig);
          hydra.getHydra().on('message', (message) => {
            this.umfHandler(message);  // Undefined
          });
        })
        .then(function () {
          resolve(hydra);
        })
        .catch(function (err) {
          reject(err);
        });
    });
  }
};

@huyaloha can you format your code above using three backticks ```
I'm not familiar with awilix - although I started looking at it after reading your post.
Are you using hydra-express?

Yes, I'm using hydra-express and have no issue with Routers. Only got issue with hydra.on(message)

The issue comes from my code itself. Close ticket.

Thanks