-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
92 lines (79 loc) · 2.29 KB
/
Copy pathindex.js
File metadata and controls
92 lines (79 loc) · 2.29 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
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const rateLimit = require("express-rate-limit");
const path = require("path");
const app = express();
const router = require("./routes/routes");
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Rate Limiter Configuration
const limiter = rateLimit({
windowMs: 30 * 60 * 1000, // 30 menit
max: 100, // batasi setiap IP hingga 100 permintaan per windowMs
message: "Too many requests from this IP, please try again after 30 minutes",
});
// Apply rate switching to all requests
app.use(limiter);
const port = 3000;
const swaggerSpec = require("./utils/swagger");
// Serve Swagger UI static files from node_modules
app.use(
"/swagger-ui",
express.static(path.join(__dirname, "node_modules/swagger-ui-dist"))
);
// Serve Swagger JSON
app.get("/docs/swagger.json", (req, res) => {
res.setHeader("Content-Type", "application/json");
res.setHeader("Access-Control-Allow-Origin", "*");
res.send(swaggerSpec);
});
app.get("/docs", (req, res) => {
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>API Public TESTING Documentation by Ojan</title>
<link rel="stylesheet" href="/swagger-ui/swagger-ui.css" />
<style>
body { margin: 0; padding: 0; }
.swagger-ui .topbar { display: none; }
</style>
</head>
<body>
<div id="swagger-ui"></div>
<script src="/swagger-ui/swagger-ui-bundle.js"></script>
<script src="/swagger-ui/swagger-ui-standalone-preset.js"></script>
<script>
window.onload = function() {
const ui = SwaggerUIBundle({
url: '/docs/swagger.json',
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
});
window.ui = ui;
};
</script>
</body>
</html>`;
res.send(html);
});
// Redirect root to docs
app.get("/", (req, res) => {
res.redirect("/docs");
});
app.use("/api", router);
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});