app-kit
Application resource loader library
Main features:
- load application modules by named configuration
- split modules to service -> api -> env architecture
- service accept requests and call api
- api is business logic only
- env used in api for doing what you need
- pluggable architecture
Installation
- We recommend using yarn insted of npm.
yarn add @akilio/app-kit
- Create index.js file with next content:
require('@akilio/app-kit').autostart();
- Add this to your package.json scripts section:
"scripts": {
"start": "node index.js",
"cli": "node index.js --cli",
},
- Create api/index.js file with exported functions^
function example(env, message) {
console.log(`Your message is "${message}"`);
}
module.exports = {
example
};
- Create config/index.js file with content:
module.exports = {
service: {
cli: {}
},
env: {},
api: {
example: { cli: {}}
}
};
Your are ready to run! Call function with next command:
yarn cli example "Hello!"
Building blocks
Service
This type of modules needed for interacting with external services, accept requests, keep connections and etc. For example this can be http-server, database or queue connection manager.
Service modules placed in service directory of your app or can be installed as npm module with names started with app-kit-service-.
Base service module (service/service_test.js) looked like this:
module.exports = async function (config, api, call) {
for (let apiName in api) {
setInterval(() => call(apiName, ...api[apiName].args), api[apiName].interval);
}
return {
state: {},
env: {},
startup: () => {},
shutdown: () => {}
};
};
Env
This is needed for excluding from api code worked with external enviroinment. For example FileSystem, web-service api, mailing, image conversion and etc.
Env modules placed in env directory of your app or can be installed as npm module with names started with app-kit-env-.
Base env module (env/env_test.js) looked like this:
module.exports = async function (config) {
return {
test: () => {}
};
};
Api
This is your business logic only code, do not place this anything like remote service calls, data transformations and other staff. Keep it clean and you will get highly reusable code.
API modules placed in api directory of your app or can be installed as npm module with names started with app-kit-api-.
Base env module looked like this:
module.exports = {
example: function (env, ...other) {}
exampleAsync: async function (env, a, b) {
env.env_test.test();
env.cli.log(`exampleAsync called with ${a} and ${b}`);
return a + b;
}
};
Config
With config you can control which modules loaded and config for them.
You have 3 sections in config file: service, env, api.
Example config:
module.exports = {
service: {
service_test: {
configVar1: "<anything>"
}
},
env: {
env_test: {
configVar2: "<anything>"
}
},
api: {
example: {
cli: {}
},
exampleAsync: {
cli: {},
service_test: {
interval: 3000,
args: [1,2]
}
},
other_module: {
other_function: {}
}
}
};
You can rename loaded modules, for example in index.js config you have this:
module.exports = {
service: {
"db:mongodb": {}
}
};
And in local.js config thix:
module.exports = {
service: {
"db:tingo": {}
}
};
In api function code you will call function as env.db.find(...), but when you start with yarn start your code will work with mongodb, but when you start with yarn start local you will work with tingodb fs database.