2.1.0-beta.0 • Published 11 months ago

@rugo-vn/service v2.1.0-beta.0

Weekly downloads
-
License
MIT
Repository
-
Last release
11 months ago

Rugo Service

Base for microservice system - Unit to build Rugo Platform.

Concept

  • system is an entire program, which run by node command.
  • The system is devided into units, called service.
  • service identity is name. It's a unique value.
  • Super service to create, load and start other services, callced broker.
  • Services were created by same broker, called scope.
  • In scope, every services share a same variable called globals. You can access globals anywhere from the service through this.globals.
  • In scope, every services can be execute functions from another service by call method, which can access anywhere in the service through this.call.
  • In service, you can bind functions to this, these functions called method.
  • this.call will call a function, called action. The call must have a address of the action you want to execute by format <name>.<action>.
  • You can bind functions before, after and when error occur by hooks.

Service Structure

const serviceDefine = {
  name: /* ... */,
  settings: {
    /* ... */
  },
  methods: {
    async methodName(/* ... */) {
      /* ... */
    },

    /* ... */
  },
  actions: {
    async actionName(/* ... */) {
      /* ... */
    },

    /* ... */
  },
  hooks: {
    before: {
      async all(/* ... */) {
        /* ... */
      },

      /* ... */
    },

    after: {
      async all(/* ... */) {
        /* ... */
      },

      /* ... */
    },

    error: {
      async all(/* ... */) {
        /* ... */
      },

      /* ... */
    }
  },

  async started(/* ... */) {
    /* ... */
  },

  async closed(/* ... */) {
    /* ... */
  },
}

Usage

const settings = {
  _services: [
    '/path/to/service',
    /* ... */
  ],
  _globals: {
    /* global variables */
  }
}

const broker = createBroker(settings); 

const service = broker.createService(serviceDefine);

await broker.loadServices();

await broker.start();
await broker.close();

License

MIT.