Skip to content

Latest commit

 

History

History

examples

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Examples

These examples show what is the minimal way to run certain functionality. Although real world cases are more complex, these examples illustrate some quick ways to implement the functionality and can help with prototyping some easy solutions.

Run a microservice, which listens on random HTTP port and exposes a single handler subject.object.predicate to other UT microservices running on the same host:

const dispatch = require('ut-function.dispatch');
require('ut-run').run({
    main: [{
        orchestrator: [
            dispatch({
                'subject.object.predicate': () => 'hello world'
            })
        ]
    }],
    config: {
        implementation: 'hello',
        utBus: {serviceBus: {jsonrpc: {domain: true}}}
    }
});

Check test.http for an example how to call the API.

Run unit tests for the exposed method:

const dispatch = require('ut-function.dispatch');
require('ut-run').run({
    main: dispatch({
        'subject.object.predicate': () => 'hello world'
    }),
    method: 'unit',
    config: {},
    params: {
        steps: [{
            method: 'subject.object.predicate',
            name: 'call subject.object.predicate',
            result: (result, assert) =>
                assert.equals(result, 'hello world', 'return hello world')
        }]
    }
});

Run a microservice, which listens on random HTTP port and easily expose any stateless function exported by any node module at top level. This is very quick way to create mocks or prototypes. All the exposed functions will be available in other microservices via importing them by name.

const dispatch = require('ut-function.dispatch');
const {promisify} = require('util');

const exec = async => (...params) => {
    const [moduleName, fn] = params.pop().method.split('.', 2);
    const mod = require(moduleName);
    const result = mod && mod[fn];
    return typeof result === 'function'
        ? (async ? promisify(result) : result).apply(mod, params)
        : result;
};

require('ut-run').run({
    main: [{
        orchestrator: [
            dispatch({
                'os.cpus': exec(),
                'process.resourceUsage': exec(),
                'dns.lookup': exec(true)
            })
        ]
    }],
    config: {
        implementation: 'api',
        utBus: {serviceBus: {jsonrpc: {domain: true}}}
    }
});

Check test.http for an example how to call the API.

Run a microservice, which listens on random HTTP port and exposes a single method subject.object.predicate to external consumers. The validation function defines the way the API is exposed:

const dispatch = require('ut-function.dispatch');

require('ut-run').run({
    main: [{
        orchestrator: [
            dispatch({
                'subject.object.predicate': () => 'hello world'
            })
        ],
        gateway: [
            function validation({joi}) {
                return {
                    'subject.object.predicate': () => ({
                        auth: false,
                        params: joi.object({}),
                        result: joi.string()
                    })
                };
            }
        ]
    }],
    config: {
        implementation: 'api',
        utBus: {serviceBus: {jsonrpc: {utLogin: false}}}
    }
});

Check test.http for an example how to call the API.

To see the API documentation, run it on a fixed port, and disable the integration with ut-login:

node api.js \
  --utBus.serviceBus.jsonrpc.utLogin=false \
  --utBus.serviceBus.jsonrpc.port=8090

Then open http://localhost:8090/api in a browser.

Run a microservice, which listens on random HTTP port and exposes a single method subject.object.predicate to external consumers through API gateway on port 8080. The module ut-gateway acts as a reverse proxy, which exposes the paths passed in the configuration apiGateway.api.

const dispatch = require('ut-function.dispatch');

require('ut-run').run({
    main: [{
        orchestrator: [
            dispatch({
                'subject.object.predicate': () => 'hello world'
            })
        ],
        gateway: [
            function validation({joi}) {
                return {
                    'subject.object.predicate': () => ({
                        auth: false,
                        params: joi.object({}),
                        result: joi.string()
                    })
                };
            },
            require('ut-gateway')({namespace: 'apiGateway'})
        ]
    }],
    config: {
        implementation: 'api',
        utBus: {serviceBus: {jsonrpc: {domain: true}}},
        apiGateway: {
            discover: true,
            api: [
                '/oauth2-redirect.html',
                '/api/{path*}',
                '/rpc/subject/{path*}'
            ]
        }
    }
});

Check test.http for an example how to call the API.

To see the API documentation, run it like this to disable the integration with ut-login:

node gateway.js --utBus.serviceBus.jsonrpc.utLogin=false