-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
103 lines (92 loc) · 2.96 KB
/
server.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
import express, { Request, Response, NextFunction } from "express";
import axios from "axios";
import { Server } from "http";
const app = express();
const PORT = 3002;
const HOST = "localhost";
app.use(express.json());
app.get("/fakeapi", (req: Request, res: Response, next: NextFunction) => {
console.log(`hello from fakeapi`);
res.send("Hello from fakeapi");
});
app.post("/bogusapi", (req: Request, res: Response, next: NextFunction) => {
console.log(`hello from bogusapi`);
res.send("Hello from bogusapi");
});
let server: Server;
app.listen(PORT, () => {
const authString = "surajdarade:surajdarade";
const encodedAuthString = Buffer.from(authString, "utf-8").toString("base64");
console.log(`Encoding authstring: ${encodedAuthString}`);
console.log("Fake API server started on port " + PORT);
registerService(encodedAuthString);
});
function registerService(encodedAuthString: string) {
console.log("Attempting to register service with the gateway...");
axios({
method: "POST",
url: "http://localhost:3000/register",
headers: {
authorization: `Basic ${encodedAuthString}`,
"Content-Type": "application/json",
},
data: {
apiName: "testapi",
protocol: "http",
host: HOST,
port: PORT,
connections: 0,
weight: 1,
healthCheckPaths: ["/fakeapi", "/bogusapi"],
},
})
.then((response) => {
console.log("Service registered successfully:", response.data);
})
.catch((err) => {
console.error("Error during service registration:", err.message);
if (err.response) {
console.error("Response data:", err.response.data);
console.error("Response status:", err.response.status);
console.error("Response headers:", err.response.headers);
}
});
}
process.on("SIGINT", () => {
console.log("Received SIGINT. Unregistering service...");
unregisterService().then(() => {
process.exit();
});
});
async function unregisterService() {
const authString = "johndoe:password";
const encodedAuthString = Buffer.from(authString, "utf-8").toString("base64");
console.log("Attempting to unregister service from the gateway...");
try {
const response = await axios({
method: "POST",
url: "http://localhost:3000/unregister",
headers: {
authorization: `Basic ${encodedAuthString}`,
"Content-Type": "application/json",
},
data: {
apiName: "testapi",
protocol: "http",
host: HOST,
port: PORT,
connections: 0,
weight: 1,
healthCheckPaths: ["/fakeapi", "/bogusapi"],
},
});
console.log("Service unregistered successfully:", response.data);
} catch (err: any) {
console.error("Error during service unregistration:", err.message);
if (err.response) {
console.error("Response data:", err.response.data);
console.error("Response status:", err.response.status);
console.error("Response headers:", err.response.headers);
}
}
}