2020-07-07 11:03:36 +02:00
|
|
|
const winston = require('winston');
|
|
|
|
const setIndent = require('./setindent');
|
2020-07-03 07:07:40 +02:00
|
|
|
|
|
|
|
|
2020-07-07 11:03:36 +02:00
|
|
|
const config = {
|
|
|
|
/*
|
2020-07-03 07:07:40 +02:00
|
|
|
|
2020-07-07 11:03:36 +02:00
|
|
|
Consul agent configuration
|
|
|
|
Determines the agent or server which will be queried and
|
|
|
|
monitored for the service catalog.
|
2020-07-03 07:07:40 +02:00
|
|
|
|
2020-07-07 11:03:36 +02:00
|
|
|
Recommended to be a local agent connected to the cluster.
|
2020-07-03 07:07:40 +02:00
|
|
|
|
2020-07-07 11:03:36 +02:00
|
|
|
Passed to the initialization of the Node Consul client.
|
|
|
|
For all options, see: https://github.com/silas/node-consul#init
|
2020-07-03 07:07:40 +02:00
|
|
|
|
2020-07-07 11:03:36 +02:00
|
|
|
*/
|
|
|
|
consul: {
|
|
|
|
host: 'pc-mvrenswoude'
|
|
|
|
},
|
2020-07-03 07:07:40 +02:00
|
|
|
|
|
|
|
|
2020-07-07 11:03:36 +02:00
|
|
|
/*
|
2020-07-03 07:07:40 +02:00
|
|
|
|
2020-07-07 11:03:36 +02:00
|
|
|
Logging
|
|
|
|
See: https://github.com/winstonjs/winston#transports
|
2020-07-03 07:07:40 +02:00
|
|
|
|
2020-07-07 11:03:36 +02:00
|
|
|
*/
|
|
|
|
logging: {
|
|
|
|
transports: [
|
|
|
|
new winston.transports.Console({
|
|
|
|
level: 'debug',
|
|
|
|
timestamp: true,
|
|
|
|
format: winston.format.combine(
|
|
|
|
winston.format.colorize(),
|
|
|
|
winston.format.simple()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
]
|
|
|
|
},
|
2020-07-03 07:07:40 +02:00
|
|
|
|
2020-07-07 11:03:36 +02:00
|
|
|
onUpdate: [],
|
|
|
|
afterUpdate: null
|
2020-07-03 07:07:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-07-07 11:03:36 +02:00
|
|
|
|
2020-07-03 07:07:40 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
onUpdate handlers
|
|
|
|
When a change occurs in the Consul catalog, each handler is called in order.
|
|
|
|
Callbacks may return a Promise. Note that when using multiple handlers,
|
|
|
|
they are not awaited immediately but at the end using Promise.all(), which
|
|
|
|
means the handlers effectively run in parallel.
|
|
|
|
|
|
|
|
The catalog parameter has a services property to enumerate the registered
|
|
|
|
services. See the Readme for the documentation of the ConsulCatalog class.
|
|
|
|
|
|
|
|
For more information about a service, including it's address and health,
|
|
|
|
an additional call to Consul is required. Calling these methods will result
|
|
|
|
in the service being watched for changes as well.
|
|
|
|
|
|
|
|
The second parameter is a reference to the Winston logger instance.
|
|
|
|
|
|
|
|
*/
|
|
|
|
const fs = require('fs').promises;
|
|
|
|
|
|
|
|
|
2020-07-07 11:03:36 +02:00
|
|
|
config.onUpdate.push(async (catalog, logger) =>
|
2020-07-03 07:07:40 +02:00
|
|
|
{
|
|
|
|
// Use catalog parameter to generate output
|
|
|
|
let output = '';
|
|
|
|
|
|
|
|
for (const service of catalog.services)
|
|
|
|
{
|
2020-07-07 11:03:36 +02:00
|
|
|
let instances = '';
|
|
|
|
|
|
|
|
for (const instance of await service.getInstances())
|
|
|
|
{
|
|
|
|
instances += setIndent(2, `
|
|
|
|
Address: ${instance.address}
|
|
|
|
Port: ${instance.port}
|
2020-07-03 07:07:40 +02:00
|
|
|
|
2020-07-07 11:03:36 +02:00
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
|
|
|
output += setIndent(`
|
|
|
|
Service: ${service.name}
|
|
|
|
Tags: ${JSON.stringify(service.tags)}
|
|
|
|
|
|
|
|
`);
|
|
|
|
|
|
|
|
output += instances + '\n';
|
2020-07-03 07:07:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
await fs.writeFile('example-output.txt', output);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2020-07-07 11:03:36 +02:00
|
|
|
// Example on how to split the update handlers into a separate file
|
|
|
|
config.onUpdate.push(require('./included.example'));
|
|
|
|
|
|
|
|
|
2020-07-03 07:07:40 +02:00
|
|
|
/*
|
|
|
|
afterUpdate handler
|
|
|
|
This is a single handler which is called after all the onUpdate handlers
|
|
|
|
have finished, including any Promises returned.
|
|
|
|
|
|
|
|
It can be used for example to reload a proxy server after the configuration
|
|
|
|
changes have been written in onUpdate.
|
|
|
|
*/
|
|
|
|
|
|
|
|
config.afterUpdate = (catalog, logger) =>
|
|
|
|
{
|
|
|
|
// Call a reload script
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = config;
|