const Consul = require('consul')
const { config } = require('../config')
class Discovery {
constructor() {
this.consul = new Consul(config.consul)
}
async getValue(key) {
return await this.consul.kv.get(key)
}
async setValue(key, value) {
return await this.consul.kv.set(key, value)
}
async parseValue(key) {
const info = await this.consul.kv.get(key)
return JSON.parse(info.Value)
}
async findService() {
for (const name of config.services.names) {
const info = await this.consul.catalog.service.nodes(name)
// console.log(info)
if (info.length > 0) {
this.setValue(name, JSON.stringify(info[0]))
}
}
}
static create() {
return new Discovery()
}
}
module.exports = Discovery.create()