-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
103 lines (90 loc) · 2.91 KB
/
index.js
File metadata and controls
103 lines (90 loc) · 2.91 KB
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
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const favicon = require("serve-favicon");
const path = require("path");
const colors = require("colors");
const dotenv = require("dotenv").config();
const cron = require("node-cron");
const axios = require("axios");
const { connectDB, disconnectDB } = require("./config/db");
const { swaggerServe, swaggerSetup } = require("./config/swaggerConfig");
// Define base URLs for both localhost and hosted server
const PORT = process.env.PORT || 5000;
// create our express app
const app = express();
// middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Enable CORS for all routes
app.use(cors());
// Serve favicon
app.use(favicon(path.join(__dirname, "assets", "favicon.ico")));
// Function to make a connection to database and disconnect
const OpenAndCloseConnection = async () => {
try {
await connectDB();
console.log("--------------------------------------------");
console.log("28 DAYS OVER!");
console.log(
"Making connection to the database to keep the cluster running!"
);
console.log("-------------------------------------------- \n \n");
setTimeout(async () => {
await disconnectDB();
console.log("--------------------------------------------");
console.log("20 Seconds Over!");
console.log("Disconnecting the connection from the database!");
console.log("-------------------------------------------- \n \n");
}, 20000); // Disconnect after 20 seconds
} catch (error) {
console.error("Error:", error);
}
};
// Schedule the function to run every 28 days
cron.schedule(
"0 0 1 * *",
async () => {
// Runs at midnight on the 1st of every month
await OpenAndCloseConnection();
},
{
scheduled: true,
timezone: "Asia/Kolkata",
}
);
// Schedule a job to make a request to the server every 5 minutes
const KeepActivityConnection =
"https://fakeauthentication-api.onrender.com/api";
cron.schedule(
"*/9 * * * *",
async () => {
try {
// Make a request to the root endpoint to keep the server active
const response = await axios.get(`${KeepActivityConnection}`);
console.log(
`\nRequest to server made to keep it active. \nResponse:${response.data}\n`
);
} catch (error) {
console.error("Error making request:", error.message);
}
},
{
scheduled: true,
timezone: "Asia/Kolkata",
}
);
// Route to serve the website
app.use(express.static(path.join(__dirname, "website")));
// route to api
app.get("/api", (req, res) => {
res.send("Welcome to the Fake Authentication API!");
});
const routes = require("./Routes/Routes");
app.use("/api", routes);
//Swagger serve route
app.use("/api/api-docs", swaggerServe, swaggerSetup);
// start the server
app.listen(PORT, () => {
console.log(`Fake Authentication API server is running on port: ${PORT}`);
});