-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
77 lines (63 loc) · 2.11 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
66
67
68
69
70
71
72
73
74
75
76
77
require("dotenv").config();
import express from 'express';
import { startDb, closeDb } from './src/service/db-service';
import cors from 'cors'
import cookieParser from 'cookie-parser';
const PORT = process.env.PORT || 5000
//routes
import publicRouter from './src/router/public-routes';
import authRouter from './src/router/auth-routes';
import userRouter from './src/router/user-routes';
import path from 'path';
const app = express();
app.use(cookieParser());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// app.use(cors({
// origin: 'http://localhost:3000',
// credentials: true
// }));
app.use(
cors({
origin: "*", // Allow all origins (for development only, restrict in production)
methods: ["GET", "POST", "PUT", "DELETE"], // Allowed methods
allowedHeaders: ["Content-Type", "Authorization"], // Allowed headers
})
);
app.use('/images', express.static(path.join(__dirname, './src/templates/images')));
//routes
app.use('/api/public',publicRouter)
app.use('/api/auth',authRouter)
app.use('/api/user',userRouter)
//start server
const startServer = async () => {
try {
// Start the database connection
await startDb();
// Start the server
const server = app.listen(PORT, () => {
console.log(`Server started on PORT = ${PORT}`);
});
// Graceful shutdown
process.on("SIGINT", async () => {
console.log("\nShutting down gracefully...");
await closeDb(); // Close the database connection
server.close(() => {
console.log("Server closed.");
process.exit(0);
});
});
process.on("SIGTERM", async () => {
console.log("\nShutting down gracefully...");
await closeDb(); // Close the database connection
server.close(() => {
console.log("Server closed.");
process.exit(0);
});
});
} catch (error) {
console.error("Failed to start the server:", error);
process.exit(1); // Exit with failure
}
}
startServer()