-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
78 lines (66 loc) · 2.1 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
import cors from "cors";
import express from "express";
import fs from "fs";
import http from "http";
import https from "https";
import { secondsToMs } from "./database/db.config";
import checkDB from "./database/dbChecker";
import projectsDB from "./routes/projectsDB";
import projectsLocal from "./routes/projectsLocal";
import logger from "./utils/logger";
// const privateKey = fs.readFileSync("/etc/letsencrypt/live/test.byteccny.com/privkey.pem", "utf8");
// const certificate = fs.readFileSync("/etc/letsencrypt/live/test.byteccny.com/cert.pem", "utf8");
// const credentials = { key: privateKey, cert: certificate };
const INTERVAL: number = secondsToMs(60 * 60);
const app = express();
let dbAval: boolean = true;
// initial check
(async () => {
try {
dbAval = await checkDB();
logger.info(`Server is up, database is ${dbAval ? "up" : "not up"}`);
} catch (e: any) {
dbAval = false;
}
})();
// routine
setInterval(async () => {
try {
dbAval = await checkDB();
} catch (e: any) {
console.error("Error:", e.message);
dbAval = false;
}
logger.info(`Database is ${dbAval ? "available" : "not available"}`);
}, INTERVAL);
app.use(cors());
app.use((req: any, res: any, next: any) => {
logger.info(`Received a ${req.method} request for ${req.url}`);
next();
});
app.use("/projects", (req: any, res: any, next: any) => {
if (dbAval) {
projectsDB(req, res, next);
} else {
projectsLocal(req, res, next);
}
});
app.get("/", (req: any, res: any) => {
res.send(
`BYTE @ CCNY. The database is ${dbAval ? "available" : "not available"}`,
).status(200);
});
// any other route will return a 404
app.get("*", (req: any, res: any) => {
res.status(404).json({
message:
"Page not found. Invalid path or method provided to make this request.",
});
});
app.listen(3000, () => {
console.log("Server up and running on port 3000");
});
// const httpServer = http.createServer(app);
// const httpsServer = https.createServer(credentials, app);
// httpServer.listen(80);
// httpsServer.listen(443);