-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (49 loc) · 1.49 KB
/
index.js
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
import express from "express";
import dotenv from "dotenv";
import axios from "axios";
import mainKeyRoutes from "./routes/main-keys.js";
import subaccountRoutes from "./routes/subaccount.js";
dotenv.config();
const app = express();
const port = process.env.VCR_PORT;
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use("/", mainKeyRoutes);
app.use("/", subaccountRoutes);
let region = process.env.REGION;
let modifiedRegion = region.replace(/^aws\./, "");
// Every 30 seconds a request is made to prevent inactivity hibernation of 1 minute, which causes cold start.
let interval = setInterval(() => {
axios
.get(
`http://${process.env.INSTANCE_SERVICE_NAME}.${modifiedRegion}.runtime.vonage.cloud/keep-alive`
)
.then((resp) => {
console.log(resp.data);
})
.catch((err) => console.log("VCR interval error:", err.code));
}, 30000);
app.get("/keep-alive", (req, res) => {
res.send(`/keep-alive`);
});
app.get("/_/health", (req, res) => {
res.sendStatus(200);
});
app.get("/_/metrics", (req, res) => {
res.sendStatus(200);
});
app.get("/", (req, res) => {
try {
res.status(200).send("VCR Proxy is running!");
} catch (error) {
console.error(`Error handling request: ${error.message}`);
res.status(500).send("Internal Server Error");
}
});
app.listen(port, (error) => {
if (error) {
console.error(`Error starting the server: ${error.message}`);
} else {
console.log(`App is now listening on port ${port}`);
}
});