-
Notifications
You must be signed in to change notification settings - Fork 0
/
consul.ts
117 lines (111 loc) · 3.31 KB
/
consul.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { ConsulClient } from "./client.ts";
import { ConsulKV, ConsulKVArray } from "./kv.ts";
import { ServiceConfig, CatalogService } from "./service.ts";
export class Consul {
client: ConsulClient;
constructor(consulAddress: ConsulClient) {
this.client = consulAddress
this.client.address = consulAddress.proto + "://" + consulAddress.hostname + ":" + consulAddress.port;
}
async getMember() {
try {
const response = await fetch(this.client.address + "/v1/agent/members", {
method: "GET",
headers: {
"X-Consul-Token": this.client.Token,
},
});
if (response.body !== null) {
const body = new Uint8Array(await response.arrayBuffer());
var asString = new TextDecoder("utf-8").decode(body);
var asJSON = JSON.parse(asString);
console.log(asJSON[0].Name);
}
} catch (e) {
throw (e);
}
}
async putKey(data: ConsulKV): Promise<boolean> {
try {
const response = await fetch(this.client.address + "/v1/kv/" + data.Key, {
method: "PUT",
body: data.Value,
headers: {
"X-Consul-Token": this.client.Token,
},
});
if (response.body !== null) {
const body = new Uint8Array(await response.arrayBuffer());
var bodyString = new TextDecoder("utf-8").decode(body);
return JSON.parse(bodyString);
}
} catch (e) {
throw (e);
}
return false;
}
async getValue(data: string): Promise<string> {
try {
const response = await fetch(this.client.address + "/v1/kv/" + data, {
method: "GET",
headers: {
"X-Consul-Token": this.client.Token,
},
});
if (response.body !== null) {
const body = new Uint8Array(await response.arrayBuffer());
var bodyString = new TextDecoder("utf-8").decode(body);
let data: ConsulKVArray = JSON.parse(bodyString);
return atob(data[0].Value);
}
} catch (e) {
throw (e);
}
return "";
}
async registerService(service: ServiceConfig) {
try {
const response = await fetch(
this.client.address +
"/v1/agent/service/register?replace-existing-checks=true",
{
method: "PUT",
headers: {
"X-Consul-Token": this.client.Token,
},
body: JSON.stringify(service),
},
);
if (response.ok && response.body != null) {
const body = new Uint8Array(await response.arrayBuffer());
var bodyString = new TextDecoder("utf-8").decode(body);
console.log(bodyString);
}
console.log(JSON.stringify(service));
} catch (e) {
throw (e);
}
}
async getServiceCatalog(): Promise<CatalogService> {
let bodyjson: CatalogService = {};
try {
const response = await fetch(this.client.address + "/v1/catalog/services", {
method: "GET",
headers: {
"X-Consul-Token": this.client.Token,
},
});
if (response.ok) {
if (response.body != null) {
const body = new Uint8Array(await response.arrayBuffer());
var bodyString = new TextDecoder("utf-8").decode(body);
bodyjson = JSON.parse(bodyString);
return bodyjson;
}
}
} catch (e) {
throw (e);
}
return bodyjson;
}
}