-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
82 lines (73 loc) · 2.47 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
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
const express = require('express');
const mongoose = require('mongoose');
const swaggerUi = require('swagger-ui-express');
const swaggerJSDoc = require('swagger-jsdoc');
const authRoutes = require('./src/routes/authRoutes');
const doubtroomRoutes = require('./src/routes/doubtroomRoutes');
const mentorRoutes = require('./src/routes/mentorRoutes');
const studentRoutes = require('./src/routes/studentRoutes');
const searchRoutes = require('./src/routes/searchRoutes');
const cookieParser = require('cookie-parser');
require('dotenv').config();
const { requireAuth } = require('./src/middleware/authMiddleware');
const Student = require('./src/models/Student');
const Mentor = require('./src/models/Mentor');
const { clearCache, clearSearchCache } = require('./src/utils/redis');
const port = 4488 || process.env.PORT;
const dbURI = process.env.DB_URI;
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cookieParser());
const swaggerOptions = {
definition: {
openapi: '3.0.0',
info: {
title: 'MentorBridge API',
version: '1.0.0',
},
},
// Path to the API routes
apis: ['./src/routes/*.js'],
};
const swaggerSpec = swaggerJSDoc(swaggerOptions);
async function ensureIndexes() {
await Student.ensureIndexes();
await Mentor.ensureIndexes();
}
mongoose
.connect(dbURI)
.then(() => ensureIndexes())
.then((result) => {
app.listen(port);
console.log(`Server is running on ${port}`);
})
.catch((err) => console.log(err));
const apiRouter = express.Router();
apiRouter.use('/doubtroom', doubtroomRoutes);
apiRouter.use('/mentor', mentorRoutes);
apiRouter.use('/student', studentRoutes);
apiRouter.use('/search', searchRoutes);
app.use('/api/auth', authRoutes);
app.use('/api', requireAuth, apiRouter);
app.get('/api/clear-all-cache', clearCache);
app.get('/api/search/clear-cache', clearSearchCache);
app.get('/', (req, res) => {
res.setHeader('Content-Type', 'text/html');
res.status(200).send(`<!DOCTYPE html>
<head>
<title>MentorBridge</title>
</head>
<body>
<h1>MentorBridge</h1>
<p>Connecting students with mentors.</p>
<p>Welcome to MentorBridge, where students find guidance and mentors make a difference!</p>
<br><br><br><button><a href="/api-docs">Visit API DOCS</a></button>
</body>
</html>`);
});
app.get('/ping', (req, res) => {
res.status(200).json({ message: 'Pong' });
});
// Serve Swagger UI
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));