-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
71 lines (61 loc) · 1.76 KB
/
Copy pathapp.js
File metadata and controls
71 lines (61 loc) · 1.76 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
import express from "express";
import { PORT } from "./config/env.js";
import errorMiddleware from "./middlewares/error.middleware.js";
import emailRouter from "./routes/email.route.js";
import swaggerUi from "swagger-ui-express";
import { swaggerSpec } from "./docs/swagger.js";
import { renderSwaggerUiPage } from "./docs/swaggerUiPage.js";
const app = express();
const isDevelopment = process.env.NODE_ENV === "development";
const DOCS_PATH = "/api/v1/docs";
const DOCS_SPEC_PATH = `${DOCS_PATH}/swagger.json`;
app.set("trust proxy", true);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.get("/", (req, res) => {
return res.redirect(`${DOCS_PATH}/`);
});
app.get(DOCS_SPEC_PATH, (req, res) => {
const forwardedProto = req.headers["x-forwarded-proto"];
const protocol =
typeof forwardedProto === "string"
? forwardedProto.split(",")[0].trim()
: req.protocol;
const host = req.headers["x-forwarded-host"] || req.get("host");
const origin = `${protocol}://${host}`;
return res.status(200).json({
...swaggerSpec,
servers: [
{
url: origin,
description: "Current server",
},
],
});
});
if (isDevelopment) {
app.use(
DOCS_PATH,
swaggerUi.serve,
swaggerUi.setup(undefined, {
swaggerOptions: {
url: DOCS_SPEC_PATH,
validatorUrl: null,
},
explorer: true,
}),
);
} else {
app.get(`${DOCS_PATH}/`, (req, res) => {
res.setHeader("Content-Type", "text/html; charset=utf-8");
return res.send(renderSwaggerUiPage(DOCS_SPEC_PATH));
});
}
app.use("/api/v1", emailRouter);
app.use(errorMiddleware);
if (isDevelopment) {
app.listen(PORT, async () => {
console.log(`App listening on http://localhost:${PORT}`);
});
}
export default app;