-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
51 lines (43 loc) · 1.54 KB
/
app.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
const express = require("express");
const cors = require("cors");
const swaggerUi = require("swagger-ui-express");
const swaggerDocument = require("./openapi.json");
const serviceRoute = require("./routes/v1/serviceRoute");
const subscribeRoute = require("./routes/v1/subscribeRoute");
const unsubscribeRoute = require("./routes/v1/unsubscribeRoute");
const RequestLoop = require("./domain/requestQueue");
const { getAllServices, updateServiceStatus } = require("./db/index");
getAllServices()
.then((allServices) => {
const mainLoop = new RequestLoop(allServices.rows, updateServiceStatus);
mainLoop.start();
const app = express();
app.use(cors());
app.use(express.json());
app.use("/api/v1/services", serviceRoute(mainLoop));
app.use("/api/v1/subscribe", subscribeRoute);
app.use("/api/v1/unsubscribe", unsubscribeRoute);
app.use("/api/v1", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use("/", express.static("home"));
app.use((req, res, next) =>
res.status(404).json({
status: "error",
error: "the requested resource was not found",
})
);
app.use((err, req, res, next) =>
res.status(500).json({
status: "error",
error:
process.NODE_ENV === "production"
? "an error occured processing your request"
: err,
})
);
const PORT = process.env.PORT || 3100;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
})
.catch((err) => console.log(err));
//module.exports = app;