feat: register service.
This commit is contained in:
5
src/app-config.model.ts
Normal file
5
src/app-config.model.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { IOptions } from "etcd3";
|
||||
|
||||
export class AppConfig {
|
||||
etcd!: Pick<IOptions, "auth" | "hosts">;
|
||||
}
|
29
src/config-loader.ts
Normal file
29
src/config-loader.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { AppConfig } from "./app-config.model";
|
||||
import { debug } from "console";
|
||||
import { Etcd3 } from "etcd3";
|
||||
import { load } from "js-yaml";
|
||||
import { hostname } from "os";
|
||||
import { getEtcdInstance } from "./etcd-connection";
|
||||
|
||||
class ConfigLoader {
|
||||
private etcd: Etcd3;
|
||||
|
||||
private readonly currHost = hostname();
|
||||
private readonly configKeyPrefix = "share/config";
|
||||
|
||||
constructor(config: AppConfig) {
|
||||
this.etcd = getEtcdInstance(config);
|
||||
}
|
||||
|
||||
async loadConfig(configKey: string): Promise<any> {
|
||||
const str = await this.etcd
|
||||
.get(`${this.configKeyPrefix}/${this.currHost}/${configKey}`)
|
||||
.string();
|
||||
|
||||
if (!str) {
|
||||
debug("config-loader", `No config found for ${configKey}`);
|
||||
return null;
|
||||
}
|
||||
return load(str);
|
||||
}
|
||||
}
|
16
src/etcd-connection.ts
Normal file
16
src/etcd-connection.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { Etcd3 } from "etcd3";
|
||||
import { AppConfig } from "./app-config.model";
|
||||
function connectEtcd(config: AppConfig) {
|
||||
return new Etcd3({
|
||||
...config.etcd,
|
||||
});
|
||||
}
|
||||
|
||||
let instance: Etcd3;
|
||||
|
||||
export function getEtcdInstance(config: AppConfig) {
|
||||
if (!instance) {
|
||||
instance = connectEtcd(config);
|
||||
}
|
||||
return instance;
|
||||
}
|
4
src/index.ts
Normal file
4
src/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export * from "./app-config.model";
|
||||
export * from "./config-loader";
|
||||
export * from "./etcd-connection";
|
||||
export * from "./service-register";
|
33
src/service-register.ts
Normal file
33
src/service-register.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { AppConfig } from "./app-config.model";
|
||||
import { Etcd3 } from "etcd3";
|
||||
import { hostname } from "os";
|
||||
import { getEtcdInstance } from "./etcd-connection";
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user