-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
116 lines (93 loc) · 3.24 KB
/
index.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
115
116
const dotenv = require('dotenv');
const multer = require('multer');
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
const userRoutes = require('./routes/userRoutes');
const loginRoutes = require('./routes/loginRoutes');
const errorHandler = require('./middleware/errorHandler');
const swaggerJsDoc = require('swagger-jsdoc');
const { swaggerUi, swaggerSpec } = require('./config/swagger'); // Import Swagger UI
const metricsRoutes = require('./routes/metricsRoutes');
const { trackRequests } = require('./controllers/metricsController');
dotenv.config();
const app = express();
const port = process.env.PORT || 3000;
app.use(express.json());
// Middleware
app.use(helmet());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(trackRequests);
// Rate Limiting
const globalLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
message: 'Too many requests, please try again later.',
headers: true, // Sends the rate limit info in the response headers
});
app.use(globalLimiter);
const cors = require('cors');
app.use(cors({
origin: 'http://localhost:3000/api-docs',
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization'], // Make sure Authorization is allowed
}));
// Serve Swagger documentation at the /api-docs route
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
// Routes
app.use('/users', userRoutes);
// Use login routes
app.use('/', loginRoutes);
// Use metrics routes
app.use('/metrics', metricsRoutes);
// Error Handler
app.use(errorHandler);
const loginLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 5, // limit each IP to 5 login requests per windowMs
message: 'Too many login attempts, please try again after 15 minutes',
});
app.use('/login', loginLimiter);
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/');
},
filename: (req, file, cb) => {
cb(null, Date.now() + path.extname(file.originalname));
},
});
const upload = multer({ storage });
app.post('/users/:id/profile-picture', upload.single('profilePicture'), (req, res) => {
const id = parseInt(req.params.id);
const picturePath = req.file.path;
pool.query('UPDATE users SET profile_picture = $1 WHERE id = $2', [picturePath, id], (error) => {
if (error) {
throw error;
}
res.status(200).send(`Profile picture updated for user ID: ${id}`);
});
});
const swaggerOptions = {
swaggerDefinition: {
openapi: '3.0.0',
info: {
title: 'User API',
version: '1.0.0',
description: 'User management API',
},
},
apis: ['./routes/*.js'], // path where API docs are located
};
const swaggerDocs = swaggerJsDoc(swaggerOptions);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs));
// Conditionally start the server only if this file is the entry point
if (require.main === module) {
app.listen(port, () => {
console.log(`App running on port ${port}.`);
});
}
module.exports = app;