-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbe_details.ts
82 lines (69 loc) · 1.65 KB
/
be_details.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
import axios from 'axios';
import { BEServerHealth } from './enum';
export interface IBackendServerDetails {
url: string;
count: number;
/**
* Reset the number of requests sent to this server
*/
resetCount(): void;
/**
* Performs a simple GET operation on the PING URL.
* Expected Status code is 200 if Backend Server is live.
*
* @public
* @async
* @returns {Promise<number>} - Status code of the PING request
*/
ping(): Promise<number>;
setStatus(status: BEServerHealth): void;
getStatus(): BEServerHealth;
/**
* Increments the total requests sent to this server
*
* @public
* @returns {number} - The number of requests sent after increment
*/
incrementCount(): number;
}
export class BackendServerDetails implements IBackendServerDetails {
url;
private status: BEServerHealth;
private pingUrl;
private controller;
count;
constructor(
url: string,
controller: AbortController,
status?: BEServerHealth
) {
this.url = url;
this.count = 0;
this.controller = controller;
this.pingUrl = url + 'ping';
this.status = status ?? BEServerHealth.UNHEALTHY;
}
public setStatus(status: BEServerHealth): void {
this.status = status;
}
public getStatus(): BEServerHealth {
return this.status;
}
public async ping(): Promise<number> {
try {
const response = await axios.get(this.pingUrl, {
signal: this.controller.signal
});
return response.status;
} catch (err) {
return 500;
}
}
public resetCount() {
this.count = 0;
}
public incrementCount(): number {
this.count++;
return this.count;
}
}