0.1.5 • Published 3 years ago

modulux v0.1.5

Weekly downloads
49
License
MIT
Repository
-
Last release
3 years ago

Modulux

Modulux development is suspended : I discovered a powerful framework to create microservices : Moleculer

What is Modulux ?

Modulux is a module oriented backend for NodeJS applications.

Features

Modules

Structure your application with many modules as needed.

Modulux automatically discover your modules and start/stop them according to their dependencies.

Each module is executed in isolated process (fork) to improve security and performances.

Modules can communicate with eath others via IPC.

Settings

Modulux provides file based mecanism to store application settings.

Settings can be automatically reloaded when file modified.

It's also possible to define globals and module specific settings.

Getting started

First, install Modulux :

npm install --save modulux

Now, create application bootstrap :

/index.js:

require('modulux').bootstrap(); // Run application

If no application configuration defined, Modulux will search modules in modules/*.js.

You can define application configuration to set some paths and change some behaviours :

require('modulux').bootstrap({
    modules: {
        path: 'my-modules/*.js' // Search modules in this path
    }
});

See below for more details about application configuration.

Now you can create your modules and store them in /modules/ dir.

This is a basic module template :

/modules/my-module.js

const Module = require('modulux').Module;

class MyModule extends Module { // Modules must extends Modulux.Module to get discovered

    static getConfiguration(){
        // Module static configuration
        return {
            id: 'mymodule' // Module id
        };
    }

    async onStart(){
        // Called when module start
    }

    async onStop(){
        // Called when module stop
    }

    async myCustomMethod(){
        // A custom method that can be called from others modules
    }

}

module.exports = MyModule;

If you need to store settings, use template below.

settings.json:

{
    "modules": {
        "module1": {
            "myModuleSetting": "OK"
        },
        "module2": {
            "myModuleSetting": {
                "": ""
            }
        }
    },

    "global": {
        "myGlobalSetting": "OK"
    }
}

Start the application :

node index.js

Application configuration

{
    // Package file of your application (optional)
    packageJson: 'package.json',

    // Settings related (optional)
    settings: {
        file: 'settings.json',  // Setting file name
        autoreload: false       // Enable auto-reload when file modified ?
    },

    // Module related (optional)
    modules: {
        path: 'modules/*.js', // Path where modules are stored
        startTimeout: 10000,    // Timeout (ms) when starting modules
        stopTimeout: 10000      // Timeout (ms) when stopping modules
    }
}

Module methods

class Module{

    /**
     * This method is called by Modulux to start the module
     */
    async onStart()
    
    /**
     * This method is called by Modulux to stop the module
     */
    async onStop()

    
    /**
     * Send a request to target module and wait for response.
     * @param {string} target Target module id
     * @param {string} method Target module method to call
     * @param {*} data  Data to pass to the method
     * @returns {*} response
     */
    async sendRequest(target, method, data=null)

    /**
     * Send a message to target module (do not wait for a response).
     * @param {string} target Target module id
     * @param {string} method Target module method to call
     * @param {*} data Data to pass to the method
     */
    async sendMessage(target, method, data=null)


    /**
     * Log info
    * @param {...*} arguments Data to log (like console.log)
    */
    async log()

    /**
     * Log warning
    * @param {...*} arguments Data to log (like console.warn)
    */
    async warn()

    /**
     * Log error
    * @param {...*} arguments Data to log (like console.error)
    */
    async error()


    /**
     * Stop application
     */
    stopApplication()
    
    /**
     * Get module setting by name
     * @param {string} name 
     * @returns {*} Module setting value
     */
    getSetting(name)

    /**
     * Get application global setting by name
     * @param {string} name
     * @returns {*} Global setting value
     */
    getGlobalSetting(name)

}

Changelog

v0.1.4 - March 26, 2021

  • Fix on module discovering
  • Various timeout tuning
  • Startup less verbose
  • Documentation update

v0.1.3 - February 26, 2021

  • New module method : stopApplication()
  • modules.path app config is now optional (default: modules/*.js)
  • packageJson app config is now optional (default: package.json)
  • Documentation update
  • Code & file cleanup

v0.1.2 - February 19, 2021

  • Initial version
0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago