Configuration

By default, Service configuration is fetched from $PROJECT_ROOT/config/config.js by the service execution procedure.

General description of service configuration options is defined by the ajv validation schema.

    // $PROJECT_ROOT/index.js
    const Service = require('serviser');
    const config  = require('serviser-config');
    const service = new Service(config);

    service.config.get() //returns an object as exported by $PROJECT_ROOT/config/config.js
    service.config.get('storage:postgres'); //returns nested value of the postgres property of the configuration object

Each App of AppManager has its own configuration scope which is populated with service.config.get('apps:<app_name>') once on the App initialization.


   service.appManager.get('myapp').config.get() //returns value of service.config.get('apps:myapp')

Basic config.js example


const response = {
    headers: [
        ["Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE, CONNECT"]
    ]
};

const bodyParser = {
    json: {
        extended: true,
        type: 'application/json',
        limit: "2mb"
    },
    urlencoded: {
        type: 'application/x-www-form-urlencoded',
        limit: "2mb",
        extended: false
    }
};

module.exports = {
    exitOnInitError: true, //whether process should be terminated when an error occurs during service initialization
    apps: {
        myapp: {
            baseUrl: `http://127.0.0.1:${process.env.PUBLIC_PORT}`,
            listen: process.env.PUBLIC_PORT,
            stopOnError: false,
            doc: { //sub-app responsible for generating documentation for its parent app
                baseUrl: `http://127.0.0.1:${process.env.PUBLIC_DOC_PORT}`,
                listen: process.env.PUBLIC_DOC_PORT,
                name: 'docs',
                title: 'My App',
                stopOnError: true,
                tryItOut: true
            },
            response: response,
            bodyParser: bodyParser,
        }
    },
    storage: {
        postgres: {
            host: "127.0.0.1",
            ssl: false,
            databases: {
                main: {
                    db: "test",
                    username: "test",
                    password: "",
                }
            }
        }
    },
    logs: {
        exitOnError: false,  // determines whether a process will exit with status code 1 on 'uncaughtException' event
        transports: [
            {
                type: 'file',
                level: 'info', // maximum log level of this sepecific transport, [optional]
                json: false,
                priority: 1,
                dir: 'logs', // can be absolute or relative to the node's process
                autocreate: true // whether the `dir` should be created if it does not exist
            },
        ]
    }
}
comments powered by Disqus