-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
65 lines (55 loc) · 1.9 KB
/
index.ts
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
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import helmet from 'helmet';
import morgan from 'morgan';
import { sequelize } from './models'; // Importing sequelize connection
import userRoutes from './routes/userRoutes';
import projectRoutes from './routes/projectRoutes';
import authRoutes from './routes/authRoutes';
import mcsRoutes from './routes/mcsRoutes';
import elevationRoutes from './routes/elevationRoutes';
import errorHandler from './middleware/errorHandler';
import swaggerUi from 'swagger-ui-express';
import swaggerDocument from './config/swagger';
const app: express.Application = express();
// Middlewares
app.use(bodyParser.json());
app.use(cors());
app.use(helmet());
app.use(morgan('combined'));
// API v1 Routes
app.use('/api/v1/users', userRoutes);
app.use('/api/v1/projects', projectRoutes);
app.use('/api/v1/auth', authRoutes);
app.use('/api/v1/mcs', mcsRoutes);
app.use('/api/v1/elevations', elevationRoutes);
// API Documentation with Swagger
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
// Error Handler
app.use(errorHandler);
// Connection to the database and then starting the server
const PORT = process.env.PORT || 3000;
// Function to sync database
const syncDatabase = async () => {
try {
if (process.env.NODE_ENV === 'development') {
await sequelize.sync();
} else if (process.env.NODE_ENV === 'production' && process.env.CREATE_TABLES === 'true') {
await sequelize.sync();
}
console.log('Database & tables synced');
} catch (err) {
console.error('Error syncing database:', err);
}
};
syncDatabase()
.then(() => {
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
})
.catch((error) => {
console.error('Failed to start the server:', error);
process.exit(1);
});