-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
114 lines (105 loc) · 3.14 KB
/
server.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
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
104
105
106
107
108
109
110
111
112
113
114
import dotenv from "dotenv";
import express from "express";
import mongoose from "mongoose";
import morgan from "morgan";
import cors from "cors";
import compression from "compression";
import helmet from "helmet";
import usersRoute from "./routes/Users.js";
import organizationsRoute from "./routes/Organizations.js";
import contributionsRoute from "./routes/Contributions.js";
import healthCheckRoute from "./routes/Health.js";
import swaggerUI from "swagger-ui-express";
import swaggerJsDoc from "swagger-jsdoc";
dotenv.config({
path: "./config.env",
});
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(helmet.hidePoweredBy());
app.use(compression());
app.use(cors());
const port = process.env.PORT;
if (process.env.NODE_ENV !== "production") {
const options = {
definition: {
openapi: "3.0.0",
info: {
title: "JOSA Top Contributors API",
version: "1.0.0",
description: "Express based APIs for the JOSA Top Contributors.",
},
servers: [{ url: `http://${process.env.HOST}:${process.env.PORT}` }],
},
apis: ["./routes/*.js"],
};
const swaggerSpecs = swaggerJsDoc(options);
app.use("/v1/docs", swaggerUI.serve, swaggerUI.setup(swaggerSpecs));
}
if (process.env.LOGGING === "dev") {
app.use(morgan("combined"));
} else if (process.env.LOGGING === "tiny") {
app.use(morgan("tiny"));
}
const ConnectToDB = async () => {
let DB_URL =
"mongodb://" +
process.env.DATABASE_HOST +
":" +
process.env.DATABASE_PORT +
"/" +
process.env.DATABASE_NAME;
if (process.env.NODE_ENV !== "dev") {
// DB_URL
// mongodb://username:password@host:port/database
DB_URL =
"mongodb+srv://" +
process.env.DATABASE_USER +
":" +
process.env.DATABASE_PASSWORD +
"@" +
process.env.DATABASE_HOST +
"/" +
process.env.DATABASE_NAME +
"?authSource=admin&tls=" +
process.env.TLS_ENABLED +
"&tlsCAFile=" +
process.env.CA_PATH +
"";
}
await mongoose.connect(DB_URL);
console.info("Connected to the database");
console.info(
`Database Host: ${mongoose.connection.host}\nDatabase Port: ${mongoose.connection.port}\nDatabase Name: ${mongoose.connection.name}`
);
};
app.use("/v1", usersRoute);
app.use("/v1", organizationsRoute);
app.use("/v1", contributionsRoute);
app.use("/v1", healthCheckRoute);
app.get("*", (req, res) => {
let message =
"Resource not found, please go to /v1/docs to see all the available routes and resources.";
if (process.env.NODE_ENV === "production") {
message = "Resource not found, check your connection and try again.";
}
res.status(404).json({
message: message,
});
});
app.listen(port, async () => {
console.log(`Express server listening on port: ${port}`);
await ConnectToDB();
});
// Listen for the signal that there is an uncaught exception
process.on("uncaughtException", async err => {
console.error(
`There is an error server can't continue running, "THE ERROR": ${err}`
);
mongoose.disconnect(() => {
console.log("Database disconnected");
});
console.log("server is closed");
process.exit(1);
});