configuration/src/service-register.ts

34 lines
945 B
TypeScript

import { AppConfig } from "./app-config.model.js";
import { Etcd3 } from "etcd3";
import { hostname } from "os";
import { getEtcdInstance } from "./etcd-connection.js";
import debug from "debug";
export class ServiceRegister {
private etcd: Etcd3;
private readonly currHost = hostname();
private readonly hostKeyPrefix = "share/gateway/hosts";
private readonly logger = debug("fennec:config:register");
constructor(config?: AppConfig) {
this.etcd = getEtcdInstance(config);
}
async register(key: string, upstream: string) {
const fullKey = `${this.hostKeyPrefix}/${this.currHost}/${key}`;
this.logger(`Registering ${key} with upstream ${upstream}`);
this.logger(`full key: ${fullKey}`);
try {
await this.etcd.put(fullKey).value(upstream).exec();
this.logger(`registered!`);
} catch (error) {
this.logger(`register failed!`);
this.logger(error);
throw error;
}
}
}